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