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 | |
| 19 | import java.util.ArrayList; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 20 | import java.util.Arrays; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 21 | |
| 22 | import android.content.Context; |
| 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 | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 28 | import android.view.MotionEvent; |
| 29 | import android.view.VelocityTracker; |
| 30 | import android.view.View; |
| 31 | import android.view.ViewConfiguration; |
| 32 | import android.view.ViewGroup; |
| 33 | import android.view.ViewParent; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 34 | import android.view.animation.Animation; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 35 | import android.view.animation.Animation.AnimationListener; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 36 | import android.view.animation.AnimationUtils; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 37 | import android.widget.Scroller; |
| 38 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 39 | import com.android.launcher.R; |
| 40 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 41 | /** |
| 42 | * An abstraction of the original Workspace which supports browsing through a |
| 43 | * sequential list of "pages" (or PagedViewCellLayouts). |
| 44 | */ |
| 45 | public abstract class PagedView extends ViewGroup { |
| 46 | private static final String TAG = "PagedView"; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 47 | private static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 48 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 49 | // the velocity at which a fling gesture will cause us to snap to the next page |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 50 | private static final int SNAP_VELOCITY = 500; |
| 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 | |
| 55 | private boolean mFirstLayout = true; |
| 56 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 57 | private int mCurrentPage; |
| 58 | private int mNextPage = INVALID_PAGE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 59 | private Scroller mScroller; |
| 60 | private VelocityTracker mVelocityTracker; |
| 61 | |
| 62 | private float mDownMotionX; |
| 63 | private float mLastMotionX; |
| 64 | private float mLastMotionY; |
| 65 | |
| 66 | private final static int TOUCH_STATE_REST = 0; |
| 67 | private final static int TOUCH_STATE_SCROLLING = 1; |
| 68 | private final static int TOUCH_STATE_PREV_PAGE = 2; |
| 69 | private final static int TOUCH_STATE_NEXT_PAGE = 3; |
| 70 | |
| 71 | private int mTouchState = TOUCH_STATE_REST; |
| 72 | |
| 73 | private OnLongClickListener mLongClickListener; |
| 74 | |
| 75 | private boolean mAllowLongPress = true; |
| 76 | |
| 77 | private int mTouchSlop; |
| 78 | private int mPagingTouchSlop; |
| 79 | private int mMaximumVelocity; |
| 80 | |
| 81 | private static final int INVALID_POINTER = -1; |
| 82 | |
| 83 | private int mActivePointerId = INVALID_POINTER; |
| 84 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 85 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 86 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 87 | private ArrayList<Boolean> mDirtyPageContent; |
| 88 | private boolean mDirtyPageAlpha; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 89 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 90 | public interface PageSwitchListener { |
| 91 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 94 | public PagedView(Context context) { |
| 95 | this(context, null); |
| 96 | } |
| 97 | |
| 98 | public PagedView(Context context, AttributeSet attrs) { |
| 99 | this(context, attrs, 0); |
| 100 | } |
| 101 | |
| 102 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 103 | super(context, attrs, defStyle); |
| 104 | |
| 105 | setHapticFeedbackEnabled(false); |
| 106 | initWorkspace(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Initializes various states for this workspace. |
| 111 | */ |
| 112 | private void initWorkspace() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 113 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 114 | mDirtyPageContent.ensureCapacity(32); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 115 | mScroller = new Scroller(getContext()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 116 | mCurrentPage = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 117 | |
| 118 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 119 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 120 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 121 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 122 | } |
| 123 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 124 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 125 | mPageSwitchListener = pageSwitchListener; |
| 126 | if (mPageSwitchListener != null) { |
| 127 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 132 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 133 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 134 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 135 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 136 | int getCurrentPage() { |
| 137 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 140 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 141 | return getChildCount(); |
| 142 | } |
| 143 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 144 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 145 | return getChildAt(index); |
| 146 | } |
| 147 | |
| 148 | int getScrollWidth() { |
| 149 | return getWidth(); |
| 150 | } |
| 151 | |
| 152 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 153 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 154 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 155 | void setCurrentPage(int currentPage) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 156 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
| 157 | if (getChildCount() == 0) return; |
| 158 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 159 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
| 160 | scrollTo(getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage), 0); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 161 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 162 | invalidate(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 163 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 166 | private void notifyPageSwitchListener() { |
| 167 | if (mPageSwitchListener != null) { |
| 168 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 173 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 174 | * |
| 175 | * @param l The listener used to respond to long clicks. |
| 176 | */ |
| 177 | @Override |
| 178 | public void setOnLongClickListener(OnLongClickListener l) { |
| 179 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 180 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 181 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 182 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void computeScroll() { |
| 188 | if (mScroller.computeScrollOffset()) { |
| 189 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
| 190 | postInvalidate(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 191 | } else if (mNextPage != INVALID_PAGE) { |
| 192 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
| 193 | notifyPageSwitchListener(); |
| 194 | mNextPage = INVALID_PAGE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 200 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 201 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 202 | if (widthMode != MeasureSpec.EXACTLY) { |
| 203 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 204 | } |
| 205 | |
| 206 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 207 | final int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 208 | if (heightMode != MeasureSpec.EXACTLY) { |
| 209 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 210 | } |
| 211 | |
| 212 | // The children are given the same width and height as the workspace |
| 213 | final int childCount = getChildCount(); |
| 214 | for (int i = 0; i < childCount; i++) { |
| 215 | getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); |
| 216 | } |
| 217 | |
| 218 | setMeasuredDimension(widthSize, heightSize); |
| 219 | |
| 220 | if (mFirstLayout) { |
| 221 | setHorizontalScrollBarEnabled(false); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 222 | scrollTo(mCurrentPage * widthSize, 0); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 223 | setHorizontalScrollBarEnabled(true); |
| 224 | mFirstLayout = false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 230 | final int childCount = getChildCount(); |
| 231 | int childLeft = 0; |
| 232 | if (childCount > 0) { |
| 233 | childLeft = (getMeasuredWidth() - getChildAt(0).getMeasuredWidth()) / 2; |
| 234 | } |
| 235 | |
| 236 | for (int i = 0; i < childCount; i++) { |
| 237 | final View child = getChildAt(i); |
| 238 | if (child.getVisibility() != View.GONE) { |
| 239 | final int childWidth = child.getMeasuredWidth(); |
| 240 | child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight()); |
| 241 | childLeft += childWidth; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 246 | @Override |
| 247 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 248 | if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 249 | int screenCenter = mScrollX + (getMeasuredWidth() / 2); |
| 250 | final int childCount = getChildCount(); |
| 251 | for (int i = 0; i < childCount; ++i) { |
| 252 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 253 | int childWidth = layout.getMeasuredWidth(); |
| 254 | int halfChildWidth = (childWidth / 2); |
| 255 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 256 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 257 | float alpha = 0.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 258 | if (distanceFromScreenCenter < halfChildWidth) { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 259 | alpha = 1.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 260 | } else if (distanceFromScreenCenter > childWidth) { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 261 | alpha = 0.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 262 | } else { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 263 | float dimAlpha = (float) (distanceFromScreenCenter - halfChildWidth) / halfChildWidth; |
| 264 | dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha))); |
| 265 | alpha = 1.0f - dimAlpha; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 266 | } |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 267 | if (Float.compare(alpha, layout.getAlpha()) != 0) { |
Winson Chung | b3347bb | 2010-08-19 14:51:28 -0700 | [diff] [blame] | 268 | layout.setAlpha(alpha); |
Winson Chung | affd7b4 | 2010-08-20 15:11:56 -0700 | [diff] [blame] | 269 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 270 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 271 | mDirtyPageAlpha = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 272 | } |
| 273 | super.dispatchDraw(canvas); |
| 274 | } |
| 275 | |
| 276 | @Override |
| 277 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 278 | int page = indexOfChild(child); |
| 279 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 280 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 288 | int focusablePage; |
| 289 | if (mNextPage != INVALID_PAGE) { |
| 290 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 291 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 292 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 293 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 294 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 295 | if (v != null) { |
| 296 | v.requestFocus(direction, previouslyFocusedRect); |
| 297 | } |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 303 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 304 | if (getCurrentPage() > 0) { |
| 305 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 306 | return true; |
| 307 | } |
| 308 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 309 | if (getCurrentPage() < getPageCount() - 1) { |
| 310 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 311 | return true; |
| 312 | } |
| 313 | } |
| 314 | return super.dispatchUnhandledMove(focused, direction); |
| 315 | } |
| 316 | |
| 317 | @Override |
| 318 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 319 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
| 320 | getPageAt(mCurrentPage).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 321 | } |
| 322 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 323 | if (mCurrentPage > 0) { |
| 324 | getPageAt(mCurrentPage - 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 325 | } |
| 326 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 327 | if (mCurrentPage < getPageCount() - 1) { |
| 328 | getPageAt(mCurrentPage + 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * 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^] | 335 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 336 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 337 | * This happens when live folders requery, and if they're off page, they |
| 338 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 339 | */ |
| 340 | @Override |
| 341 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 342 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 343 | View v = focused; |
| 344 | while (true) { |
| 345 | if (v == current) { |
| 346 | super.focusableViewAvailable(focused); |
| 347 | return; |
| 348 | } |
| 349 | if (v == this) { |
| 350 | return; |
| 351 | } |
| 352 | ViewParent parent = v.getParent(); |
| 353 | if (parent instanceof View) { |
| 354 | v = (View)v.getParent(); |
| 355 | } else { |
| 356 | return; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * {@inheritDoc} |
| 363 | */ |
| 364 | @Override |
| 365 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 366 | if (disallowIntercept) { |
| 367 | // We need to make sure to cancel our long press if |
| 368 | // a scrollable widget takes over touch events |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 369 | final View currentPage = getChildAt(mCurrentPage); |
| 370 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 371 | } |
| 372 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 377 | /* |
| 378 | * This method JUST determines whether we want to intercept the motion. |
| 379 | * If we return true, onTouchEvent will be called and we do the actual |
| 380 | * scrolling there. |
| 381 | */ |
| 382 | |
| 383 | /* |
| 384 | * Shortcut the most recurring case: the user is in the dragging |
| 385 | * state and he is moving his finger. We want to intercept this |
| 386 | * motion. |
| 387 | */ |
| 388 | final int action = ev.getAction(); |
| 389 | if ((action == MotionEvent.ACTION_MOVE) && |
| 390 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | switch (action & MotionEvent.ACTION_MASK) { |
| 396 | case MotionEvent.ACTION_MOVE: { |
| 397 | /* |
| 398 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 399 | * whether the user has moved far enough from his original down touch. |
| 400 | */ |
| 401 | determineScrollingStart(ev); |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | case MotionEvent.ACTION_DOWN: { |
| 406 | final float x = ev.getX(); |
| 407 | final float y = ev.getY(); |
| 408 | // Remember location of down touch |
| 409 | mDownMotionX = x; |
| 410 | mLastMotionX = x; |
| 411 | mLastMotionY = y; |
| 412 | mActivePointerId = ev.getPointerId(0); |
| 413 | mAllowLongPress = true; |
| 414 | |
| 415 | /* |
| 416 | * If being flinged and user touches the screen, initiate drag; |
| 417 | * otherwise don't. mScroller.isFinished should be false when |
| 418 | * being flinged. |
| 419 | */ |
| 420 | mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; |
| 421 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 422 | // 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] | 423 | // to scroll the current page |
| 424 | if ((mTouchState != TOUCH_STATE_PREV_PAGE) && |
| 425 | (mTouchState != TOUCH_STATE_NEXT_PAGE)) { |
| 426 | if (getChildCount() > 0) { |
| 427 | int relativeChildLeft = getChildOffset(0); |
| 428 | int relativeChildRight = relativeChildLeft + getChildAt(0).getMeasuredWidth(); |
| 429 | if (x < relativeChildLeft) { |
| 430 | mTouchState = TOUCH_STATE_PREV_PAGE; |
| 431 | } else if (x > relativeChildRight) { |
| 432 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | case MotionEvent.ACTION_CANCEL: |
| 440 | case MotionEvent.ACTION_UP: |
| 441 | // Release the drag |
| 442 | mTouchState = TOUCH_STATE_REST; |
| 443 | mAllowLongPress = false; |
| 444 | mActivePointerId = INVALID_POINTER; |
| 445 | |
| 446 | break; |
| 447 | |
| 448 | case MotionEvent.ACTION_POINTER_UP: |
| 449 | onSecondaryPointerUp(ev); |
| 450 | break; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * The only time we want to intercept motion events is if we are in the |
| 455 | * drag mode. |
| 456 | */ |
| 457 | return mTouchState != TOUCH_STATE_REST; |
| 458 | } |
| 459 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 460 | protected void animateClickFeedback(View v, final Runnable r) { |
| 461 | // animate the view slightly to show click feedback running some logic after it is "pressed" |
| 462 | Animation anim = AnimationUtils.loadAnimation(getContext(), |
| 463 | R.anim.paged_view_click_feedback); |
| 464 | anim.setAnimationListener(new AnimationListener() { |
| 465 | @Override |
| 466 | public void onAnimationStart(Animation animation) {} |
| 467 | @Override |
| 468 | public void onAnimationRepeat(Animation animation) { |
| 469 | r.run(); |
| 470 | } |
| 471 | @Override |
| 472 | public void onAnimationEnd(Animation animation) {} |
| 473 | }); |
| 474 | v.startAnimation(anim); |
| 475 | } |
| 476 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 477 | /* |
| 478 | * Determines if we should change the touch state to start scrolling after the |
| 479 | * user moves their touch point too far. |
| 480 | */ |
| 481 | private void determineScrollingStart(MotionEvent ev) { |
| 482 | /* |
| 483 | * Locally do absolute value. mLastMotionX is set to the y value |
| 484 | * of the down event. |
| 485 | */ |
| 486 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 487 | final float x = ev.getX(pointerIndex); |
| 488 | final float y = ev.getY(pointerIndex); |
| 489 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 490 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 491 | |
| 492 | final int touchSlop = mTouchSlop; |
| 493 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 494 | boolean xMoved = xDiff > touchSlop; |
| 495 | boolean yMoved = yDiff > touchSlop; |
| 496 | |
| 497 | if (xMoved || yMoved) { |
| 498 | if (xPaged) { |
| 499 | // Scroll if the user moved far enough along the X axis |
| 500 | mTouchState = TOUCH_STATE_SCROLLING; |
| 501 | mLastMotionX = x; |
| 502 | } |
| 503 | // Either way, cancel any pending longpress |
| 504 | if (mAllowLongPress) { |
| 505 | mAllowLongPress = false; |
| 506 | // Try canceling the long press. It could also have been scheduled |
| 507 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 508 | // everything |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 509 | final View currentPage = getPageAt(mCurrentPage); |
| 510 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | @Override |
| 516 | public boolean onTouchEvent(MotionEvent ev) { |
| 517 | if (mVelocityTracker == null) { |
| 518 | mVelocityTracker = VelocityTracker.obtain(); |
| 519 | } |
| 520 | mVelocityTracker.addMovement(ev); |
| 521 | |
| 522 | final int action = ev.getAction(); |
| 523 | |
| 524 | switch (action & MotionEvent.ACTION_MASK) { |
| 525 | case MotionEvent.ACTION_DOWN: |
| 526 | /* |
| 527 | * If being flinged and user touches, stop the fling. isFinished |
| 528 | * will be false if being flinged. |
| 529 | */ |
| 530 | if (!mScroller.isFinished()) { |
| 531 | mScroller.abortAnimation(); |
| 532 | } |
| 533 | |
| 534 | // Remember where the motion event started |
| 535 | mDownMotionX = mLastMotionX = ev.getX(); |
| 536 | mActivePointerId = ev.getPointerId(0); |
| 537 | break; |
| 538 | |
| 539 | case MotionEvent.ACTION_MOVE: |
| 540 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 541 | // Scroll to follow the motion event |
| 542 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 543 | final float x = ev.getX(pointerIndex); |
| 544 | final int deltaX = (int) (mLastMotionX - x); |
| 545 | mLastMotionX = x; |
| 546 | |
| 547 | int sx = getScrollX(); |
| 548 | if (deltaX < 0) { |
| 549 | if (sx > 0) { |
| 550 | scrollBy(Math.max(-sx, deltaX), 0); |
| 551 | } |
| 552 | } else if (deltaX > 0) { |
| 553 | final int lastChildIndex = getChildCount() - 1; |
| 554 | final int availableToScroll = getChildOffset(lastChildIndex) - |
| 555 | getRelativeChildOffset(lastChildIndex) - sx; |
| 556 | if (availableToScroll > 0) { |
| 557 | scrollBy(Math.min(availableToScroll, deltaX), 0); |
| 558 | } |
| 559 | } else { |
| 560 | awakenScrollBars(); |
| 561 | } |
| 562 | } else if ((mTouchState == TOUCH_STATE_PREV_PAGE) || |
| 563 | (mTouchState == TOUCH_STATE_NEXT_PAGE)) { |
| 564 | determineScrollingStart(ev); |
| 565 | } |
| 566 | break; |
| 567 | |
| 568 | case MotionEvent.ACTION_UP: |
| 569 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 570 | final int activePointerId = mActivePointerId; |
| 571 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 572 | final float x = ev.getX(pointerIndex); |
| 573 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 574 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 575 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
| 576 | boolean isfling = Math.abs(mDownMotionX - x) > MIN_LENGTH_FOR_FLING; |
| 577 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 578 | if (isfling && velocityX > SNAP_VELOCITY && mCurrentPage > 0) { |
| 579 | snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 580 | } else if (isfling && velocityX < -SNAP_VELOCITY && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 581 | mCurrentPage < getChildCount() - 1) { |
| 582 | snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 583 | } else { |
| 584 | snapToDestination(); |
| 585 | } |
| 586 | |
| 587 | if (mVelocityTracker != null) { |
| 588 | mVelocityTracker.recycle(); |
| 589 | mVelocityTracker = null; |
| 590 | } |
| 591 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 592 | // at this point we have not moved beyond the touch slop |
| 593 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 594 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 595 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 596 | if (nextPage != mCurrentPage) { |
| 597 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 598 | } else { |
| 599 | snapToDestination(); |
| 600 | } |
| 601 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
| 602 | // at this point we have not moved beyond the touch slop |
| 603 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 604 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 605 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 606 | if (nextPage != mCurrentPage) { |
| 607 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 608 | } else { |
| 609 | snapToDestination(); |
| 610 | } |
| 611 | } |
| 612 | mTouchState = TOUCH_STATE_REST; |
| 613 | mActivePointerId = INVALID_POINTER; |
| 614 | break; |
| 615 | |
| 616 | case MotionEvent.ACTION_CANCEL: |
| 617 | mTouchState = TOUCH_STATE_REST; |
| 618 | mActivePointerId = INVALID_POINTER; |
| 619 | break; |
| 620 | |
| 621 | case MotionEvent.ACTION_POINTER_UP: |
| 622 | onSecondaryPointerUp(ev); |
| 623 | break; |
| 624 | } |
| 625 | |
| 626 | return true; |
| 627 | } |
| 628 | |
| 629 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 630 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 631 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 632 | final int pointerId = ev.getPointerId(pointerIndex); |
| 633 | if (pointerId == mActivePointerId) { |
| 634 | // This was our active pointer going up. Choose a new |
| 635 | // active pointer and adjust accordingly. |
| 636 | // TODO: Make this decision more intelligent. |
| 637 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 638 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 639 | mLastMotionY = ev.getY(newPointerIndex); |
| 640 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 641 | if (mVelocityTracker != null) { |
| 642 | mVelocityTracker.clear(); |
| 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public void requestChildFocus(View child, View focused) { |
| 649 | super.requestChildFocus(child, focused); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 650 | int page = indexOfChild(child); |
| 651 | if (page >= 0 && !isInTouchMode()) { |
| 652 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | protected int getRelativeChildOffset(int index) { |
| 657 | return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2; |
| 658 | } |
| 659 | |
| 660 | protected int getChildOffset(int index) { |
| 661 | if (getChildCount() == 0) |
| 662 | return 0; |
| 663 | |
| 664 | int offset = getRelativeChildOffset(0); |
| 665 | for (int i = 0; i < index; ++i) { |
| 666 | offset += getChildAt(i).getMeasuredWidth(); |
| 667 | } |
| 668 | return offset; |
| 669 | } |
| 670 | |
| 671 | protected void snapToDestination() { |
| 672 | int minDistanceFromScreenCenter = getMeasuredWidth(); |
| 673 | int minDistanceFromScreenCenterIndex = -1; |
| 674 | int screenCenter = mScrollX + (getMeasuredWidth() / 2); |
| 675 | final int childCount = getChildCount(); |
| 676 | for (int i = 0; i < childCount; ++i) { |
| 677 | PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 678 | int childWidth = layout.getMeasuredWidth(); |
| 679 | int halfChildWidth = (childWidth / 2); |
| 680 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 681 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 682 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 683 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 684 | minDistanceFromScreenCenterIndex = i; |
| 685 | } |
| 686 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 687 | snapToPage(minDistanceFromScreenCenterIndex, 1000); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 690 | void snapToPage(int whichPage) { |
| 691 | snapToPage(whichPage, 1000); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 694 | void snapToPage(int whichPage, int duration) { |
| 695 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 696 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 697 | mNextPage = whichPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 698 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 699 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 700 | final int sX = getScrollX(); |
| 701 | final int delta = newX - sX; |
| 702 | awakenScrollBars(duration); |
| 703 | if (duration == 0) { |
| 704 | duration = Math.abs(delta); |
| 705 | } |
| 706 | |
| 707 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
| 708 | mScroller.startScroll(sX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 709 | |
| 710 | // only load some associated pages |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 711 | loadAssociatedPages(mNextPage); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 712 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 713 | invalidate(); |
| 714 | } |
| 715 | |
| 716 | @Override |
| 717 | protected Parcelable onSaveInstanceState() { |
| 718 | final SavedState state = new SavedState(super.onSaveInstanceState()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 719 | state.currentPage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 720 | return state; |
| 721 | } |
| 722 | |
| 723 | @Override |
| 724 | protected void onRestoreInstanceState(Parcelable state) { |
| 725 | SavedState savedState = (SavedState) state; |
| 726 | super.onRestoreInstanceState(savedState.getSuperState()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 727 | if (savedState.currentPage != -1) { |
| 728 | mCurrentPage = savedState.currentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | public void scrollLeft() { |
| 733 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 734 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 735 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 736 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | |
| 740 | public void scrollRight() { |
| 741 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 742 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 743 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 744 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 748 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 749 | int result = -1; |
| 750 | if (v != null) { |
| 751 | ViewParent vp = v.getParent(); |
| 752 | int count = getChildCount(); |
| 753 | for (int i = 0; i < count; i++) { |
| 754 | if (vp == getChildAt(i)) { |
| 755 | return i; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | return result; |
| 760 | } |
| 761 | |
| 762 | /** |
| 763 | * @return True is long presses are still allowed for the current touch |
| 764 | */ |
| 765 | public boolean allowLongPress() { |
| 766 | return mAllowLongPress; |
| 767 | } |
| 768 | |
| 769 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 770 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 771 | |
| 772 | SavedState(Parcelable superState) { |
| 773 | super(superState); |
| 774 | } |
| 775 | |
| 776 | private SavedState(Parcel in) { |
| 777 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 778 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | @Override |
| 782 | public void writeToParcel(Parcel out, int flags) { |
| 783 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 784 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 788 | new Parcelable.Creator<SavedState>() { |
| 789 | public SavedState createFromParcel(Parcel in) { |
| 790 | return new SavedState(in); |
| 791 | } |
| 792 | |
| 793 | public SavedState[] newArray(int size) { |
| 794 | return new SavedState[size]; |
| 795 | } |
| 796 | }; |
| 797 | } |
| 798 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 799 | public void loadAssociatedPages(int page) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 800 | final int count = getChildCount(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 801 | if (page < count) { |
| 802 | int lowerPageBound = Math.max(0, page - 1); |
| 803 | int upperPageBound = Math.min(page + 1, count - 1); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 804 | for (int i = 0; i < count; ++i) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 805 | final PagedViewCellLayout layout = (PagedViewCellLayout) getChildAt(i); |
| 806 | final int childCount = layout.getChildCount(); |
| 807 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 808 | if (mDirtyPageContent.get(i)) { |
| 809 | syncPageItems(i); |
| 810 | mDirtyPageContent.set(i, false); |
| 811 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 812 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 813 | if (childCount > 0) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 814 | layout.removeAllViews(); |
| 815 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 816 | mDirtyPageContent.set(i, true); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 817 | } |
| 818 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 822 | /** |
| 823 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 824 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 825 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 826 | * and therefore, individual page items do not need to be updated in this method. |
| 827 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 828 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 829 | |
| 830 | /** |
| 831 | * This method is called to synchronize the items that are on a particular page. If views on |
| 832 | * the page can be reused, then they should be updated within this method. |
| 833 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 834 | public abstract void syncPageItems(int page); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 835 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 836 | public void invalidatePageData() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 837 | // Update all the pages |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 838 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame^] | 839 | |
| 840 | // Mark each of the pages as dirty |
| 841 | final int count = getChildCount(); |
| 842 | mDirtyPageContent.clear(); |
| 843 | for (int i = 0; i < count; ++i) { |
| 844 | mDirtyPageContent.add(true); |
| 845 | } |
| 846 | |
| 847 | // Load any pages that are necessary for the current window of views |
| 848 | loadAssociatedPages(mCurrentPage); |
| 849 | mDirtyPageAlpha = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 850 | requestLayout(); |
| 851 | } |
| 852 | } |