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