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