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