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