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