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