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