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