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