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