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