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