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