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 | // On certain graphics drivers, if you draw to a off-screen buffer that's not |
| 794 | // used, it can lead to poor performance. We were running into this when |
| 795 | // setChildrenLayersEnabled was called on a CellLayout; that triggered a re-draw |
| 796 | // of that CellLayout's hardware layer, even if that CellLayout wasn't visible. |
| 797 | // As a fix, below we set pages that aren't going to be rendered are to be |
| 798 | // View.INVISIBLE, preventing re-drawing of their hardware layer |
| 799 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 800 | final View v = getPageAt(i); |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 801 | if (mForceDrawAllChildrenNextFrame || |
| 802 | (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 803 | v.setVisibility(VISIBLE); |
| 804 | drawChild(canvas, v, drawingTime); |
| 805 | } else { |
| 806 | v.setVisibility(INVISIBLE); |
| 807 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 808 | } |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 809 | mForceDrawAllChildrenNextFrame = false; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 810 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 811 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 812 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | @Override |
| 816 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 817 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 818 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 819 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 820 | return true; |
| 821 | } |
| 822 | return false; |
| 823 | } |
| 824 | |
| 825 | @Override |
| 826 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 827 | int focusablePage; |
| 828 | if (mNextPage != INVALID_PAGE) { |
| 829 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 830 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 831 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 832 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 833 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 834 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 835 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 836 | } |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | @Override |
| 841 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 842 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 843 | if (getCurrentPage() > 0) { |
| 844 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 845 | return true; |
| 846 | } |
| 847 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 848 | if (getCurrentPage() < getPageCount() - 1) { |
| 849 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 850 | return true; |
| 851 | } |
| 852 | } |
| 853 | return super.dispatchUnhandledMove(focused, direction); |
| 854 | } |
| 855 | |
| 856 | @Override |
| 857 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 858 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 859 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 860 | } |
| 861 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 862 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 863 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 864 | } |
| 865 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 866 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 867 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * 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] | 874 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 875 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 876 | * This happens when live folders requery, and if they're off page, they |
| 877 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 878 | */ |
| 879 | @Override |
| 880 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 881 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 882 | View v = focused; |
| 883 | while (true) { |
| 884 | if (v == current) { |
| 885 | super.focusableViewAvailable(focused); |
| 886 | return; |
| 887 | } |
| 888 | if (v == this) { |
| 889 | return; |
| 890 | } |
| 891 | ViewParent parent = v.getParent(); |
| 892 | if (parent instanceof View) { |
| 893 | v = (View)v.getParent(); |
| 894 | } else { |
| 895 | return; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * {@inheritDoc} |
| 902 | */ |
| 903 | @Override |
| 904 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 905 | if (disallowIntercept) { |
| 906 | // We need to make sure to cancel our long press if |
| 907 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 908 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 909 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 910 | } |
| 911 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 912 | } |
| 913 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 914 | /** |
| 915 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 916 | */ |
| 917 | protected boolean hitsPreviousPage(float x, float y) { |
| 918 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 923 | */ |
| 924 | protected boolean hitsNextPage(float x, float y) { |
| 925 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 926 | } |
| 927 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 928 | @Override |
| 929 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 930 | /* |
| 931 | * This method JUST determines whether we want to intercept the motion. |
| 932 | * If we return true, onTouchEvent will be called and we do the actual |
| 933 | * scrolling there. |
| 934 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 935 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 936 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 937 | // Skip touch handling if there are no pages to swipe |
| 938 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 939 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 940 | /* |
| 941 | * Shortcut the most recurring case: the user is in the dragging |
| 942 | * state and he is moving his finger. We want to intercept this |
| 943 | * motion. |
| 944 | */ |
| 945 | final int action = ev.getAction(); |
| 946 | if ((action == MotionEvent.ACTION_MOVE) && |
| 947 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 948 | return true; |
| 949 | } |
| 950 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 951 | switch (action & MotionEvent.ACTION_MASK) { |
| 952 | case MotionEvent.ACTION_MOVE: { |
| 953 | /* |
| 954 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 955 | * whether the user has moved far enough from his original down touch. |
| 956 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 957 | if (mActivePointerId != INVALID_POINTER) { |
| 958 | determineScrollingStart(ev); |
| 959 | break; |
| 960 | } |
| 961 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 962 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 963 | // i.e. fall through to the next case (don't break) |
| 964 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 965 | // 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] | 966 | } |
| 967 | |
| 968 | case MotionEvent.ACTION_DOWN: { |
| 969 | final float x = ev.getX(); |
| 970 | final float y = ev.getY(); |
| 971 | // Remember location of down touch |
| 972 | mDownMotionX = x; |
| 973 | mLastMotionX = x; |
| 974 | mLastMotionY = y; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 975 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 976 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 977 | mActivePointerId = ev.getPointerId(0); |
| 978 | mAllowLongPress = true; |
| 979 | |
| 980 | /* |
| 981 | * If being flinged and user touches the screen, initiate drag; |
| 982 | * otherwise don't. mScroller.isFinished should be false when |
| 983 | * being flinged. |
| 984 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 985 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 986 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 987 | if (finishedScrolling) { |
| 988 | mTouchState = TOUCH_STATE_REST; |
| 989 | mScroller.abortAnimation(); |
| 990 | } else { |
| 991 | mTouchState = TOUCH_STATE_SCROLLING; |
| 992 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 993 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 994 | // 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] | 995 | // to scroll the current page |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 996 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 997 | if (getChildCount() > 0) { |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 998 | if (hitsPreviousPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 999 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1000 | } else if (hitsNextPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1001 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | break; |
| 1006 | } |
| 1007 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1008 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1009 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1010 | mTouchState = TOUCH_STATE_REST; |
| 1011 | mAllowLongPress = false; |
| 1012 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1013 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1014 | break; |
| 1015 | |
| 1016 | case MotionEvent.ACTION_POINTER_UP: |
| 1017 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1018 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1019 | break; |
| 1020 | } |
| 1021 | |
| 1022 | /* |
| 1023 | * The only time we want to intercept motion events is if we are in the |
| 1024 | * drag mode. |
| 1025 | */ |
| 1026 | return mTouchState != TOUCH_STATE_REST; |
| 1027 | } |
| 1028 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1029 | protected void determineScrollingStart(MotionEvent ev) { |
| 1030 | determineScrollingStart(ev, 1.0f); |
| 1031 | } |
| 1032 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1033 | /* |
| 1034 | * Determines if we should change the touch state to start scrolling after the |
| 1035 | * user moves their touch point too far. |
| 1036 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1037 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1038 | /* |
| 1039 | * Locally do absolute value. mLastMotionX is set to the y value |
| 1040 | * of the down event. |
| 1041 | */ |
| 1042 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Michael Jurka | 2698db4 | 2011-07-13 18:25:14 -0700 | [diff] [blame] | 1043 | if (pointerIndex == -1) { |
| 1044 | return; |
| 1045 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1046 | final float x = ev.getX(pointerIndex); |
| 1047 | final float y = ev.getY(pointerIndex); |
| 1048 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 1049 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 1050 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1051 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1052 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 1053 | boolean xMoved = xDiff > touchSlop; |
| 1054 | boolean yMoved = yDiff > touchSlop; |
| 1055 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1056 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1057 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1058 | // Scroll if the user moved far enough along the X axis |
| 1059 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1060 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1061 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1062 | mLastMotionXRemainder = 0; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1063 | mTouchX = getScrollX(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1064 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1065 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1066 | } |
| 1067 | // Either way, cancel any pending longpress |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1068 | cancelCurrentPageLongPress(); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | protected void cancelCurrentPageLongPress() { |
| 1073 | if (mAllowLongPress) { |
| 1074 | mAllowLongPress = false; |
| 1075 | // Try canceling the long press. It could also have been scheduled |
| 1076 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1077 | // everything |
| 1078 | final View currentPage = getPageAt(mCurrentPage); |
| 1079 | if (currentPage != null) { |
| 1080 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1085 | protected float getScrollProgress(int screenCenter, View v, int page) { |
| 1086 | final int halfScreenSize = getMeasuredWidth() / 2; |
| 1087 | |
| 1088 | int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing; |
| 1089 | int delta = screenCenter - (getChildOffset(page) - |
| 1090 | getRelativeChildOffset(page) + halfScreenSize); |
| 1091 | |
| 1092 | float scrollProgress = delta / (totalDistance * 1.0f); |
| 1093 | scrollProgress = Math.min(scrollProgress, 1.0f); |
| 1094 | scrollProgress = Math.max(scrollProgress, -1.0f); |
| 1095 | return scrollProgress; |
| 1096 | } |
| 1097 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1098 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 1099 | // as the user pulls further and further from the bounds |
| 1100 | private float overScrollInfluenceCurve(float f) { |
| 1101 | f -= 1.0f; |
| 1102 | return f * f * f + 1.0f; |
| 1103 | } |
| 1104 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1105 | protected void acceleratedOverScroll(float amount) { |
| 1106 | int screenSize = getMeasuredWidth(); |
| 1107 | |
| 1108 | // We want to reach the max over scroll effect when the user has |
| 1109 | // over scrolled half the size of the screen |
| 1110 | float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize); |
| 1111 | |
| 1112 | if (f == 0) return; |
| 1113 | |
| 1114 | // Clamp this factor, f, to -1 < f < 1 |
| 1115 | if (Math.abs(f) >= 1) { |
| 1116 | f /= Math.abs(f); |
| 1117 | } |
| 1118 | |
| 1119 | int overScrollAmount = (int) Math.round(f * screenSize); |
| 1120 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1121 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1122 | super.scrollTo(0, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1123 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1124 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1125 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1126 | } |
| 1127 | invalidate(); |
| 1128 | } |
| 1129 | |
| 1130 | protected void dampedOverScroll(float amount) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1131 | int screenSize = getMeasuredWidth(); |
| 1132 | |
| 1133 | float f = (amount / screenSize); |
| 1134 | |
| 1135 | if (f == 0) return; |
| 1136 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1137 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 1138 | // Clamp this factor, f, to -1 < f < 1 |
| 1139 | if (Math.abs(f) >= 1) { |
| 1140 | f /= Math.abs(f); |
| 1141 | } |
| 1142 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1143 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1144 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1145 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1146 | super.scrollTo(0, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1147 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1148 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1149 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1150 | } |
| 1151 | invalidate(); |
| 1152 | } |
| 1153 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1154 | protected void overScroll(float amount) { |
| 1155 | dampedOverScroll(amount); |
| 1156 | } |
| 1157 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1158 | protected float maxOverScroll() { |
| 1159 | // 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] | 1160 | // 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] | 1161 | float f = 1.0f; |
| 1162 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1163 | return OVERSCROLL_DAMP_FACTOR * f; |
| 1164 | } |
| 1165 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1166 | @Override |
| 1167 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1168 | // Skip touch handling if there are no pages to swipe |
| 1169 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1170 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1171 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1172 | |
| 1173 | final int action = ev.getAction(); |
| 1174 | |
| 1175 | switch (action & MotionEvent.ACTION_MASK) { |
| 1176 | case MotionEvent.ACTION_DOWN: |
| 1177 | /* |
| 1178 | * If being flinged and user touches, stop the fling. isFinished |
| 1179 | * will be false if being flinged. |
| 1180 | */ |
| 1181 | if (!mScroller.isFinished()) { |
| 1182 | mScroller.abortAnimation(); |
| 1183 | } |
| 1184 | |
| 1185 | // Remember where the motion event started |
| 1186 | mDownMotionX = mLastMotionX = ev.getX(); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1187 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1188 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1189 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1190 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1191 | pageBeginMoving(); |
| 1192 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1193 | break; |
| 1194 | |
| 1195 | case MotionEvent.ACTION_MOVE: |
| 1196 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1197 | // Scroll to follow the motion event |
| 1198 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 1199 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1200 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1201 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1202 | mTotalMotionX += Math.abs(deltaX); |
| 1203 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1204 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1205 | // keep the remainder because we are actually testing if we've moved from the last |
| 1206 | // scrolled position (which is discrete). |
| 1207 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1208 | mTouchX += deltaX; |
| 1209 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1210 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1211 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1212 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1213 | } else { |
| 1214 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1215 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1216 | mLastMotionX = x; |
| 1217 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1218 | } else { |
| 1219 | awakenScrollBars(); |
| 1220 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1221 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1222 | determineScrollingStart(ev); |
| 1223 | } |
| 1224 | break; |
| 1225 | |
| 1226 | case MotionEvent.ACTION_UP: |
| 1227 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1228 | final int activePointerId = mActivePointerId; |
| 1229 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1230 | final float x = ev.getX(pointerIndex); |
| 1231 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1232 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1233 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1234 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1235 | final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage)); |
| 1236 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1237 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1238 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1239 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1240 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1241 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1242 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1243 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1244 | // In the case that the page is moved far to one direction and then is flung |
| 1245 | // in the opposite direction, we use a threshold to determine whether we should |
| 1246 | // just return to the starting page, or if we should skip one further. |
| 1247 | boolean returnToOriginalPage = false; |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1248 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1249 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1250 | returnToOriginalPage = true; |
| 1251 | } |
| 1252 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1253 | int finalPage; |
| 1254 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1255 | // test for a large move if a fling has been registered. That is, a large |
| 1256 | // move to the left and fling to the right will register as a fling to the right. |
| 1257 | if (((isSignificantMove && deltaX > 0 && !isFling) || |
| 1258 | (isFling && velocityX > 0)) && mCurrentPage > 0) { |
| 1259 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1260 | snapToPageWithVelocity(finalPage, velocityX); |
| 1261 | } else if (((isSignificantMove && deltaX < 0 && !isFling) || |
| 1262 | (isFling && velocityX < 0)) && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1263 | mCurrentPage < getChildCount() - 1) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1264 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1265 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1266 | } else { |
| 1267 | snapToDestination(); |
| 1268 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1269 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1270 | // at this point we have not moved beyond the touch slop |
| 1271 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1272 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1273 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1274 | if (nextPage != mCurrentPage) { |
| 1275 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1276 | } else { |
| 1277 | snapToDestination(); |
| 1278 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1279 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1280 | // at this point we have not moved beyond the touch slop |
| 1281 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1282 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1283 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1284 | if (nextPage != mCurrentPage) { |
| 1285 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1286 | } else { |
| 1287 | snapToDestination(); |
| 1288 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1289 | } else { |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1290 | onUnhandledTap(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1291 | } |
| 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_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1298 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1299 | snapToDestination(); |
| 1300 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1301 | mTouchState = TOUCH_STATE_REST; |
| 1302 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1303 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1304 | break; |
| 1305 | |
| 1306 | case MotionEvent.ACTION_POINTER_UP: |
| 1307 | onSecondaryPointerUp(ev); |
| 1308 | break; |
| 1309 | } |
| 1310 | |
| 1311 | return true; |
| 1312 | } |
| 1313 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1314 | @Override |
| 1315 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1316 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1317 | switch (event.getAction()) { |
| 1318 | case MotionEvent.ACTION_SCROLL: { |
| 1319 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1320 | final float vscroll; |
| 1321 | final float hscroll; |
| 1322 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1323 | vscroll = 0; |
| 1324 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1325 | } else { |
| 1326 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1327 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1328 | } |
| 1329 | if (hscroll != 0 || vscroll != 0) { |
| 1330 | if (hscroll > 0 || vscroll > 0) { |
| 1331 | scrollRight(); |
| 1332 | } else { |
| 1333 | scrollLeft(); |
| 1334 | } |
| 1335 | return true; |
| 1336 | } |
| 1337 | } |
| 1338 | } |
| 1339 | } |
| 1340 | return super.onGenericMotionEvent(event); |
| 1341 | } |
| 1342 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1343 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1344 | if (mVelocityTracker == null) { |
| 1345 | mVelocityTracker = VelocityTracker.obtain(); |
| 1346 | } |
| 1347 | mVelocityTracker.addMovement(ev); |
| 1348 | } |
| 1349 | |
| 1350 | private void releaseVelocityTracker() { |
| 1351 | if (mVelocityTracker != null) { |
| 1352 | mVelocityTracker.recycle(); |
| 1353 | mVelocityTracker = null; |
| 1354 | } |
| 1355 | } |
| 1356 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1357 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1358 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1359 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1360 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1361 | if (pointerId == mActivePointerId) { |
| 1362 | // This was our active pointer going up. Choose a new |
| 1363 | // active pointer and adjust accordingly. |
| 1364 | // TODO: Make this decision more intelligent. |
| 1365 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1366 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1367 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1368 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1369 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1370 | if (mVelocityTracker != null) { |
| 1371 | mVelocityTracker.clear(); |
| 1372 | } |
| 1373 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1376 | protected void onUnhandledTap(MotionEvent ev) {} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1377 | |
| 1378 | @Override |
| 1379 | public void requestChildFocus(View child, View focused) { |
| 1380 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1381 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1382 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1383 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1387 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 1388 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1389 | int left; |
| 1390 | int right; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1391 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1392 | left = getRelativeChildOffset(i); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1393 | right = (left + getScaledMeasuredWidth(getPageAt(i))); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1394 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1395 | return i; |
| 1396 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1397 | } |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1401 | protected int getChildWidth(int index) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1402 | // This functions are called enough times that it actually makes a difference in the |
| 1403 | // profiler -- so just inline the max() here |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1404 | final int measuredWidth = getPageAt(index).getMeasuredWidth(); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1405 | final int minWidth = mMinimumWidth; |
| 1406 | return (minWidth > measuredWidth) ? minWidth : measuredWidth; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1407 | } |
| 1408 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1409 | int getPageNearestToCenterOfScreen() { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1410 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1411 | int minDistanceFromScreenCenterIndex = -1; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1412 | int screenCenter = getScrollX() + (getMeasuredWidth() / 2); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1413 | final int childCount = getChildCount(); |
| 1414 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1415 | View layout = (View) getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1416 | int childWidth = getScaledMeasuredWidth(layout); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1417 | int halfChildWidth = (childWidth / 2); |
| 1418 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 1419 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1420 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1421 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1422 | minDistanceFromScreenCenterIndex = i; |
| 1423 | } |
| 1424 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1425 | return minDistanceFromScreenCenterIndex; |
| 1426 | } |
| 1427 | |
| 1428 | protected void snapToDestination() { |
| 1429 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1432 | private static class ScrollInterpolator implements Interpolator { |
| 1433 | public ScrollInterpolator() { |
| 1434 | } |
| 1435 | |
| 1436 | public float getInterpolation(float t) { |
| 1437 | t -= 1.0f; |
| 1438 | return t*t*t*t*t + 1; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1443 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1444 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1445 | // of travel has on the overall snap duration. |
| 1446 | float distanceInfluenceForSnapDuration(float f) { |
| 1447 | f -= 0.5f; // center the values about 0. |
| 1448 | f *= 0.3f * Math.PI / 2.0f; |
| 1449 | return (float) Math.sin(f); |
| 1450 | } |
| 1451 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1452 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1453 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 1454 | int halfScreenSize = getMeasuredWidth() / 2; |
| 1455 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1456 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1457 | if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): " |
| 1458 | + getMeasuredWidth() + ", " + getChildWidth(whichPage)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1459 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1460 | int delta = newX - mUnboundedScrollX; |
| 1461 | int duration = 0; |
| 1462 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1463 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1464 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1465 | // as opposed to an apparent physical response to flinging |
| 1466 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1471 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1472 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1473 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1474 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1475 | float distance = halfScreenSize + halfScreenSize * |
| 1476 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1477 | |
| 1478 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1479 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1480 | |
| 1481 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1482 | // 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] | 1483 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1484 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1485 | |
| 1486 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1490 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1493 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1494 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1495 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1496 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1497 | if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 1498 | + getChildWidth(whichPage)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1499 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1500 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1501 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1502 | snapToPage(whichPage, delta, duration); |
| 1503 | } |
| 1504 | |
| 1505 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1506 | mNextPage = whichPage; |
| 1507 | |
| 1508 | View focusedChild = getFocusedChild(); |
| 1509 | if (focusedChild != null && whichPage != mCurrentPage && |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1510 | focusedChild == getPageAt(mCurrentPage)) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1511 | focusedChild.clearFocus(); |
| 1512 | } |
| 1513 | |
| 1514 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1515 | awakenScrollBars(duration); |
| 1516 | if (duration == 0) { |
| 1517 | duration = Math.abs(delta); |
| 1518 | } |
| 1519 | |
| 1520 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1521 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1522 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1523 | // Load associated pages immediately if someone else is handling the scroll, otherwise defer |
| 1524 | // loading associated pages until the scroll settles |
| 1525 | if (mDeferScrollUpdate) { |
| 1526 | loadAssociatedPages(mNextPage); |
| 1527 | } else { |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1528 | mDeferLoadAssociatedPagesUntilScrollCompletes = true; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1529 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1530 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1531 | invalidate(); |
| 1532 | } |
| 1533 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1534 | public void scrollLeft() { |
| 1535 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1536 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1537 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1538 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | public void scrollRight() { |
| 1543 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1544 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1545 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1546 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1550 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1551 | int result = -1; |
| 1552 | if (v != null) { |
| 1553 | ViewParent vp = v.getParent(); |
| 1554 | int count = getChildCount(); |
| 1555 | for (int i = 0; i < count; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1556 | if (vp == getPageAt(i)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1557 | return i; |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | return result; |
| 1562 | } |
| 1563 | |
| 1564 | /** |
| 1565 | * @return True is long presses are still allowed for the current touch |
| 1566 | */ |
| 1567 | public boolean allowLongPress() { |
| 1568 | return mAllowLongPress; |
| 1569 | } |
| 1570 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1571 | /** |
| 1572 | * Set true to allow long-press events to be triggered, usually checked by |
| 1573 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1574 | */ |
| 1575 | public void setAllowLongPress(boolean allowLongPress) { |
| 1576 | mAllowLongPress = allowLongPress; |
| 1577 | } |
| 1578 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1579 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1580 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1581 | |
| 1582 | SavedState(Parcelable superState) { |
| 1583 | super(superState); |
| 1584 | } |
| 1585 | |
| 1586 | private SavedState(Parcel in) { |
| 1587 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1588 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | @Override |
| 1592 | public void writeToParcel(Parcel out, int flags) { |
| 1593 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1594 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1598 | new Parcelable.Creator<SavedState>() { |
| 1599 | public SavedState createFromParcel(Parcel in) { |
| 1600 | return new SavedState(in); |
| 1601 | } |
| 1602 | |
| 1603 | public SavedState[] newArray(int size) { |
| 1604 | return new SavedState[size]; |
| 1605 | } |
| 1606 | }; |
| 1607 | } |
| 1608 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1609 | protected void loadAssociatedPages(int page) { |
| 1610 | loadAssociatedPages(page, false); |
| 1611 | } |
| 1612 | protected void loadAssociatedPages(int page, boolean immediateAndOnly) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1613 | if (mContentIsRefreshable) { |
| 1614 | final int count = getChildCount(); |
| 1615 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1616 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1617 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1618 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 1619 | + upperPageBound); |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 1620 | // First, clear any pages that should no longer be loaded |
| 1621 | for (int i = 0; i < count; ++i) { |
| 1622 | Page layout = (Page) getPageAt(i); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 1623 | if ((i < lowerPageBound) || (i > upperPageBound)) { |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 1624 | if (layout.getPageChildCount() > 0) { |
| 1625 | layout.removeAllViewsOnPage(); |
| 1626 | } |
| 1627 | mDirtyPageContent.set(i, true); |
| 1628 | } |
| 1629 | } |
| 1630 | // Next, load any new pages |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1631 | for (int i = 0; i < count; ++i) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1632 | if ((i != page) && immediateAndOnly) { |
| 1633 | continue; |
| 1634 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1635 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1636 | if (mDirtyPageContent.get(i)) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1637 | syncPageItems(i, (i == page) && immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1638 | mDirtyPageContent.set(i, false); |
| 1639 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1640 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1641 | } |
| 1642 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1643 | } |
| 1644 | } |
| 1645 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1646 | protected int getAssociatedLowerPageBound(int page) { |
| 1647 | return Math.max(0, page - 1); |
| 1648 | } |
| 1649 | protected int getAssociatedUpperPageBound(int page) { |
| 1650 | final int count = getChildCount(); |
| 1651 | return Math.min(page + 1, count - 1); |
| 1652 | } |
| 1653 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1654 | /** |
| 1655 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1656 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1657 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1658 | * and therefore, individual page items do not need to be updated in this method. |
| 1659 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1660 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1661 | |
| 1662 | /** |
| 1663 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1664 | * the page can be reused, then they should be updated within this method. |
| 1665 | */ |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1666 | public abstract void syncPageItems(int page, boolean immediate); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1667 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 1668 | protected void invalidatePageData() { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1669 | invalidatePageData(-1, false); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1670 | } |
| 1671 | protected void invalidatePageData(int currentPage) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1672 | invalidatePageData(currentPage, false); |
| 1673 | } |
| 1674 | protected void invalidatePageData(int currentPage, boolean immediateAndOnly) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1675 | if (!mIsDataReady) { |
| 1676 | return; |
| 1677 | } |
| 1678 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1679 | if (mContentIsRefreshable) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1680 | // Force all scrolling-related behavior to end |
| 1681 | mScroller.forceFinished(true); |
| 1682 | mNextPage = INVALID_PAGE; |
| 1683 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1684 | // Update all the pages |
| 1685 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1686 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1687 | // We must force a measure after we've loaded the pages to update the content width and |
| 1688 | // to determine the full scroll width |
| 1689 | measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), |
| 1690 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); |
| 1691 | |
| 1692 | // Set a new page as the current page if necessary |
| 1693 | if (currentPage > -1) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1694 | setCurrentPage(Math.min(getPageCount() - 1, currentPage)); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1695 | } |
| 1696 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1697 | // Mark each of the pages as dirty |
| 1698 | final int count = getChildCount(); |
| 1699 | mDirtyPageContent.clear(); |
| 1700 | for (int i = 0; i < count; ++i) { |
| 1701 | mDirtyPageContent.add(true); |
| 1702 | } |
| 1703 | |
| 1704 | // Load any pages that are necessary for the current window of views |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1705 | loadAssociatedPages(mCurrentPage, immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1706 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1707 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1708 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1709 | |
Michael Jurka | afaa050 | 2011-12-13 18:22:50 -0800 | [diff] [blame] | 1710 | protected View getScrollingIndicator() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1711 | // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator |
| 1712 | // found |
| 1713 | if (mHasScrollIndicator && mScrollIndicator == null) { |
| 1714 | ViewGroup parent = (ViewGroup) getParent(); |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 1715 | if (parent != null) { |
| 1716 | mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator)); |
| 1717 | mHasScrollIndicator = mScrollIndicator != null; |
| 1718 | if (mHasScrollIndicator) { |
| 1719 | mScrollIndicator.setVisibility(View.VISIBLE); |
| 1720 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | return mScrollIndicator; |
| 1724 | } |
| 1725 | |
| 1726 | protected boolean isScrollingIndicatorEnabled() { |
Winson Chung | 649723c | 2011-07-06 20:41:23 -0700 | [diff] [blame] | 1727 | return !LauncherApplication.isScreenLarge(); |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1730 | Runnable hideScrollingIndicatorRunnable = new Runnable() { |
| 1731 | @Override |
| 1732 | public void run() { |
| 1733 | hideScrollingIndicator(false); |
| 1734 | } |
| 1735 | }; |
Michael Jurka | b737ee6 | 2011-11-15 15:57:22 -0800 | [diff] [blame] | 1736 | protected void flashScrollingIndicator(boolean animated) { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1737 | removeCallbacks(hideScrollingIndicatorRunnable); |
Michael Jurka | b737ee6 | 2011-11-15 15:57:22 -0800 | [diff] [blame] | 1738 | showScrollingIndicator(!animated); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1739 | postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1740 | } |
| 1741 | |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1742 | protected void showScrollingIndicator(boolean immediately) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1743 | mShouldShowScrollIndicator = true; |
| 1744 | mShouldShowScrollIndicatorImmediately = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1745 | if (getChildCount() <= 1) return; |
| 1746 | if (!isScrollingIndicatorEnabled()) return; |
| 1747 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1748 | mShouldShowScrollIndicator = false; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1749 | getScrollingIndicator(); |
| 1750 | if (mScrollIndicator != null) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1751 | // Fade the indicator in |
| 1752 | updateScrollingIndicatorPosition(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1753 | mScrollIndicator.setVisibility(View.VISIBLE); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1754 | cancelScrollingIndicatorAnimations(); |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1755 | if (immediately) { |
| 1756 | mScrollIndicator.setAlpha(1f); |
| 1757 | } else { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame^] | 1758 | mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f); |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1759 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration); |
| 1760 | mScrollIndicatorAnimator.start(); |
| 1761 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1765 | protected void cancelScrollingIndicatorAnimations() { |
| 1766 | if (mScrollIndicatorAnimator != null) { |
| 1767 | mScrollIndicatorAnimator.cancel(); |
| 1768 | } |
| 1769 | } |
| 1770 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1771 | protected void hideScrollingIndicator(boolean immediately) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1772 | if (getChildCount() <= 1) return; |
| 1773 | if (!isScrollingIndicatorEnabled()) return; |
| 1774 | |
| 1775 | getScrollingIndicator(); |
| 1776 | if (mScrollIndicator != null) { |
| 1777 | // Fade the indicator out |
| 1778 | updateScrollingIndicatorPosition(); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1779 | cancelScrollingIndicatorAnimations(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1780 | if (immediately) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1781 | mScrollIndicator.setVisibility(View.INVISIBLE); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1782 | mScrollIndicator.setAlpha(0f); |
| 1783 | } else { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame^] | 1784 | mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1785 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration); |
| 1786 | mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() { |
| 1787 | private boolean cancelled = false; |
| 1788 | @Override |
| 1789 | public void onAnimationCancel(android.animation.Animator animation) { |
| 1790 | cancelled = true; |
| 1791 | } |
| 1792 | @Override |
| 1793 | public void onAnimationEnd(Animator animation) { |
| 1794 | if (!cancelled) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1795 | mScrollIndicator.setVisibility(View.INVISIBLE); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1796 | } |
| 1797 | } |
| 1798 | }); |
| 1799 | mScrollIndicatorAnimator.start(); |
| 1800 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1804 | /** |
| 1805 | * To be overridden by subclasses to determine whether the scroll indicator should stretch to |
| 1806 | * fill its space on the track or not. |
| 1807 | */ |
| 1808 | protected boolean hasElasticScrollIndicator() { |
Winson Chung | dea74b7 | 2011-09-13 18:06:43 -0700 | [diff] [blame] | 1809 | return true; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1810 | } |
| 1811 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1812 | private void updateScrollingIndicator() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1813 | if (getChildCount() <= 1) return; |
| 1814 | if (!isScrollingIndicatorEnabled()) return; |
| 1815 | |
| 1816 | getScrollingIndicator(); |
| 1817 | if (mScrollIndicator != null) { |
| 1818 | updateScrollingIndicatorPosition(); |
| 1819 | } |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1820 | if (mShouldShowScrollIndicator) { |
| 1821 | showScrollingIndicator(mShouldShowScrollIndicatorImmediately); |
| 1822 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1825 | private void updateScrollingIndicatorPosition() { |
Winson Chung | 649723c | 2011-07-06 20:41:23 -0700 | [diff] [blame] | 1826 | if (!isScrollingIndicatorEnabled()) return; |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1827 | if (mScrollIndicator == null) return; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1828 | int numPages = getChildCount(); |
| 1829 | int pageWidth = getMeasuredWidth(); |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1830 | int lastChildIndex = Math.max(0, getChildCount() - 1); |
| 1831 | int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex); |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 1832 | int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1833 | int indicatorWidth = mScrollIndicator.getMeasuredWidth() - |
| 1834 | mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1835 | |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1836 | float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX)); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1837 | int indicatorSpace = trackWidth / numPages; |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1838 | int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1839 | if (hasElasticScrollIndicator()) { |
| 1840 | if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) { |
| 1841 | mScrollIndicator.getLayoutParams().width = indicatorSpace; |
| 1842 | mScrollIndicator.requestLayout(); |
| 1843 | } |
| 1844 | } else { |
| 1845 | int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2; |
| 1846 | indicatorPos += indicatorCenterOffset; |
| 1847 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1848 | mScrollIndicator.setTranslationX(indicatorPos); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1849 | } |
| 1850 | |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1851 | public void showScrollIndicatorTrack() { |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | public void hideScrollIndicatorTrack() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1855 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1856 | |
| 1857 | /* Accessibility */ |
| 1858 | @Override |
| 1859 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1860 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1861 | info.setScrollable(getPageCount() > 1); |
| 1862 | if (getCurrentPage() < getPageCount() - 1) { |
| 1863 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 1864 | } |
| 1865 | if (getCurrentPage() > 0) { |
| 1866 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 1867 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | @Override |
| 1871 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1872 | super.onInitializeAccessibilityEvent(event); |
| 1873 | event.setScrollable(true); |
| 1874 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 1875 | event.setFromIndex(mCurrentPage); |
| 1876 | event.setToIndex(mCurrentPage); |
| 1877 | event.setItemCount(getChildCount()); |
| 1878 | } |
| 1879 | } |
| 1880 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1881 | @Override |
| 1882 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 1883 | if (super.performAccessibilityAction(action, arguments)) { |
| 1884 | return true; |
| 1885 | } |
| 1886 | switch (action) { |
| 1887 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 1888 | if (getCurrentPage() < getPageCount() - 1) { |
| 1889 | scrollRight(); |
| 1890 | return true; |
| 1891 | } |
| 1892 | } break; |
| 1893 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 1894 | if (getCurrentPage() > 0) { |
| 1895 | scrollLeft(); |
| 1896 | return true; |
| 1897 | } |
| 1898 | } break; |
| 1899 | } |
| 1900 | return false; |
| 1901 | } |
| 1902 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1903 | protected String getCurrentPageDescription() { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1904 | return String.format(getContext().getString(R.string.default_scroll_format), |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 1905 | getNextPage() + 1, getChildCount()); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1906 | } |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 1907 | |
| 1908 | @Override |
| 1909 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 1910 | return true; |
| 1911 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1912 | } |