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