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