Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 19 | import java.util.ArrayList; |
| 20 | import java.util.HashMap; |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 21 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 25 | import android.graphics.Canvas; |
| 26 | import android.graphics.Rect; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
| 29 | import android.util.AttributeSet; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 31 | import android.view.ActionMode; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 32 | import android.view.MotionEvent; |
| 33 | import android.view.VelocityTracker; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewConfiguration; |
| 36 | import android.view.ViewGroup; |
| 37 | import android.view.ViewParent; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 38 | import android.view.animation.Animation; |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 39 | import android.view.animation.Animation.AnimationListener; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 40 | import android.view.animation.AnimationUtils; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 41 | import android.widget.Checkable; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 42 | import android.widget.LinearLayout; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 43 | import android.widget.Scroller; |
| 44 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 45 | import com.android.launcher.R; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 46 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 47 | /** |
| 48 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 49 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 50 | */ |
| 51 | public abstract class PagedView extends ViewGroup { |
| 52 | private static final String TAG = "PagedView"; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 53 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 54 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 55 | // the min drag distance for a fling to register, to prevent random page shifts |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 56 | private static final int MIN_LENGTH_FOR_FLING = 50; |
| 57 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 58 | private static final int PAGE_SNAP_ANIMATION_DURATION = 1000; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 59 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 60 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 61 | // the velocity at which a fling gesture will cause us to snap to the next page |
| 62 | protected int mSnapVelocity = 500; |
| 63 | |
| 64 | protected float mSmoothingTime; |
| 65 | protected float mTouchX; |
| 66 | |
| 67 | protected boolean mFirstLayout = true; |
| 68 | |
| 69 | protected int mCurrentPage; |
| 70 | protected int mNextPage = INVALID_PAGE; |
| 71 | protected Scroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 72 | private VelocityTracker mVelocityTracker; |
| 73 | |
| 74 | private float mDownMotionX; |
| 75 | private float mLastMotionX; |
| 76 | private float mLastMotionY; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 77 | private int mLastScreenCenter = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 78 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 79 | protected final static int TOUCH_STATE_REST = 0; |
| 80 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 81 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 82 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 83 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 84 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 85 | protected int mTouchState = TOUCH_STATE_REST; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 86 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 87 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 88 | |
| 89 | private boolean mAllowLongPress = true; |
| 90 | |
| 91 | private int mTouchSlop; |
| 92 | private int mPagingTouchSlop; |
| 93 | private int mMaximumVelocity; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 94 | protected int mPageSpacing; |
| 95 | protected int mPageLayoutPaddingTop; |
| 96 | protected int mPageLayoutPaddingBottom; |
| 97 | protected int mPageLayoutPaddingLeft; |
| 98 | protected int mPageLayoutPaddingRight; |
| 99 | protected int mCellCountX; |
| 100 | protected int mCellCountY; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 101 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 102 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 103 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 104 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 105 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 106 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 107 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 108 | private ArrayList<Boolean> mDirtyPageContent; |
| 109 | private boolean mDirtyPageAlpha; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 110 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 111 | // choice modes |
| 112 | protected static final int CHOICE_MODE_NONE = 0; |
| 113 | protected static final int CHOICE_MODE_SINGLE = 1; |
| 114 | // Multiple selection mode is not supported by all Launcher actions atm |
| 115 | protected static final int CHOICE_MODE_MULTIPLE = 2; |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 116 | |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 117 | protected int mChoiceMode; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 118 | private ActionMode mActionMode; |
| 119 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 120 | protected PagedViewIconCache mPageViewIconCache; |
| 121 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 122 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 123 | protected boolean mContentIsRefreshable = true; |
| 124 | |
| 125 | // If true, modify alpha of neighboring pages as user scrolls left/right |
| 126 | protected boolean mFadeInAdjacentScreens = true; |
| 127 | |
| 128 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 129 | // to switch to a new page |
| 130 | protected boolean mUsePagingTouchSlop = true; |
| 131 | |
| 132 | // If true, the subclass should directly update mScrollX itself in its computeScroll method |
| 133 | // (SmoothPagedView does this) |
| 134 | protected boolean mDeferScrollUpdate = false; |
| 135 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 136 | protected boolean mIsPageMoving = false; |
| 137 | |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Simple cache mechanism for PagedViewIcon outlines. |
| 140 | */ |
| 141 | class PagedViewIconCache { |
| 142 | private final HashMap<Object, Bitmap> iconOutlineCache = new HashMap<Object, Bitmap>(); |
| 143 | |
| 144 | public void clear() { |
| 145 | iconOutlineCache.clear(); |
| 146 | } |
| 147 | public void addOutline(Object key, Bitmap b) { |
| 148 | iconOutlineCache.put(key, b); |
| 149 | } |
| 150 | public void removeOutline(Object key) { |
| 151 | if (iconOutlineCache.containsKey(key)) { |
| 152 | iconOutlineCache.remove(key); |
| 153 | } |
| 154 | } |
| 155 | public Bitmap getOutline(Object key) { |
| 156 | return iconOutlineCache.get(key); |
| 157 | } |
| 158 | } |
| 159 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 160 | public interface PageSwitchListener { |
| 161 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 164 | public PagedView(Context context) { |
| 165 | this(context, null); |
| 166 | } |
| 167 | |
| 168 | public PagedView(Context context, AttributeSet attrs) { |
| 169 | this(context, attrs, 0); |
| 170 | } |
| 171 | |
| 172 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 173 | super(context, attrs, defStyle); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 174 | mChoiceMode = CHOICE_MODE_NONE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 175 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 176 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 177 | R.styleable.PagedView, defStyle, 0); |
| 178 | mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0); |
| 179 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
| 180 | R.styleable.PagedView_pageLayoutPaddingTop, 10); |
| 181 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
| 182 | R.styleable.PagedView_pageLayoutPaddingBottom, 10); |
| 183 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
| 184 | R.styleable.PagedView_pageLayoutPaddingLeft, 10); |
| 185 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
| 186 | R.styleable.PagedView_pageLayoutPaddingRight, 10); |
| 187 | a.recycle(); |
| 188 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 189 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 190 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Initializes various states for this workspace. |
| 195 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 196 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 197 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 198 | mDirtyPageContent.ensureCapacity(32); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 199 | mPageViewIconCache = new PagedViewIconCache(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 200 | mScroller = new Scroller(getContext()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 201 | mCurrentPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 202 | |
| 203 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 204 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 205 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 206 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 207 | } |
| 208 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 209 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 210 | mPageSwitchListener = pageSwitchListener; |
| 211 | if (mPageSwitchListener != null) { |
| 212 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 217 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 218 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 219 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 220 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 221 | int getCurrentPage() { |
| 222 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 225 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 226 | return getChildCount(); |
| 227 | } |
| 228 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 229 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 230 | return getChildAt(index); |
| 231 | } |
| 232 | |
| 233 | int getScrollWidth() { |
| 234 | return getWidth(); |
| 235 | } |
| 236 | |
| 237 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 238 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 239 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 240 | void setCurrentPage(int currentPage) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 241 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
| 242 | if (getChildCount() == 0) return; |
| 243 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 244 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Michael Jurka | c0e8fca | 2010-10-06 16:41:29 -0700 | [diff] [blame] | 245 | int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage); |
| 246 | scrollTo(newX, 0); |
| 247 | mScroller.setFinalX(newX); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 248 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 249 | invalidate(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 250 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 253 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 254 | if (mPageSwitchListener != null) { |
| 255 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 259 | private void pageBeginMoving() { |
| 260 | mIsPageMoving = true; |
| 261 | onPageBeginMoving(); |
| 262 | } |
| 263 | |
| 264 | private void pageEndMoving() { |
| 265 | onPageEndMoving(); |
| 266 | mIsPageMoving = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 270 | protected void onPageBeginMoving() { |
| 271 | } |
| 272 | |
| 273 | // a method that subclasses can override to add behavior |
| 274 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 277 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 278 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 279 | * |
| 280 | * @param l The listener used to respond to long clicks. |
| 281 | */ |
| 282 | @Override |
| 283 | public void setOnLongClickListener(OnLongClickListener l) { |
| 284 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 285 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 286 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 287 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 292 | public void scrollTo(int x, int y) { |
| 293 | super.scrollTo(x, y); |
| 294 | mTouchX = x; |
| 295 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 296 | } |
| 297 | |
| 298 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 299 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 300 | if (mScroller.computeScrollOffset()) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 301 | mDirtyPageAlpha = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 302 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 303 | invalidate(); |
| 304 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 305 | } else if (mNextPage != INVALID_PAGE) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 306 | mDirtyPageAlpha = true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 307 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 308 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 309 | notifyPageSwitchListener(); |
| 310 | pageEndMoving(); |
| 311 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 312 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void computeScroll() { |
| 318 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | @Override |
| 322 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 323 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 324 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 325 | if (widthMode != MeasureSpec.EXACTLY) { |
| 326 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 327 | } |
| 328 | |
| 329 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 330 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 331 | if (heightMode != MeasureSpec.EXACTLY) { |
| 332 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 333 | } |
| 334 | |
| 335 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 336 | // unless they were set to WRAP_CONTENT |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 337 | final int childCount = getChildCount(); |
| 338 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 339 | // disallowing padding in paged view (just pass 0) |
| 340 | final View child = getChildAt(i); |
| 341 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 342 | |
| 343 | int childWidthMode; |
| 344 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 345 | childWidthMode = MeasureSpec.AT_MOST; |
| 346 | } else { |
| 347 | childWidthMode = MeasureSpec.EXACTLY; |
| 348 | } |
| 349 | |
| 350 | int childHeightMode; |
| 351 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 352 | childHeightMode = MeasureSpec.AT_MOST; |
| 353 | } else { |
| 354 | childHeightMode = MeasureSpec.EXACTLY; |
| 355 | } |
| 356 | |
| 357 | final int childWidthMeasureSpec = |
| 358 | MeasureSpec.makeMeasureSpec(widthSize, childWidthMode); |
| 359 | final int childHeightMeasureSpec = |
| 360 | MeasureSpec.makeMeasureSpec(heightSize, childHeightMode); |
| 361 | |
| 362 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | setMeasuredDimension(widthSize, heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 369 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Michael Jurka | cfc6294 | 2010-09-14 14:01:07 -0700 | [diff] [blame] | 370 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 371 | setHorizontalScrollBarEnabled(false); |
| 372 | int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage); |
| 373 | scrollTo(newX, 0); |
| 374 | mScroller.setFinalX(newX); |
| 375 | setHorizontalScrollBarEnabled(true); |
| 376 | mFirstLayout = false; |
| 377 | } |
| 378 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 379 | final int childCount = getChildCount(); |
| 380 | int childLeft = 0; |
| 381 | if (childCount > 0) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 382 | childLeft = getRelativeChildOffset(0); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | for (int i = 0; i < childCount; i++) { |
| 386 | final View child = getChildAt(i); |
| 387 | if (child.getVisibility() != View.GONE) { |
| 388 | final int childWidth = child.getMeasuredWidth(); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 389 | final int childHeight = (getMeasuredHeight() - child.getMeasuredHeight()) / 2; |
| 390 | child.layout(childLeft, childHeight, |
| 391 | childLeft + childWidth, childHeight + child.getMeasuredHeight()); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 392 | childLeft += childWidth + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 397 | protected void updateAdjacentPagesAlpha() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 398 | if (mFadeInAdjacentScreens) { |
| 399 | if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 400 | int halfScreenSize = getMeasuredWidth() / 2; |
| 401 | int screenCenter = mScrollX + halfScreenSize; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 402 | final int childCount = getChildCount(); |
| 403 | for (int i = 0; i < childCount; ++i) { |
| 404 | View layout = (View) getChildAt(i); |
| 405 | int childWidth = layout.getMeasuredWidth(); |
| 406 | int halfChildWidth = (childWidth / 2); |
| 407 | int childCenter = getChildOffset(i) + halfChildWidth; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 408 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 409 | // On the first layout, we may not have a width nor a proper offset, so for now |
| 410 | // we should just assume full page width (and calculate the offset according to |
| 411 | // that). |
| 412 | if (childWidth <= 0) { |
| 413 | childWidth = getMeasuredWidth(); |
| 414 | childCenter = (i * childWidth) + (childWidth / 2); |
| 415 | } |
| 416 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 417 | int d = halfChildWidth; |
| 418 | int distanceFromScreenCenter = childCenter - screenCenter; |
| 419 | if (distanceFromScreenCenter > 0) { |
| 420 | if (i > 0) { |
| 421 | d += getChildAt(i - 1).getMeasuredWidth() / 2; |
| 422 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 423 | } else { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 424 | if (i < childCount - 1) { |
| 425 | d += getChildAt(i + 1).getMeasuredWidth() / 2; |
| 426 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 427 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 428 | d += mPageSpacing; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 429 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 430 | // Preventing potential divide-by-zero |
| 431 | d = Math.max(1, d); |
| 432 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 433 | float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d; |
| 434 | dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha))); |
| 435 | float alpha = 1.0f - dimAlpha; |
| 436 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 437 | if (alpha < ALPHA_QUANTIZE_LEVEL) { |
| 438 | alpha = 0.0f; |
| 439 | } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) { |
| 440 | alpha = 1.0f; |
| 441 | } |
| 442 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 443 | if (Float.compare(alpha, layout.getAlpha()) != 0) { |
| 444 | layout.setAlpha(alpha); |
| 445 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 446 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 447 | mDirtyPageAlpha = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 452 | protected void screenScrolled(int screenCenter) { |
| 453 | } |
| 454 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 455 | @Override |
| 456 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 457 | int halfScreenSize = getMeasuredWidth() / 2; |
| 458 | int screenCenter = mScrollX + halfScreenSize; |
| 459 | |
| 460 | if (screenCenter != mLastScreenCenter) { |
| 461 | screenScrolled(screenCenter); |
| 462 | updateAdjacentPagesAlpha(); |
| 463 | mLastScreenCenter = screenCenter; |
| 464 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 465 | |
| 466 | // Find out which screens are visible; as an optimization we only call draw on them |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 467 | // As an optimization, this code assumes that all pages have the same width as the 0th |
| 468 | // page. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 469 | final int pageCount = getChildCount(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 470 | if (pageCount > 0) { |
| 471 | final int pageWidth = getChildAt(0).getMeasuredWidth(); |
| 472 | final int screenWidth = getMeasuredWidth(); |
| 473 | int x = getRelativeChildOffset(0) + pageWidth; |
| 474 | int leftScreen = 0; |
| 475 | int rightScreen = 0; |
| 476 | while (x <= mScrollX) { |
| 477 | leftScreen++; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 478 | x += pageWidth + mPageSpacing; |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 479 | // replace above line with this if you don't assume all pages have same width as 0th |
| 480 | // page: |
| 481 | // x += getChildAt(leftScreen).getMeasuredWidth(); |
| 482 | } |
| 483 | rightScreen = leftScreen; |
| 484 | while (x < mScrollX + screenWidth) { |
| 485 | rightScreen++; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 486 | x += pageWidth + mPageSpacing; |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 487 | // replace above line with this if you don't assume all pages have same width as 0th |
| 488 | // page: |
| 489 | //if (rightScreen < pageCount) { |
| 490 | // x += getChildAt(rightScreen).getMeasuredWidth(); |
| 491 | //} |
| 492 | } |
| 493 | rightScreen = Math.min(getChildCount() - 1, rightScreen); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 494 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 495 | final long drawingTime = getDrawingTime(); |
| 496 | for (int i = leftScreen; i <= rightScreen; i++) { |
| 497 | drawChild(canvas, getChildAt(i), drawingTime); |
| 498 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 499 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 504 | int page = indexOfChild(child); |
| 505 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 506 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 507 | return true; |
| 508 | } |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 514 | int focusablePage; |
| 515 | if (mNextPage != INVALID_PAGE) { |
| 516 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 517 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 518 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 519 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 520 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 521 | if (v != null) { |
| 522 | v.requestFocus(direction, previouslyFocusedRect); |
| 523 | } |
| 524 | return false; |
| 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 529 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 530 | if (getCurrentPage() > 0) { |
| 531 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 532 | return true; |
| 533 | } |
| 534 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 535 | if (getCurrentPage() < getPageCount() - 1) { |
| 536 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 537 | return true; |
| 538 | } |
| 539 | } |
| 540 | return super.dispatchUnhandledMove(focused, direction); |
| 541 | } |
| 542 | |
| 543 | @Override |
| 544 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 545 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
| 546 | getPageAt(mCurrentPage).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 547 | } |
| 548 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 549 | if (mCurrentPage > 0) { |
| 550 | getPageAt(mCurrentPage - 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 551 | } |
| 552 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 553 | if (mCurrentPage < getPageCount() - 1) { |
| 554 | getPageAt(mCurrentPage + 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * If one of our descendant views decides that it could be focused now, only |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 561 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 562 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 563 | * This happens when live folders requery, and if they're off page, they |
| 564 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 565 | */ |
| 566 | @Override |
| 567 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 568 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 569 | View v = focused; |
| 570 | while (true) { |
| 571 | if (v == current) { |
| 572 | super.focusableViewAvailable(focused); |
| 573 | return; |
| 574 | } |
| 575 | if (v == this) { |
| 576 | return; |
| 577 | } |
| 578 | ViewParent parent = v.getParent(); |
| 579 | if (parent instanceof View) { |
| 580 | v = (View)v.getParent(); |
| 581 | } else { |
| 582 | return; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * {@inheritDoc} |
| 589 | */ |
| 590 | @Override |
| 591 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 592 | if (disallowIntercept) { |
| 593 | // We need to make sure to cancel our long press if |
| 594 | // a scrollable widget takes over touch events |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 595 | final View currentPage = getChildAt(mCurrentPage); |
| 596 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 597 | } |
| 598 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 599 | } |
| 600 | |
| 601 | @Override |
| 602 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 603 | /* |
| 604 | * This method JUST determines whether we want to intercept the motion. |
| 605 | * If we return true, onTouchEvent will be called and we do the actual |
| 606 | * scrolling there. |
| 607 | */ |
| 608 | |
| 609 | /* |
| 610 | * Shortcut the most recurring case: the user is in the dragging |
| 611 | * state and he is moving his finger. We want to intercept this |
| 612 | * motion. |
| 613 | */ |
| 614 | final int action = ev.getAction(); |
| 615 | if ((action == MotionEvent.ACTION_MOVE) && |
| 616 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 617 | return true; |
| 618 | } |
| 619 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 620 | switch (action & MotionEvent.ACTION_MASK) { |
| 621 | case MotionEvent.ACTION_MOVE: { |
| 622 | /* |
| 623 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 624 | * whether the user has moved far enough from his original down touch. |
| 625 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 626 | if (mActivePointerId != INVALID_POINTER) { |
| 627 | determineScrollingStart(ev); |
| 628 | break; |
| 629 | } |
| 630 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 631 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 632 | // i.e. fall through to the next case (don't break) |
| 633 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 634 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | case MotionEvent.ACTION_DOWN: { |
| 638 | final float x = ev.getX(); |
| 639 | final float y = ev.getY(); |
| 640 | // Remember location of down touch |
| 641 | mDownMotionX = x; |
| 642 | mLastMotionX = x; |
| 643 | mLastMotionY = y; |
| 644 | mActivePointerId = ev.getPointerId(0); |
| 645 | mAllowLongPress = true; |
| 646 | |
| 647 | /* |
| 648 | * If being flinged and user touches the screen, initiate drag; |
| 649 | * otherwise don't. mScroller.isFinished should be false when |
| 650 | * being flinged. |
| 651 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame^] | 652 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 653 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 654 | if (finishedScrolling) { |
| 655 | mTouchState = TOUCH_STATE_REST; |
| 656 | mScroller.abortAnimation(); |
| 657 | } else { |
| 658 | mTouchState = TOUCH_STATE_SCROLLING; |
| 659 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 660 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 661 | // check if this can be the beginning of a tap on the side of the pages |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 662 | // to scroll the current page |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 663 | if ((mTouchState != TOUCH_STATE_PREV_PAGE) && !handlePagingClicks() && |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 664 | (mTouchState != TOUCH_STATE_NEXT_PAGE)) { |
| 665 | if (getChildCount() > 0) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 666 | int width = getMeasuredWidth(); |
| 667 | int offset = getRelativeChildOffset(mCurrentPage); |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 668 | if (x < offset - mPageSpacing) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 669 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 670 | } else if (x > (width - offset + mPageSpacing)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 671 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | case MotionEvent.ACTION_CANCEL: |
| 679 | case MotionEvent.ACTION_UP: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 680 | mTouchState = TOUCH_STATE_REST; |
| 681 | mAllowLongPress = false; |
| 682 | mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | case MotionEvent.ACTION_POINTER_UP: |
| 686 | onSecondaryPointerUp(ev); |
| 687 | break; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * The only time we want to intercept motion events is if we are in the |
| 692 | * drag mode. |
| 693 | */ |
| 694 | return mTouchState != TOUCH_STATE_REST; |
| 695 | } |
| 696 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 697 | protected void animateClickFeedback(View v, final Runnable r) { |
| 698 | // animate the view slightly to show click feedback running some logic after it is "pressed" |
| 699 | Animation anim = AnimationUtils.loadAnimation(getContext(), |
| 700 | R.anim.paged_view_click_feedback); |
| 701 | anim.setAnimationListener(new AnimationListener() { |
| 702 | @Override |
| 703 | public void onAnimationStart(Animation animation) {} |
| 704 | @Override |
| 705 | public void onAnimationRepeat(Animation animation) { |
| 706 | r.run(); |
| 707 | } |
| 708 | @Override |
| 709 | public void onAnimationEnd(Animation animation) {} |
| 710 | }); |
| 711 | v.startAnimation(anim); |
| 712 | } |
| 713 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 714 | /* |
| 715 | * Determines if we should change the touch state to start scrolling after the |
| 716 | * user moves their touch point too far. |
| 717 | */ |
Michael Jurka | 1adf539 | 2010-10-18 18:10:22 -0700 | [diff] [blame] | 718 | protected void determineScrollingStart(MotionEvent ev) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 719 | /* |
| 720 | * Locally do absolute value. mLastMotionX is set to the y value |
| 721 | * of the down event. |
| 722 | */ |
| 723 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 724 | final float x = ev.getX(pointerIndex); |
| 725 | final float y = ev.getY(pointerIndex); |
| 726 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 727 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 728 | |
| 729 | final int touchSlop = mTouchSlop; |
| 730 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 731 | boolean xMoved = xDiff > touchSlop; |
| 732 | boolean yMoved = yDiff > touchSlop; |
| 733 | |
| 734 | if (xMoved || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 735 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 736 | // Scroll if the user moved far enough along the X axis |
| 737 | mTouchState = TOUCH_STATE_SCROLLING; |
| 738 | mLastMotionX = x; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 739 | mTouchX = mScrollX; |
| 740 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 741 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 742 | } |
| 743 | // Either way, cancel any pending longpress |
| 744 | if (mAllowLongPress) { |
| 745 | mAllowLongPress = false; |
| 746 | // Try canceling the long press. It could also have been scheduled |
| 747 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 748 | // everything |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 749 | final View currentPage = getPageAt(mCurrentPage); |
| 750 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 755 | protected boolean handlePagingClicks() { |
| 756 | return false; |
| 757 | } |
| 758 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 759 | @Override |
| 760 | public boolean onTouchEvent(MotionEvent ev) { |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 761 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 762 | |
| 763 | final int action = ev.getAction(); |
| 764 | |
| 765 | switch (action & MotionEvent.ACTION_MASK) { |
| 766 | case MotionEvent.ACTION_DOWN: |
| 767 | /* |
| 768 | * If being flinged and user touches, stop the fling. isFinished |
| 769 | * will be false if being flinged. |
| 770 | */ |
| 771 | if (!mScroller.isFinished()) { |
| 772 | mScroller.abortAnimation(); |
| 773 | } |
| 774 | |
| 775 | // Remember where the motion event started |
| 776 | mDownMotionX = mLastMotionX = ev.getX(); |
| 777 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 778 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 779 | pageBeginMoving(); |
| 780 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 781 | break; |
| 782 | |
| 783 | case MotionEvent.ACTION_MOVE: |
| 784 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 785 | // Scroll to follow the motion event |
| 786 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 787 | final float x = ev.getX(pointerIndex); |
| 788 | final int deltaX = (int) (mLastMotionX - x); |
| 789 | mLastMotionX = x; |
| 790 | |
| 791 | int sx = getScrollX(); |
| 792 | if (deltaX < 0) { |
| 793 | if (sx > 0) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 794 | mTouchX += Math.max(-mTouchX, deltaX); |
| 795 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 796 | if (!mDeferScrollUpdate) { |
| 797 | scrollBy(Math.max(-sx, deltaX), 0); |
| 798 | } else { |
| 799 | // This will trigger a call to computeScroll() on next drawChild() call |
| 800 | invalidate(); |
| 801 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 802 | } |
| 803 | } else if (deltaX > 0) { |
| 804 | final int lastChildIndex = getChildCount() - 1; |
| 805 | final int availableToScroll = getChildOffset(lastChildIndex) - |
| 806 | getRelativeChildOffset(lastChildIndex) - sx; |
| 807 | if (availableToScroll > 0) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 808 | mTouchX += Math.min(availableToScroll, deltaX); |
| 809 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 810 | if (!mDeferScrollUpdate) { |
| 811 | scrollBy(Math.min(availableToScroll, deltaX), 0); |
| 812 | } else { |
| 813 | // This will trigger a call to computeScroll() on next drawChild() call |
| 814 | invalidate(); |
| 815 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 816 | } |
| 817 | } else { |
| 818 | awakenScrollBars(); |
| 819 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 820 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 821 | determineScrollingStart(ev); |
| 822 | } |
| 823 | break; |
| 824 | |
| 825 | case MotionEvent.ACTION_UP: |
| 826 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 827 | final int activePointerId = mActivePointerId; |
| 828 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 829 | final float x = ev.getX(pointerIndex); |
| 830 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 831 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 832 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
| 833 | boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING; |
| 834 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 835 | final int snapVelocity = mSnapVelocity; |
| 836 | if (isfling && velocityX > snapVelocity && mCurrentPage > 0) { |
| 837 | snapToPageWithVelocity(mCurrentPage - 1, velocityX); |
| 838 | } else if (isfling && velocityX < -snapVelocity && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 839 | mCurrentPage < getChildCount() - 1) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 840 | snapToPageWithVelocity(mCurrentPage + 1, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 841 | } else { |
| 842 | snapToDestination(); |
| 843 | } |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 844 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE && !handlePagingClicks()) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 845 | // at this point we have not moved beyond the touch slop |
| 846 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 847 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 848 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 849 | if (nextPage != mCurrentPage) { |
| 850 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 851 | } else { |
| 852 | snapToDestination(); |
| 853 | } |
Adam Cohen | 21f12b5 | 2010-10-07 17:15:37 -0700 | [diff] [blame] | 854 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE && !handlePagingClicks()) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 855 | // at this point we have not moved beyond the touch slop |
| 856 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 857 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 858 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 859 | if (nextPage != mCurrentPage) { |
| 860 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 861 | } else { |
| 862 | snapToDestination(); |
| 863 | } |
| 864 | } |
| 865 | mTouchState = TOUCH_STATE_REST; |
| 866 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 867 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 868 | break; |
| 869 | |
| 870 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 871 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 872 | snapToDestination(); |
| 873 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 874 | mTouchState = TOUCH_STATE_REST; |
| 875 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 876 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 877 | break; |
| 878 | |
| 879 | case MotionEvent.ACTION_POINTER_UP: |
| 880 | onSecondaryPointerUp(ev); |
| 881 | break; |
| 882 | } |
| 883 | |
| 884 | return true; |
| 885 | } |
| 886 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 887 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 888 | if (mVelocityTracker == null) { |
| 889 | mVelocityTracker = VelocityTracker.obtain(); |
| 890 | } |
| 891 | mVelocityTracker.addMovement(ev); |
| 892 | } |
| 893 | |
| 894 | private void releaseVelocityTracker() { |
| 895 | if (mVelocityTracker != null) { |
| 896 | mVelocityTracker.recycle(); |
| 897 | mVelocityTracker = null; |
| 898 | } |
| 899 | } |
| 900 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 901 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 902 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 903 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 904 | final int pointerId = ev.getPointerId(pointerIndex); |
| 905 | if (pointerId == mActivePointerId) { |
| 906 | // This was our active pointer going up. Choose a new |
| 907 | // active pointer and adjust accordingly. |
| 908 | // TODO: Make this decision more intelligent. |
| 909 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 910 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 911 | mLastMotionY = ev.getY(newPointerIndex); |
| 912 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 913 | if (mVelocityTracker != null) { |
| 914 | mVelocityTracker.clear(); |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | @Override |
| 920 | public void requestChildFocus(View child, View focused) { |
| 921 | super.requestChildFocus(child, focused); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 922 | int page = indexOfChild(child); |
| 923 | if (page >= 0 && !isInTouchMode()) { |
| 924 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 928 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 929 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 930 | int left; |
| 931 | int right; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 932 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 933 | left = getRelativeChildOffset(i); |
| 934 | right = (left + getChildAt(i).getMeasuredWidth()); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 935 | if (left <= relativeOffset && relativeOffset <= right) { |
| 936 | return i; |
| 937 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 938 | } |
| 939 | return -1; |
| 940 | } |
| 941 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 942 | protected int getRelativeChildOffset(int index) { |
| 943 | return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2; |
| 944 | } |
| 945 | |
| 946 | protected int getChildOffset(int index) { |
| 947 | if (getChildCount() == 0) |
| 948 | return 0; |
| 949 | |
| 950 | int offset = getRelativeChildOffset(0); |
| 951 | for (int i = 0; i < index; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 952 | offset += getChildAt(i).getMeasuredWidth() + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 953 | } |
| 954 | return offset; |
| 955 | } |
| 956 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 957 | int getPageNearestToCenterOfScreen() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 958 | int minDistanceFromScreenCenter = getMeasuredWidth(); |
| 959 | int minDistanceFromScreenCenterIndex = -1; |
| 960 | int screenCenter = mScrollX + (getMeasuredWidth() / 2); |
| 961 | final int childCount = getChildCount(); |
| 962 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 963 | View layout = (View) getChildAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 964 | int childWidth = layout.getMeasuredWidth(); |
| 965 | int halfChildWidth = (childWidth / 2); |
| 966 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 967 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 968 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 969 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 970 | minDistanceFromScreenCenterIndex = i; |
| 971 | } |
| 972 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 973 | return minDistanceFromScreenCenterIndex; |
| 974 | } |
| 975 | |
| 976 | protected void snapToDestination() { |
| 977 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 980 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
| 981 | // We ignore velocity in this implementation, but children (e.g. SmoothPagedView) |
| 982 | // can use it |
| 983 | snapToPage(whichPage); |
| 984 | } |
| 985 | |
| 986 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 987 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 990 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 991 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 992 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 993 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 994 | final int sX = getScrollX(); |
| 995 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 996 | snapToPage(whichPage, delta, duration); |
| 997 | } |
| 998 | |
| 999 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1000 | mNextPage = whichPage; |
| 1001 | |
| 1002 | View focusedChild = getFocusedChild(); |
| 1003 | if (focusedChild != null && whichPage != mCurrentPage && |
| 1004 | focusedChild == getChildAt(mCurrentPage)) { |
| 1005 | focusedChild.clearFocus(); |
| 1006 | } |
| 1007 | |
| 1008 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1009 | awakenScrollBars(duration); |
| 1010 | if (duration == 0) { |
| 1011 | duration = Math.abs(delta); |
| 1012 | } |
| 1013 | |
| 1014 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1015 | mScroller.startScroll(getScrollX(), 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1016 | |
| 1017 | // only load some associated pages |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1018 | loadAssociatedPages(mNextPage); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1019 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1020 | invalidate(); |
| 1021 | } |
| 1022 | |
| 1023 | @Override |
| 1024 | protected Parcelable onSaveInstanceState() { |
| 1025 | final SavedState state = new SavedState(super.onSaveInstanceState()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1026 | state.currentPage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1027 | return state; |
| 1028 | } |
| 1029 | |
| 1030 | @Override |
| 1031 | protected void onRestoreInstanceState(Parcelable state) { |
| 1032 | SavedState savedState = (SavedState) state; |
| 1033 | super.onRestoreInstanceState(savedState.getSuperState()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1034 | if (savedState.currentPage != -1) { |
| 1035 | mCurrentPage = savedState.currentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | public void scrollLeft() { |
| 1040 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1041 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1042 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1043 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | public void scrollRight() { |
| 1048 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1049 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1050 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1051 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1055 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1056 | int result = -1; |
| 1057 | if (v != null) { |
| 1058 | ViewParent vp = v.getParent(); |
| 1059 | int count = getChildCount(); |
| 1060 | for (int i = 0; i < count; i++) { |
| 1061 | if (vp == getChildAt(i)) { |
| 1062 | return i; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | return result; |
| 1067 | } |
| 1068 | |
| 1069 | /** |
| 1070 | * @return True is long presses are still allowed for the current touch |
| 1071 | */ |
| 1072 | public boolean allowLongPress() { |
| 1073 | return mAllowLongPress; |
| 1074 | } |
| 1075 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1076 | /** |
| 1077 | * Set true to allow long-press events to be triggered, usually checked by |
| 1078 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1079 | */ |
| 1080 | public void setAllowLongPress(boolean allowLongPress) { |
| 1081 | mAllowLongPress = allowLongPress; |
| 1082 | } |
| 1083 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1084 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1085 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1086 | |
| 1087 | SavedState(Parcelable superState) { |
| 1088 | super(superState); |
| 1089 | } |
| 1090 | |
| 1091 | private SavedState(Parcel in) { |
| 1092 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1093 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | @Override |
| 1097 | public void writeToParcel(Parcel out, int flags) { |
| 1098 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1099 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1103 | new Parcelable.Creator<SavedState>() { |
| 1104 | public SavedState createFromParcel(Parcel in) { |
| 1105 | return new SavedState(in); |
| 1106 | } |
| 1107 | |
| 1108 | public SavedState[] newArray(int size) { |
| 1109 | return new SavedState[size]; |
| 1110 | } |
| 1111 | }; |
| 1112 | } |
| 1113 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1114 | public void loadAssociatedPages(int page) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1115 | if (mContentIsRefreshable) { |
| 1116 | final int count = getChildCount(); |
| 1117 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1118 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1119 | int upperPageBound = getAssociatedUpperPageBound(page); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1120 | for (int i = 0; i < count; ++i) { |
| 1121 | final ViewGroup layout = (ViewGroup) getChildAt(i); |
| 1122 | final int childCount = layout.getChildCount(); |
| 1123 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1124 | if (mDirtyPageContent.get(i)) { |
| 1125 | syncPageItems(i); |
| 1126 | mDirtyPageContent.set(i, false); |
| 1127 | } |
| 1128 | } else { |
| 1129 | if (childCount > 0) { |
| 1130 | layout.removeAllViews(); |
| 1131 | } |
| 1132 | mDirtyPageContent.set(i, true); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1133 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1134 | } |
| 1135 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1139 | protected int getAssociatedLowerPageBound(int page) { |
| 1140 | return Math.max(0, page - 1); |
| 1141 | } |
| 1142 | protected int getAssociatedUpperPageBound(int page) { |
| 1143 | final int count = getChildCount(); |
| 1144 | return Math.min(page + 1, count - 1); |
| 1145 | } |
| 1146 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1147 | protected void startChoiceMode(int mode, ActionMode.Callback callback) { |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 1148 | if (isChoiceMode(CHOICE_MODE_NONE)) { |
| 1149 | mChoiceMode = mode; |
| 1150 | mActionMode = startActionMode(callback); |
| 1151 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1152 | } |
| 1153 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1154 | public void endChoiceMode() { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1155 | if (!isChoiceMode(CHOICE_MODE_NONE)) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1156 | mChoiceMode = CHOICE_MODE_NONE; |
| 1157 | resetCheckedGrandchildren(); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 1158 | if (mActionMode != null) mActionMode.finish(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1159 | mActionMode = null; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | protected boolean isChoiceMode(int mode) { |
| 1164 | return mChoiceMode == mode; |
| 1165 | } |
| 1166 | |
| 1167 | protected ArrayList<Checkable> getCheckedGrandchildren() { |
| 1168 | ArrayList<Checkable> checked = new ArrayList<Checkable>(); |
| 1169 | final int childCount = getChildCount(); |
| 1170 | for (int i = 0; i < childCount; ++i) { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1171 | final ViewGroup layout = (ViewGroup) getChildAt(i); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1172 | final int grandChildCount = layout.getChildCount(); |
| 1173 | for (int j = 0; j < grandChildCount; ++j) { |
| 1174 | final View v = layout.getChildAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1175 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1176 | checked.add((Checkable) v); |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | return checked; |
| 1181 | } |
| 1182 | |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1183 | /** |
| 1184 | * If in CHOICE_MODE_SINGLE and an item is checked, returns that item. |
| 1185 | * Otherwise, returns null. |
| 1186 | */ |
| 1187 | protected Checkable getSingleCheckedGrandchild() { |
| 1188 | if (mChoiceMode == CHOICE_MODE_SINGLE) { |
| 1189 | final int childCount = getChildCount(); |
| 1190 | for (int i = 0; i < childCount; ++i) { |
Winson Chung | d0d4301 | 2010-09-26 17:26:45 -0700 | [diff] [blame] | 1191 | final ViewGroup layout = (ViewGroup) getChildAt(i); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1192 | final int grandChildCount = layout.getChildCount(); |
| 1193 | for (int j = 0; j < grandChildCount; ++j) { |
| 1194 | final View v = layout.getChildAt(j); |
| 1195 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
| 1196 | return (Checkable) v; |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | return null; |
| 1202 | } |
| 1203 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1204 | public Object getChosenItem() { |
| 1205 | View checkedView = (View) getSingleCheckedGrandchild(); |
| 1206 | if (checkedView != null) { |
| 1207 | return checkedView.getTag(); |
| 1208 | } |
| 1209 | return null; |
| 1210 | } |
| 1211 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1212 | protected void resetCheckedGrandchildren() { |
| 1213 | // loop through children, and set all of their children to _not_ be checked |
| 1214 | final ArrayList<Checkable> checked = getCheckedGrandchildren(); |
| 1215 | for (int i = 0; i < checked.size(); ++i) { |
| 1216 | final Checkable c = checked.get(i); |
| 1217 | c.setChecked(false); |
| 1218 | } |
| 1219 | } |
| 1220 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1221 | /** |
| 1222 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1223 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1224 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1225 | * and therefore, individual page items do not need to be updated in this method. |
| 1226 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1227 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1228 | |
| 1229 | /** |
| 1230 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1231 | * the page can be reused, then they should be updated within this method. |
| 1232 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1233 | public abstract void syncPageItems(int page); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1234 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1235 | public void invalidatePageData() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1236 | if (mContentIsRefreshable) { |
| 1237 | // Update all the pages |
| 1238 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1239 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1240 | // Mark each of the pages as dirty |
| 1241 | final int count = getChildCount(); |
| 1242 | mDirtyPageContent.clear(); |
| 1243 | for (int i = 0; i < count; ++i) { |
| 1244 | mDirtyPageContent.add(true); |
| 1245 | } |
| 1246 | |
| 1247 | // Load any pages that are necessary for the current window of views |
| 1248 | loadAssociatedPages(mCurrentPage); |
| 1249 | mDirtyPageAlpha = true; |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 1250 | updateAdjacentPagesAlpha(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1251 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1252 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1253 | } |
| 1254 | } |