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