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 | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 503 | protected void updateAdjacentPagesAlpha() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 504 | if (mFadeInAdjacentScreens) { |
| 505 | if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 506 | int halfScreenSize = getMeasuredWidth() / 2; |
| 507 | int screenCenter = mScrollX + halfScreenSize; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 508 | final int childCount = getChildCount(); |
| 509 | for (int i = 0; i < childCount; ++i) { |
| 510 | View layout = (View) getChildAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 511 | int childWidth = getScaledMeasuredWidth(layout); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 512 | int halfChildWidth = (childWidth / 2); |
| 513 | int childCenter = getChildOffset(i) + halfChildWidth; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 514 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 515 | // On the first layout, we may not have a width nor a proper offset, so for now |
| 516 | // we should just assume full page width (and calculate the offset according to |
| 517 | // that). |
| 518 | if (childWidth <= 0) { |
| 519 | childWidth = getMeasuredWidth(); |
| 520 | childCenter = (i * childWidth) + (childWidth / 2); |
| 521 | } |
| 522 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 523 | int d = halfChildWidth; |
| 524 | int distanceFromScreenCenter = childCenter - screenCenter; |
| 525 | if (distanceFromScreenCenter > 0) { |
| 526 | if (i > 0) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 527 | d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 528 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 529 | } else { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 530 | if (i < childCount - 1) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 531 | d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 532 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 533 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 534 | d += mPageSpacing; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 535 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 536 | // Preventing potential divide-by-zero |
| 537 | d = Math.max(1, d); |
| 538 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 539 | float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d; |
| 540 | dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha))); |
| 541 | float alpha = 1.0f - dimAlpha; |
| 542 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 543 | if (alpha < ALPHA_QUANTIZE_LEVEL) { |
| 544 | alpha = 0.0f; |
| 545 | } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) { |
| 546 | alpha = 1.0f; |
| 547 | } |
| 548 | |
Michael Jurka | cfdb096 | 2011-02-04 01:38:00 -0800 | [diff] [blame] | 549 | //if (Float.compare(alpha, layout.getAlpha()) != 0) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 550 | layout.setAlpha(alpha); |
Michael Jurka | cfdb096 | 2011-02-04 01:38:00 -0800 | [diff] [blame] | 551 | //} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 552 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 553 | mDirtyPageAlpha = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 554 | } |
| 555 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 558 | protected void screenScrolled(int screenCenter) { |
| 559 | } |
| 560 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 561 | @Override |
| 562 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 563 | int halfScreenSize = getMeasuredWidth() / 2; |
| 564 | int screenCenter = mScrollX + halfScreenSize; |
| 565 | |
| 566 | if (screenCenter != mLastScreenCenter) { |
| 567 | screenScrolled(screenCenter); |
| 568 | updateAdjacentPagesAlpha(); |
| 569 | mLastScreenCenter = screenCenter; |
| 570 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 571 | |
| 572 | // 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] | 573 | // As an optimization, this code assumes that all pages have the same width as the 0th |
| 574 | // page. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 575 | final int pageCount = getChildCount(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 576 | if (pageCount > 0) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 577 | final int pageWidth = getScaledMeasuredWidth(getChildAt(0)); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 578 | final int screenWidth = getMeasuredWidth(); |
| 579 | int x = getRelativeChildOffset(0) + pageWidth; |
| 580 | int leftScreen = 0; |
| 581 | int rightScreen = 0; |
| 582 | while (x <= mScrollX) { |
| 583 | leftScreen++; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 584 | x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing; |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 585 | } |
| 586 | rightScreen = leftScreen; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 587 | while (x < mScrollX + screenWidth && rightScreen < pageCount) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 588 | rightScreen++; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 589 | if (rightScreen < pageCount) { |
| 590 | x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing; |
| 591 | } |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 592 | } |
| 593 | rightScreen = Math.min(getChildCount() - 1, rightScreen); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 594 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 595 | final long drawingTime = getDrawingTime(); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 596 | // Clip to the bounds |
| 597 | canvas.save(); |
| 598 | canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft, |
| 599 | mScrollY + mBottom - mTop); |
| 600 | |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 601 | for (int i = 0; i < pageCount; i++) { |
| 602 | View child = getChildAt(i); |
| 603 | if (child != null && child instanceof PagedViewCellLayout) { |
| 604 | boolean willBeDrawn = i >= leftScreen && i <= rightScreen; |
| 605 | if (!willBeDrawn) { |
| 606 | ((PagedViewCellLayout)child).destroyHardwareLayers(); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 611 | for (int i = leftScreen; i <= rightScreen; i++) { |
Michael Jurka | c0759f5 | 2011-02-03 16:47:14 -0800 | [diff] [blame] | 612 | View child = getChildAt(i); |
| 613 | if (child != null && child instanceof PagedViewCellLayout) { |
| 614 | ((PagedViewCellLayout)child).createHardwareLayers(); |
| 615 | } |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 616 | drawChild(canvas, getChildAt(i), drawingTime); |
| 617 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 618 | canvas.restore(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 619 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 624 | int page = indexOfChild(child); |
| 625 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 626 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 627 | return true; |
| 628 | } |
| 629 | return false; |
| 630 | } |
| 631 | |
| 632 | @Override |
| 633 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 634 | int focusablePage; |
| 635 | if (mNextPage != INVALID_PAGE) { |
| 636 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 637 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 638 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 639 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 640 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 641 | if (v != null) { |
| 642 | v.requestFocus(direction, previouslyFocusedRect); |
| 643 | } |
| 644 | return false; |
| 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 649 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 650 | if (getCurrentPage() > 0) { |
| 651 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 652 | return true; |
| 653 | } |
| 654 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 655 | if (getCurrentPage() < getPageCount() - 1) { |
| 656 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 657 | return true; |
| 658 | } |
| 659 | } |
| 660 | return super.dispatchUnhandledMove(focused, direction); |
| 661 | } |
| 662 | |
| 663 | @Override |
| 664 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 665 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
| 666 | getPageAt(mCurrentPage).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 667 | } |
| 668 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 669 | if (mCurrentPage > 0) { |
| 670 | getPageAt(mCurrentPage - 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 671 | } |
| 672 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 673 | if (mCurrentPage < getPageCount() - 1) { |
| 674 | getPageAt(mCurrentPage + 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /** |
| 680 | * 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] | 681 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 682 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 683 | * This happens when live folders requery, and if they're off page, they |
| 684 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 685 | */ |
| 686 | @Override |
| 687 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 688 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 689 | View v = focused; |
| 690 | while (true) { |
| 691 | if (v == current) { |
| 692 | super.focusableViewAvailable(focused); |
| 693 | return; |
| 694 | } |
| 695 | if (v == this) { |
| 696 | return; |
| 697 | } |
| 698 | ViewParent parent = v.getParent(); |
| 699 | if (parent instanceof View) { |
| 700 | v = (View)v.getParent(); |
| 701 | } else { |
| 702 | return; |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * {@inheritDoc} |
| 709 | */ |
| 710 | @Override |
| 711 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 712 | if (disallowIntercept) { |
| 713 | // We need to make sure to cancel our long press if |
| 714 | // a scrollable widget takes over touch events |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 715 | final View currentPage = getChildAt(mCurrentPage); |
| 716 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 717 | } |
| 718 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 719 | } |
| 720 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 721 | /** |
| 722 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 723 | */ |
| 724 | protected boolean hitsPreviousPage(float x, float y) { |
| 725 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 730 | */ |
| 731 | protected boolean hitsNextPage(float x, float y) { |
| 732 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 733 | } |
| 734 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 735 | @Override |
| 736 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 737 | /* |
| 738 | * This method JUST determines whether we want to intercept the motion. |
| 739 | * If we return true, onTouchEvent will be called and we do the actual |
| 740 | * scrolling there. |
| 741 | */ |
| 742 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 743 | // Skip touch handling if there are no pages to swipe |
| 744 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 745 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 746 | /* |
| 747 | * Shortcut the most recurring case: the user is in the dragging |
| 748 | * state and he is moving his finger. We want to intercept this |
| 749 | * motion. |
| 750 | */ |
| 751 | final int action = ev.getAction(); |
| 752 | if ((action == MotionEvent.ACTION_MOVE) && |
| 753 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 754 | return true; |
| 755 | } |
| 756 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 757 | switch (action & MotionEvent.ACTION_MASK) { |
| 758 | case MotionEvent.ACTION_MOVE: { |
| 759 | /* |
| 760 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 761 | * whether the user has moved far enough from his original down touch. |
| 762 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 763 | if (mActivePointerId != INVALID_POINTER) { |
| 764 | determineScrollingStart(ev); |
| 765 | break; |
| 766 | } |
| 767 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 768 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 769 | // i.e. fall through to the next case (don't break) |
| 770 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 771 | // 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] | 772 | } |
| 773 | |
| 774 | case MotionEvent.ACTION_DOWN: { |
| 775 | final float x = ev.getX(); |
| 776 | final float y = ev.getY(); |
| 777 | // Remember location of down touch |
| 778 | mDownMotionX = x; |
| 779 | mLastMotionX = x; |
| 780 | mLastMotionY = y; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 781 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 782 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 783 | mActivePointerId = ev.getPointerId(0); |
| 784 | mAllowLongPress = true; |
| 785 | |
| 786 | /* |
| 787 | * If being flinged and user touches the screen, initiate drag; |
| 788 | * otherwise don't. mScroller.isFinished should be false when |
| 789 | * being flinged. |
| 790 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 791 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 792 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 793 | if (finishedScrolling) { |
| 794 | mTouchState = TOUCH_STATE_REST; |
| 795 | mScroller.abortAnimation(); |
| 796 | } else { |
| 797 | mTouchState = TOUCH_STATE_SCROLLING; |
| 798 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 799 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 800 | // 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] | 801 | // to scroll the current page |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 802 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 803 | if (getChildCount() > 0) { |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 804 | if (hitsPreviousPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 805 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 806 | } else if (hitsNextPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 807 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | break; |
| 812 | } |
| 813 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 814 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 815 | onWallpaperTap(ev); |
| 816 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 817 | mTouchState = TOUCH_STATE_REST; |
| 818 | mAllowLongPress = false; |
| 819 | mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 820 | break; |
| 821 | |
| 822 | case MotionEvent.ACTION_POINTER_UP: |
| 823 | onSecondaryPointerUp(ev); |
| 824 | break; |
| 825 | } |
| 826 | |
| 827 | /* |
| 828 | * The only time we want to intercept motion events is if we are in the |
| 829 | * drag mode. |
| 830 | */ |
| 831 | return mTouchState != TOUCH_STATE_REST; |
| 832 | } |
| 833 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 834 | protected void animateClickFeedback(View v, final Runnable r) { |
| 835 | // 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] | 836 | ObjectAnimator anim = (ObjectAnimator) AnimatorInflater. |
| 837 | loadAnimator(mContext, R.anim.paged_view_click_feedback); |
| 838 | anim.setTarget(v); |
| 839 | anim.addListener(new AnimatorListenerAdapter() { |
| 840 | public void onAnimationRepeat(Animator animation) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 841 | r.run(); |
| 842 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 843 | }); |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 844 | anim.start(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 847 | protected void determineScrollingStart(MotionEvent ev) { |
| 848 | determineScrollingStart(ev, 1.0f); |
| 849 | } |
| 850 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 851 | /* |
| 852 | * Determines if we should change the touch state to start scrolling after the |
| 853 | * user moves their touch point too far. |
| 854 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 855 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 856 | /* |
| 857 | * Locally do absolute value. mLastMotionX is set to the y value |
| 858 | * of the down event. |
| 859 | */ |
| 860 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 861 | final float x = ev.getX(pointerIndex); |
| 862 | final float y = ev.getY(pointerIndex); |
| 863 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 864 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 865 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 866 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 867 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 868 | boolean xMoved = xDiff > touchSlop; |
| 869 | boolean yMoved = yDiff > touchSlop; |
| 870 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 871 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 872 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 873 | // Scroll if the user moved far enough along the X axis |
| 874 | mTouchState = TOUCH_STATE_SCROLLING; |
| 875 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 876 | mLastMotionXRemainder = 0; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 877 | mTouchX = mScrollX; |
| 878 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 879 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 880 | } |
| 881 | // Either way, cancel any pending longpress |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 882 | cancelCurrentPageLongPress(); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | protected void cancelCurrentPageLongPress() { |
| 887 | if (mAllowLongPress) { |
| 888 | mAllowLongPress = false; |
| 889 | // Try canceling the long press. It could also have been scheduled |
| 890 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 891 | // everything |
| 892 | final View currentPage = getPageAt(mCurrentPage); |
| 893 | if (currentPage != null) { |
| 894 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | } |
| 898 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 899 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 900 | // as the user pulls further and further from the bounds |
| 901 | private float overScrollInfluenceCurve(float f) { |
| 902 | f -= 1.0f; |
| 903 | return f * f * f + 1.0f; |
| 904 | } |
| 905 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 906 | protected void overScroll(float amount) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 907 | int screenSize = getMeasuredWidth(); |
| 908 | |
| 909 | float f = (amount / screenSize); |
| 910 | |
| 911 | if (f == 0) return; |
| 912 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 913 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 914 | // Clamp this factor, f, to -1 < f < 1 |
| 915 | if (Math.abs(f) >= 1) { |
| 916 | f /= Math.abs(f); |
| 917 | } |
| 918 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 919 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 920 | if (amount < 0) { |
| 921 | mScrollX = overScrollAmount; |
| 922 | } else { |
| 923 | mScrollX = mMaxScrollX + overScrollAmount; |
| 924 | } |
| 925 | invalidate(); |
| 926 | } |
| 927 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 928 | protected float maxOverScroll() { |
| 929 | // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not |
| 930 | // exceed). Used to find out how much extra wallpaper we need for the overscroll effect |
| 931 | float f = 1.0f; |
| 932 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 933 | return OVERSCROLL_DAMP_FACTOR * f; |
| 934 | } |
| 935 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 936 | @Override |
| 937 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 938 | // Skip touch handling if there are no pages to swipe |
| 939 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 940 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 941 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 942 | |
| 943 | final int action = ev.getAction(); |
| 944 | |
| 945 | switch (action & MotionEvent.ACTION_MASK) { |
| 946 | case MotionEvent.ACTION_DOWN: |
| 947 | /* |
| 948 | * If being flinged and user touches, stop the fling. isFinished |
| 949 | * will be false if being flinged. |
| 950 | */ |
| 951 | if (!mScroller.isFinished()) { |
| 952 | mScroller.abortAnimation(); |
| 953 | } |
| 954 | |
| 955 | // Remember where the motion event started |
| 956 | mDownMotionX = mLastMotionX = ev.getX(); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 957 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 958 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 959 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 960 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 961 | pageBeginMoving(); |
| 962 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 963 | break; |
| 964 | |
| 965 | case MotionEvent.ACTION_MOVE: |
| 966 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 967 | // Scroll to follow the motion event |
| 968 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 969 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 970 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 971 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 972 | mTotalMotionX += Math.abs(deltaX); |
| 973 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 974 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 975 | // keep the remainder because we are actually testing if we've moved from the last |
| 976 | // scrolled position (which is discrete). |
| 977 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 978 | mTouchX += deltaX; |
| 979 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 980 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 981 | scrollBy((int) deltaX, 0); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 982 | } else { |
| 983 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 984 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 985 | mLastMotionX = x; |
| 986 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 987 | } else { |
| 988 | awakenScrollBars(); |
| 989 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 990 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 991 | determineScrollingStart(ev); |
| 992 | } |
| 993 | break; |
| 994 | |
| 995 | case MotionEvent.ACTION_UP: |
| 996 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 997 | final int activePointerId = mActivePointerId; |
| 998 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 999 | final float x = ev.getX(pointerIndex); |
| 1000 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1001 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1002 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1003 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame^] | 1004 | boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1005 | final int snapVelocity = mSnapVelocity; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1006 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame^] | 1007 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1008 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1009 | // In the case that the page is moved far to one direction and then is flung |
| 1010 | // in the opposite direction, we use a threshold to determine whether we should |
| 1011 | // just return to the starting page, or if we should skip one further. |
| 1012 | boolean returnToOriginalPage = false; |
| 1013 | final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage)); |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame^] | 1014 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1015 | Math.signum(velocityX) != Math.signum(deltaX)) { |
| 1016 | returnToOriginalPage = true; |
| 1017 | } |
| 1018 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame^] | 1019 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1020 | Math.abs(velocityX) > snapVelocity; |
| 1021 | |
| 1022 | int finalPage; |
| 1023 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1024 | // test for a large move if a fling has been registered. That is, a large |
| 1025 | // move to the left and fling to the right will register as a fling to the right. |
| 1026 | if (((isSignificantMove && deltaX > 0 && !isFling) || |
| 1027 | (isFling && velocityX > 0)) && mCurrentPage > 0) { |
| 1028 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1029 | snapToPageWithVelocity(finalPage, velocityX); |
| 1030 | } else if (((isSignificantMove && deltaX < 0 && !isFling) || |
| 1031 | (isFling && velocityX < 0)) && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1032 | mCurrentPage < getChildCount() - 1) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1033 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1034 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1035 | } else { |
| 1036 | snapToDestination(); |
| 1037 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1038 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1039 | // at this point we have not moved beyond the touch slop |
| 1040 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1041 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1042 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1043 | if (nextPage != mCurrentPage) { |
| 1044 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1045 | } else { |
| 1046 | snapToDestination(); |
| 1047 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1048 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1049 | // at this point we have not moved beyond the touch slop |
| 1050 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1051 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1052 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1053 | if (nextPage != mCurrentPage) { |
| 1054 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1055 | } else { |
| 1056 | snapToDestination(); |
| 1057 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1058 | } else { |
| 1059 | onWallpaperTap(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1060 | } |
| 1061 | mTouchState = TOUCH_STATE_REST; |
| 1062 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1063 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1064 | break; |
| 1065 | |
| 1066 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1067 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1068 | snapToDestination(); |
| 1069 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1070 | mTouchState = TOUCH_STATE_REST; |
| 1071 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1072 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1073 | break; |
| 1074 | |
| 1075 | case MotionEvent.ACTION_POINTER_UP: |
| 1076 | onSecondaryPointerUp(ev); |
| 1077 | break; |
| 1078 | } |
| 1079 | |
| 1080 | return true; |
| 1081 | } |
| 1082 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1083 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1084 | if (mVelocityTracker == null) { |
| 1085 | mVelocityTracker = VelocityTracker.obtain(); |
| 1086 | } |
| 1087 | mVelocityTracker.addMovement(ev); |
| 1088 | } |
| 1089 | |
| 1090 | private void releaseVelocityTracker() { |
| 1091 | if (mVelocityTracker != null) { |
| 1092 | mVelocityTracker.recycle(); |
| 1093 | mVelocityTracker = null; |
| 1094 | } |
| 1095 | } |
| 1096 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1097 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1098 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1099 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1100 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1101 | if (pointerId == mActivePointerId) { |
| 1102 | // This was our active pointer going up. Choose a new |
| 1103 | // active pointer and adjust accordingly. |
| 1104 | // TODO: Make this decision more intelligent. |
| 1105 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1106 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1107 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1108 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1109 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1110 | if (mVelocityTracker != null) { |
| 1111 | mVelocityTracker.clear(); |
| 1112 | } |
| 1113 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1114 | if (mTouchState == TOUCH_STATE_REST) { |
| 1115 | onWallpaperTap(ev); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | protected void onWallpaperTap(MotionEvent ev) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | @Override |
| 1123 | public void requestChildFocus(View child, View focused) { |
| 1124 | super.requestChildFocus(child, focused); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1125 | int page = indexOfChild(child); |
| 1126 | if (page >= 0 && !isInTouchMode()) { |
| 1127 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1131 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 1132 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1133 | int left; |
| 1134 | int right; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1135 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1136 | left = getRelativeChildOffset(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1137 | right = (left + getScaledMeasuredWidth(getChildAt(i))); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1138 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1139 | return i; |
| 1140 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1141 | } |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1145 | protected int getRelativeChildOffset(int index) { |
| 1146 | return (getMeasuredWidth() - getChildAt(index).getMeasuredWidth()) / 2; |
| 1147 | } |
| 1148 | |
| 1149 | protected int getChildOffset(int index) { |
| 1150 | if (getChildCount() == 0) |
| 1151 | return 0; |
| 1152 | |
| 1153 | int offset = getRelativeChildOffset(0); |
| 1154 | for (int i = 0; i < index; ++i) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1155 | offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1156 | } |
| 1157 | return offset; |
| 1158 | } |
| 1159 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1160 | protected int getScaledMeasuredWidth(View child) { |
| 1161 | return (int) (child.getMeasuredWidth() * mLayoutScale + 0.5f); |
| 1162 | } |
| 1163 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1164 | int getPageNearestToCenterOfScreen() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1165 | int minDistanceFromScreenCenter = getMeasuredWidth(); |
| 1166 | int minDistanceFromScreenCenterIndex = -1; |
| 1167 | int screenCenter = mScrollX + (getMeasuredWidth() / 2); |
| 1168 | final int childCount = getChildCount(); |
| 1169 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1170 | View layout = (View) getChildAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1171 | int childWidth = getScaledMeasuredWidth(layout); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1172 | int halfChildWidth = (childWidth / 2); |
| 1173 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 1174 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1175 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1176 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1177 | minDistanceFromScreenCenterIndex = i; |
| 1178 | } |
| 1179 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1180 | return minDistanceFromScreenCenterIndex; |
| 1181 | } |
| 1182 | |
| 1183 | protected void snapToDestination() { |
| 1184 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1187 | private static class ScrollInterpolator implements Interpolator { |
| 1188 | public ScrollInterpolator() { |
| 1189 | } |
| 1190 | |
| 1191 | public float getInterpolation(float t) { |
| 1192 | t -= 1.0f; |
| 1193 | return t*t*t*t*t + 1; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1198 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1199 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1200 | // of travel has on the overall snap duration. |
| 1201 | float distanceInfluenceForSnapDuration(float f) { |
| 1202 | f -= 0.5f; // center the values about 0. |
| 1203 | f *= 0.3f * Math.PI / 2.0f; |
| 1204 | return (float) Math.sin(f); |
| 1205 | } |
| 1206 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1207 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1208 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 1209 | int halfScreenSize = getMeasuredWidth() / 2; |
| 1210 | |
| 1211 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1212 | int delta = newX - mUnboundedScrollX; |
| 1213 | int duration = 0; |
| 1214 | |
| 1215 | if (Math.abs(velocity) < MIN_FLING_VELOCITY) { |
| 1216 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1217 | // as opposed to an apparent physical response to flinging |
| 1218 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1223 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1224 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1225 | // duration as a function of the distance the page needs to travel. |
| 1226 | float distanceRatio = 1.0f * Math.abs(delta) / 2 * halfScreenSize; |
| 1227 | float distance = halfScreenSize + halfScreenSize * |
| 1228 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1229 | |
| 1230 | velocity = Math.abs(velocity); |
| 1231 | velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity); |
| 1232 | |
| 1233 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1234 | // user flings, so we scale the duration by a value near to the derivative of the scroll |
| 1235 | // interpolator at zero, ie. 5. We use 6 to make it a little slower. |
| 1236 | duration = 6 * Math.round(1000 * Math.abs(distance / velocity)); |
| 1237 | |
| 1238 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1242 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1245 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1246 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1247 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1248 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1249 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1250 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1251 | snapToPage(whichPage, delta, duration); |
| 1252 | } |
| 1253 | |
| 1254 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1255 | mNextPage = whichPage; |
| 1256 | |
| 1257 | View focusedChild = getFocusedChild(); |
| 1258 | if (focusedChild != null && whichPage != mCurrentPage && |
| 1259 | focusedChild == getChildAt(mCurrentPage)) { |
| 1260 | focusedChild.clearFocus(); |
| 1261 | } |
| 1262 | |
| 1263 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1264 | awakenScrollBars(duration); |
| 1265 | if (duration == 0) { |
| 1266 | duration = Math.abs(delta); |
| 1267 | } |
| 1268 | |
| 1269 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1270 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1271 | |
| 1272 | // only load some associated pages |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1273 | loadAssociatedPages(mNextPage); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1274 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1275 | invalidate(); |
| 1276 | } |
| 1277 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1278 | public void scrollLeft() { |
| 1279 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1280 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1281 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1282 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | public void scrollRight() { |
| 1287 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1288 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1289 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1290 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1294 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1295 | int result = -1; |
| 1296 | if (v != null) { |
| 1297 | ViewParent vp = v.getParent(); |
| 1298 | int count = getChildCount(); |
| 1299 | for (int i = 0; i < count; i++) { |
| 1300 | if (vp == getChildAt(i)) { |
| 1301 | return i; |
| 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | return result; |
| 1306 | } |
| 1307 | |
| 1308 | /** |
| 1309 | * @return True is long presses are still allowed for the current touch |
| 1310 | */ |
| 1311 | public boolean allowLongPress() { |
| 1312 | return mAllowLongPress; |
| 1313 | } |
| 1314 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1315 | /** |
| 1316 | * Set true to allow long-press events to be triggered, usually checked by |
| 1317 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1318 | */ |
| 1319 | public void setAllowLongPress(boolean allowLongPress) { |
| 1320 | mAllowLongPress = allowLongPress; |
| 1321 | } |
| 1322 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1323 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1324 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1325 | |
| 1326 | SavedState(Parcelable superState) { |
| 1327 | super(superState); |
| 1328 | } |
| 1329 | |
| 1330 | private SavedState(Parcel in) { |
| 1331 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1332 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | @Override |
| 1336 | public void writeToParcel(Parcel out, int flags) { |
| 1337 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1338 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1342 | new Parcelable.Creator<SavedState>() { |
| 1343 | public SavedState createFromParcel(Parcel in) { |
| 1344 | return new SavedState(in); |
| 1345 | } |
| 1346 | |
| 1347 | public SavedState[] newArray(int size) { |
| 1348 | return new SavedState[size]; |
| 1349 | } |
| 1350 | }; |
| 1351 | } |
| 1352 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1353 | public void loadAssociatedPages(int page) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1354 | if (mContentIsRefreshable) { |
| 1355 | final int count = getChildCount(); |
| 1356 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1357 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1358 | int upperPageBound = getAssociatedUpperPageBound(page); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1359 | for (int i = 0; i < count; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1360 | Page layout = (Page) getChildAt(i); |
| 1361 | final int childCount = layout.getPageChildCount(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1362 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1363 | if (mDirtyPageContent.get(i)) { |
| 1364 | syncPageItems(i); |
| 1365 | mDirtyPageContent.set(i, false); |
| 1366 | } |
| 1367 | } else { |
| 1368 | if (childCount > 0) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1369 | layout.removeAllViewsOnPage(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1370 | } |
| 1371 | mDirtyPageContent.set(i, true); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1372 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1375 | } |
| 1376 | } |
| 1377 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1378 | protected int getAssociatedLowerPageBound(int page) { |
| 1379 | return Math.max(0, page - 1); |
| 1380 | } |
| 1381 | protected int getAssociatedUpperPageBound(int page) { |
| 1382 | final int count = getChildCount(); |
| 1383 | return Math.min(page + 1, count - 1); |
| 1384 | } |
| 1385 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1386 | protected void startChoiceMode(int mode, ActionMode.Callback callback) { |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 1387 | if (isChoiceMode(CHOICE_MODE_NONE)) { |
| 1388 | mChoiceMode = mode; |
| 1389 | mActionMode = startActionMode(callback); |
| 1390 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1393 | public void endChoiceMode() { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1394 | if (!isChoiceMode(CHOICE_MODE_NONE)) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1395 | mChoiceMode = CHOICE_MODE_NONE; |
| 1396 | resetCheckedGrandchildren(); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 1397 | if (mActionMode != null) mActionMode.finish(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1398 | mActionMode = null; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | protected boolean isChoiceMode(int mode) { |
| 1403 | return mChoiceMode == mode; |
| 1404 | } |
| 1405 | |
| 1406 | protected ArrayList<Checkable> getCheckedGrandchildren() { |
| 1407 | ArrayList<Checkable> checked = new ArrayList<Checkable>(); |
| 1408 | final int childCount = getChildCount(); |
| 1409 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1410 | Page layout = (Page) getChildAt(i); |
| 1411 | final int grandChildCount = layout.getPageChildCount(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1412 | for (int j = 0; j < grandChildCount; ++j) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1413 | final View v = layout.getChildOnPageAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1414 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1415 | checked.add((Checkable) v); |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | return checked; |
| 1420 | } |
| 1421 | |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1422 | /** |
| 1423 | * If in CHOICE_MODE_SINGLE and an item is checked, returns that item. |
| 1424 | * Otherwise, returns null. |
| 1425 | */ |
| 1426 | protected Checkable getSingleCheckedGrandchild() { |
| 1427 | if (mChoiceMode == CHOICE_MODE_SINGLE) { |
| 1428 | final int childCount = getChildCount(); |
| 1429 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1430 | Page layout = (Page) getChildAt(i); |
| 1431 | final int grandChildCount = layout.getPageChildCount(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1432 | for (int j = 0; j < grandChildCount; ++j) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1433 | final View v = layout.getChildOnPageAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1434 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
| 1435 | return (Checkable) v; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | return null; |
| 1441 | } |
| 1442 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1443 | public Object getChosenItem() { |
| 1444 | View checkedView = (View) getSingleCheckedGrandchild(); |
| 1445 | if (checkedView != null) { |
| 1446 | return checkedView.getTag(); |
| 1447 | } |
| 1448 | return null; |
| 1449 | } |
| 1450 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1451 | protected void resetCheckedGrandchildren() { |
| 1452 | // loop through children, and set all of their children to _not_ be checked |
| 1453 | final ArrayList<Checkable> checked = getCheckedGrandchildren(); |
| 1454 | for (int i = 0; i < checked.size(); ++i) { |
| 1455 | final Checkable c = checked.get(i); |
| 1456 | c.setChecked(false); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 1460 | public void setRestorePage(int restorePage) { |
| 1461 | mRestorePage = restorePage; |
| 1462 | } |
| 1463 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1464 | /** |
| 1465 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1466 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1467 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1468 | * and therefore, individual page items do not need to be updated in this method. |
| 1469 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1470 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1471 | |
| 1472 | /** |
| 1473 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1474 | * the page can be reused, then they should be updated within this method. |
| 1475 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1476 | public abstract void syncPageItems(int page); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1477 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1478 | public void invalidatePageData() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1479 | if (mContentIsRefreshable) { |
| 1480 | // Update all the pages |
| 1481 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1482 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1483 | // Mark each of the pages as dirty |
| 1484 | final int count = getChildCount(); |
| 1485 | mDirtyPageContent.clear(); |
| 1486 | for (int i = 0; i < count; ++i) { |
| 1487 | mDirtyPageContent.add(true); |
| 1488 | } |
| 1489 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 1490 | // Use the restore page if necessary |
| 1491 | if (mRestorePage > -1) { |
| 1492 | mCurrentPage = mRestorePage; |
| 1493 | mRestorePage = -1; |
| 1494 | } |
| 1495 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1496 | // Load any pages that are necessary for the current window of views |
| 1497 | loadAssociatedPages(mCurrentPage); |
| 1498 | mDirtyPageAlpha = true; |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 1499 | updateAdjacentPagesAlpha(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1500 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1501 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |