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