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