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