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