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