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