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