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