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