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