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