Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Jim Miller | 5ecd811 | 2013-01-09 18:50:26 -0800 | [diff] [blame] | 17 | package com.android.keyguard; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 18 | |
| 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 21 | import android.animation.AnimatorSet; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 22 | import android.animation.ObjectAnimator; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 23 | import android.animation.TimeInterpolator; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 24 | import android.animation.ValueAnimator; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 25 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Winson Chung | 48275d2 | 2012-11-05 10:56:31 -0800 | [diff] [blame] | 27 | import android.content.res.Resources; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
| 29 | import android.graphics.Canvas; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 30 | import android.graphics.Matrix; |
| 31 | import android.graphics.PointF; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 32 | import android.graphics.Rect; |
| 33 | import android.os.Bundle; |
| 34 | import android.os.Parcel; |
| 35 | import android.os.Parcelable; |
| 36 | import android.util.AttributeSet; |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 37 | import android.util.DisplayMetrics; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | import android.view.InputDevice; |
| 40 | import android.view.KeyEvent; |
| 41 | import android.view.MotionEvent; |
| 42 | import android.view.VelocityTracker; |
| 43 | import android.view.View; |
| 44 | import android.view.ViewConfiguration; |
| 45 | import android.view.ViewGroup; |
| 46 | import android.view.ViewParent; |
| 47 | import android.view.accessibility.AccessibilityEvent; |
| 48 | import android.view.accessibility.AccessibilityManager; |
| 49 | import android.view.accessibility.AccessibilityNodeInfo; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 50 | import android.view.animation.AnimationUtils; |
| 51 | import android.view.animation.DecelerateInterpolator; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 52 | import android.view.animation.Interpolator; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 53 | import android.view.animation.LinearInterpolator; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 54 | import android.widget.Scroller; |
| 55 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 56 | import java.util.ArrayList; |
| 57 | |
| 58 | /** |
| 59 | * An abstraction of the original Workspace which supports browsing through a |
| 60 | * sequential list of "pages" |
| 61 | */ |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 62 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 63 | private static final String TAG = "WidgetPagedView"; |
| 64 | private static final boolean DEBUG = false; |
| 65 | protected static final int INVALID_PAGE = -1; |
| 66 | |
| 67 | // the min drag distance for a fling to register, to prevent random page shifts |
| 68 | private static final int MIN_LENGTH_FOR_FLING = 25; |
| 69 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 70 | protected static final int PAGE_SNAP_ANIMATION_DURATION = 750; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 71 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
| 72 | protected static final float NANOTIME_DIV = 1000000000.0f; |
| 73 | |
| 74 | private static final float OVERSCROLL_ACCELERATE_FACTOR = 2; |
| 75 | private static final float OVERSCROLL_DAMP_FACTOR = 0.14f; |
| 76 | |
| 77 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
| 78 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 79 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
| 80 | |
| 81 | // The following constants need to be scaled based on density. The scaled versions will be |
| 82 | // assigned to the corresponding member variables below. |
| 83 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 84 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 85 | private static final int MIN_FLING_VELOCITY = 250; |
| 86 | |
Jim Miller | d794e64 | 2013-05-22 15:53:24 -0700 | [diff] [blame] | 87 | // We are disabling touch interaction of the widget region for factory ROM. |
Jim Miller | 838906b | 2012-10-19 18:41:25 -0700 | [diff] [blame] | 88 | private static final boolean DISABLE_TOUCH_INTERACTION = false; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 89 | private static final boolean DISABLE_TOUCH_SIDE_PAGES = true; |
| 90 | private static final boolean DISABLE_FLING_TO_DELETE = false; |
Adam Cohen | 258d9fc | 2012-10-13 20:24:26 -0700 | [diff] [blame] | 91 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 92 | static final int AUTOMATIC_PAGE_SPACING = -1; |
| 93 | |
| 94 | protected int mFlingThresholdVelocity; |
| 95 | protected int mMinFlingVelocity; |
| 96 | protected int mMinSnapVelocity; |
| 97 | |
| 98 | protected float mDensity; |
| 99 | protected float mSmoothingTime; |
| 100 | protected float mTouchX; |
| 101 | |
| 102 | protected boolean mFirstLayout = true; |
| 103 | |
| 104 | protected int mCurrentPage; |
Jim Miller | 0ff7f01 | 2012-10-11 20:40:01 -0700 | [diff] [blame] | 105 | protected int mChildCountOnLastMeasure; |
| 106 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 107 | protected int mNextPage = INVALID_PAGE; |
| 108 | protected int mMaxScrollX; |
| 109 | protected Scroller mScroller; |
| 110 | private VelocityTracker mVelocityTracker; |
| 111 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 112 | private float mParentDownMotionX; |
| 113 | private float mParentDownMotionY; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 114 | private float mDownMotionX; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 115 | private float mDownMotionY; |
| 116 | private float mDownScrollX; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 117 | protected float mLastMotionX; |
| 118 | protected float mLastMotionXRemainder; |
| 119 | protected float mLastMotionY; |
| 120 | protected float mTotalMotionX; |
| 121 | private int mLastScreenCenter = -1; |
| 122 | private int[] mChildOffsets; |
| 123 | private int[] mChildRelativeOffsets; |
| 124 | private int[] mChildOffsetsWithLayoutScale; |
| 125 | |
| 126 | protected final static int TOUCH_STATE_REST = 0; |
| 127 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 128 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 129 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 130 | protected final static int TOUCH_STATE_REORDERING = 4; |
| 131 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 132 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
| 133 | |
| 134 | protected int mTouchState = TOUCH_STATE_REST; |
| 135 | protected boolean mForceScreenScrolled = false; |
| 136 | |
| 137 | protected OnLongClickListener mLongClickListener; |
| 138 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 139 | protected int mTouchSlop; |
| 140 | private int mPagingTouchSlop; |
| 141 | private int mMaximumVelocity; |
| 142 | private int mMinimumWidth; |
| 143 | protected int mPageSpacing; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 144 | protected int mCellCountX = 0; |
| 145 | protected int mCellCountY = 0; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 146 | protected boolean mAllowOverScroll = true; |
| 147 | protected int mUnboundedScrollX; |
| 148 | protected int[] mTempVisiblePagesRange = new int[2]; |
| 149 | protected boolean mForceDrawAllChildrenNextFrame; |
| 150 | |
| 151 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. Otherwise |
| 152 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 153 | // the screens from continuing to translate beyond the normal bounds. |
| 154 | protected int mOverScrollX; |
| 155 | |
| 156 | // parameter that adjusts the layout to be optimized for pages with that scale factor |
| 157 | protected float mLayoutScale = 1.0f; |
| 158 | |
| 159 | protected static final int INVALID_POINTER = -1; |
| 160 | |
| 161 | protected int mActivePointerId = INVALID_POINTER; |
| 162 | |
| 163 | private PageSwitchListener mPageSwitchListener; |
| 164 | |
| 165 | protected ArrayList<Boolean> mDirtyPageContent; |
| 166 | |
| 167 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 168 | protected boolean mContentIsRefreshable = true; |
| 169 | |
| 170 | // If true, modify alpha of neighboring pages as user scrolls left/right |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 171 | protected boolean mFadeInAdjacentScreens = false; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 172 | |
| 173 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 174 | // to switch to a new page |
| 175 | protected boolean mUsePagingTouchSlop = true; |
| 176 | |
| 177 | // If true, the subclass should directly update scrollX itself in its computeScroll method |
| 178 | // (SmoothPagedView does this) |
| 179 | protected boolean mDeferScrollUpdate = false; |
| 180 | |
| 181 | protected boolean mIsPageMoving = false; |
| 182 | |
| 183 | // All syncs and layout passes are deferred until data is ready. |
| 184 | protected boolean mIsDataReady = true; |
| 185 | |
| 186 | // Scrolling indicator |
| 187 | private ValueAnimator mScrollIndicatorAnimator; |
| 188 | private View mScrollIndicator; |
| 189 | private int mScrollIndicatorPaddingLeft; |
| 190 | private int mScrollIndicatorPaddingRight; |
| 191 | private boolean mShouldShowScrollIndicator = false; |
| 192 | private boolean mShouldShowScrollIndicatorImmediately = false; |
| 193 | protected static final int sScrollIndicatorFadeInDuration = 150; |
| 194 | protected static final int sScrollIndicatorFadeOutDuration = 650; |
| 195 | protected static final int sScrollIndicatorFlashDuration = 650; |
| 196 | |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 197 | // The viewport whether the pages are to be contained (the actual view may be larger than the |
| 198 | // viewport) |
| 199 | private Rect mViewport = new Rect(); |
| 200 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 201 | // Reordering |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 202 | // We use the min scale to determine how much to expand the actually PagedView measured |
| 203 | // dimensions such that when we are zoomed out, the view is not clipped |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 204 | private int REORDERING_DROP_REPOSITION_DURATION = 200; |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 205 | protected int REORDERING_REORDER_REPOSITION_DURATION = 300; |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 206 | protected int REORDERING_ZOOM_IN_OUT_DURATION = 250; |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 207 | private int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 300; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 208 | private float REORDERING_SIDE_PAGE_BUFFER_PERCENTAGE = 0.1f; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 209 | private long REORDERING_DELETE_DROP_TARGET_FADE_DURATION = 150; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 210 | private float mMinScale = 1f; |
| 211 | protected View mDragView; |
Winson Chung | 48275d2 | 2012-11-05 10:56:31 -0800 | [diff] [blame] | 212 | protected AnimatorSet mZoomInOutAnim; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 213 | private Runnable mSidePageHoverRunnable; |
| 214 | private int mSidePageHoverIndex = -1; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 215 | // This variable's scope is only for the duration of startReordering() and endReordering() |
| 216 | private boolean mReorderingStarted = false; |
| 217 | // This variable's scope is for the duration of startReordering() and after the zoomIn() |
| 218 | // animation after endReordering() |
| 219 | private boolean mIsReordering; |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 220 | // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition |
| 221 | private int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2; |
| 222 | private int mPostReorderingPreZoomInRemainingAnimationCount; |
| 223 | private Runnable mPostReorderingPreZoomInRunnable; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 224 | |
| 225 | // Edge swiping |
| 226 | private boolean mOnlyAllowEdgeSwipes = false; |
| 227 | private boolean mDownEventOnEdge = false; |
| 228 | private int mEdgeSwipeRegionSize = 0; |
| 229 | |
| 230 | // Convenience/caching |
| 231 | private Matrix mTmpInvMatrix = new Matrix(); |
| 232 | private float[] mTmpPoint = new float[2]; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 233 | private Rect mTmpRect = new Rect(); |
Winson Chung | c065a5d | 2012-11-07 17:17:33 -0800 | [diff] [blame] | 234 | private Rect mAltTmpRect = new Rect(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 235 | |
| 236 | // Fling to delete |
| 237 | private int FLING_TO_DELETE_FADE_OUT_DURATION = 350; |
| 238 | private float FLING_TO_DELETE_FRICTION = 0.035f; |
| 239 | // The degrees specifies how much deviation from the up vector to still consider a fling "up" |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 240 | private float FLING_TO_DELETE_MAX_FLING_DEGREES = 65f; |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 241 | protected int mFlingToDeleteThresholdVelocity = -1400; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 242 | // Drag to delete |
| 243 | private boolean mDeferringForDelete = false; |
| 244 | private int DELETE_SLIDE_IN_SIDE_PAGE_DURATION = 250; |
| 245 | private int DRAG_TO_DELETE_FADE_OUT_DURATION = 350; |
| 246 | |
| 247 | // Drop to delete |
| 248 | private View mDeleteDropTarget; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 249 | |
Winson Chung | 48275d2 | 2012-11-05 10:56:31 -0800 | [diff] [blame] | 250 | // Bouncer |
| 251 | private boolean mTopAlignPageWhenShrinkingForBouncer = false; |
| 252 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 253 | public interface PageSwitchListener { |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 254 | void onPageSwitching(View newPage, int newPageIndex); |
| 255 | void onPageSwitched(View newPage, int newPageIndex); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | public PagedView(Context context) { |
| 259 | this(context, null); |
| 260 | } |
| 261 | |
| 262 | public PagedView(Context context, AttributeSet attrs) { |
| 263 | this(context, attrs, 0); |
| 264 | } |
| 265 | |
| 266 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 267 | super(context, attrs, defStyle); |
| 268 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 269 | R.styleable.PagedView, defStyle, 0); |
| 270 | setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0)); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 271 | mScrollIndicatorPaddingLeft = |
| 272 | a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0); |
| 273 | mScrollIndicatorPaddingRight = |
Adam Powell | 0b1b552 | 2012-10-25 13:39:30 -0700 | [diff] [blame] | 274 | a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 275 | a.recycle(); |
| 276 | |
Winson Chung | 48275d2 | 2012-11-05 10:56:31 -0800 | [diff] [blame] | 277 | Resources r = getResources(); |
| 278 | mEdgeSwipeRegionSize = r.getDimensionPixelSize(R.dimen.kg_edge_swipe_region_size); |
| 279 | mTopAlignPageWhenShrinkingForBouncer = |
| 280 | r.getBoolean(R.bool.kg_top_align_page_shrink_on_bouncer_visible); |
Adam Powell | 0b1b552 | 2012-10-25 13:39:30 -0700 | [diff] [blame] | 281 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 282 | setHapticFeedbackEnabled(false); |
| 283 | init(); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Initializes various states for this workspace. |
| 288 | */ |
| 289 | protected void init() { |
| 290 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 291 | mDirtyPageContent.ensureCapacity(32); |
| 292 | mScroller = new Scroller(getContext(), new ScrollInterpolator()); |
| 293 | mCurrentPage = 0; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 294 | |
| 295 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 296 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 297 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 298 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 299 | mDensity = getResources().getDisplayMetrics().density; |
| 300 | |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 301 | // Scale the fling-to-delete threshold by the density |
| 302 | mFlingToDeleteThresholdVelocity = |
| 303 | (int) (mFlingToDeleteThresholdVelocity * mDensity); |
| 304 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 305 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
| 306 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity); |
| 307 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity); |
| 308 | setOnHierarchyChangeListener(this); |
| 309 | } |
| 310 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 311 | void setDeleteDropTarget(View v) { |
| 312 | mDeleteDropTarget = v; |
| 313 | } |
| 314 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 315 | // Convenience methods to map points from self to parent and vice versa |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 316 | float[] mapPointFromViewToParent(View v, float x, float y) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 317 | mTmpPoint[0] = x; |
| 318 | mTmpPoint[1] = y; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 319 | v.getMatrix().mapPoints(mTmpPoint); |
| 320 | mTmpPoint[0] += v.getLeft(); |
| 321 | mTmpPoint[1] += v.getTop(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 322 | return mTmpPoint; |
| 323 | } |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 324 | float[] mapPointFromParentToView(View v, float x, float y) { |
| 325 | mTmpPoint[0] = x - v.getLeft(); |
| 326 | mTmpPoint[1] = y - v.getTop(); |
| 327 | v.getMatrix().invert(mTmpInvMatrix); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 328 | mTmpInvMatrix.mapPoints(mTmpPoint); |
| 329 | return mTmpPoint; |
| 330 | } |
| 331 | |
| 332 | void updateDragViewTranslationDuringDrag() { |
| 333 | float x = mLastMotionX - mDownMotionX + getScrollX() - mDownScrollX; |
| 334 | float y = mLastMotionY - mDownMotionY; |
| 335 | mDragView.setTranslationX(x); |
| 336 | mDragView.setTranslationY(y); |
| 337 | |
| 338 | if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): " + x + ", " + y); |
| 339 | } |
| 340 | |
| 341 | public void setMinScale(float f) { |
| 342 | mMinScale = f; |
| 343 | requestLayout(); |
| 344 | } |
| 345 | |
| 346 | @Override |
| 347 | public void setScaleX(float scaleX) { |
| 348 | super.setScaleX(scaleX); |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 349 | if (isReordering(true)) { |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 350 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 351 | mLastMotionX = p[0]; |
| 352 | mLastMotionY = p[1]; |
| 353 | updateDragViewTranslationDuringDrag(); |
| 354 | } |
| 355 | } |
| 356 | |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 357 | // Convenience methods to get the actual width/height of the PagedView (since it is measured |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 358 | // to be larger to account for the minimum possible scale) |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 359 | int getViewportWidth() { |
| 360 | return mViewport.width(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 361 | } |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 362 | int getViewportHeight() { |
| 363 | return mViewport.height(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 366 | // Convenience methods to get the offset ASSUMING that we are centering the pages in the |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 367 | // PagedView both horizontally and vertically |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 368 | int getViewportOffsetX() { |
| 369 | return (getMeasuredWidth() - getViewportWidth()) / 2; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 370 | } |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 371 | int getViewportOffsetY() { |
| 372 | return (getMeasuredHeight() - getViewportHeight()) / 2; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 375 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 376 | mPageSwitchListener = pageSwitchListener; |
| 377 | if (mPageSwitchListener != null) { |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 378 | mPageSwitchListener.onPageSwitched(getPageAt(mCurrentPage), mCurrentPage); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Called by subclasses to mark that data is ready, and that we can begin loading and laying |
| 384 | * out pages. |
| 385 | */ |
| 386 | protected void setDataIsReady() { |
| 387 | mIsDataReady = true; |
| 388 | } |
| 389 | |
| 390 | protected boolean isDataReady() { |
| 391 | return mIsDataReady; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Returns the index of the currently displayed page. |
| 396 | * |
| 397 | * @return The index of the currently displayed page. |
| 398 | */ |
| 399 | int getCurrentPage() { |
| 400 | return mCurrentPage; |
| 401 | } |
| 402 | |
| 403 | int getNextPage() { |
| 404 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 405 | } |
| 406 | |
| 407 | int getPageCount() { |
| 408 | return getChildCount(); |
| 409 | } |
| 410 | |
| 411 | View getPageAt(int index) { |
| 412 | return getChildAt(index); |
| 413 | } |
| 414 | |
| 415 | protected int indexToPage(int index) { |
| 416 | return index; |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 421 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 422 | * the previous tab page. |
| 423 | */ |
| 424 | protected void updateCurrentPageScroll() { |
| 425 | int offset = getChildOffset(mCurrentPage); |
| 426 | int relOffset = getRelativeChildOffset(mCurrentPage); |
| 427 | int newX = offset - relOffset; |
| 428 | scrollTo(newX, 0); |
| 429 | mScroller.setFinalX(newX); |
| 430 | mScroller.forceFinished(true); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Sets the current page. |
| 435 | */ |
| 436 | void setCurrentPage(int currentPage) { |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 437 | notifyPageSwitching(currentPage); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 438 | if (!mScroller.isFinished()) { |
| 439 | mScroller.abortAnimation(); |
| 440 | } |
| 441 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 442 | // the default |
| 443 | if (getChildCount() == 0) { |
| 444 | return; |
| 445 | } |
| 446 | |
Adam Cohen | 7e39410 | 2012-10-13 19:10:56 -0700 | [diff] [blame] | 447 | mForceScreenScrolled = true; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 448 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
| 449 | updateCurrentPageScroll(); |
| 450 | updateScrollingIndicator(); |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 451 | notifyPageSwitched(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 452 | invalidate(); |
| 453 | } |
| 454 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 455 | public void setOnlyAllowEdgeSwipes(boolean enable) { |
| 456 | mOnlyAllowEdgeSwipes = enable; |
| 457 | } |
| 458 | |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 459 | protected void notifyPageSwitching(int whichPage) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 460 | if (mPageSwitchListener != null) { |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 461 | mPageSwitchListener.onPageSwitching(getPageAt(whichPage), whichPage); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | protected void notifyPageSwitched() { |
| 466 | if (mPageSwitchListener != null) { |
| 467 | mPageSwitchListener.onPageSwitched(getPageAt(mCurrentPage), mCurrentPage); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
| 471 | protected void pageBeginMoving() { |
| 472 | if (!mIsPageMoving) { |
| 473 | mIsPageMoving = true; |
| 474 | onPageBeginMoving(); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | protected void pageEndMoving() { |
| 479 | if (mIsPageMoving) { |
| 480 | mIsPageMoving = false; |
| 481 | onPageEndMoving(); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | protected boolean isPageMoving() { |
| 486 | return mIsPageMoving; |
| 487 | } |
| 488 | |
| 489 | // a method that subclasses can override to add behavior |
| 490 | protected void onPageBeginMoving() { |
| 491 | } |
| 492 | |
| 493 | // a method that subclasses can override to add behavior |
| 494 | protected void onPageEndMoving() { |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Registers the specified listener on each page contained in this workspace. |
| 499 | * |
| 500 | * @param l The listener used to respond to long clicks. |
| 501 | */ |
| 502 | @Override |
| 503 | public void setOnLongClickListener(OnLongClickListener l) { |
| 504 | mLongClickListener = l; |
| 505 | final int count = getPageCount(); |
| 506 | for (int i = 0; i < count; i++) { |
| 507 | getPageAt(i).setOnLongClickListener(l); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public void scrollBy(int x, int y) { |
| 513 | scrollTo(mUnboundedScrollX + x, getScrollY() + y); |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void scrollTo(int x, int y) { |
| 518 | mUnboundedScrollX = x; |
| 519 | |
| 520 | if (x < 0) { |
| 521 | super.scrollTo(0, y); |
| 522 | if (mAllowOverScroll) { |
| 523 | overScroll(x); |
| 524 | } |
| 525 | } else if (x > mMaxScrollX) { |
| 526 | super.scrollTo(mMaxScrollX, y); |
| 527 | if (mAllowOverScroll) { |
| 528 | overScroll(x - mMaxScrollX); |
| 529 | } |
| 530 | } else { |
| 531 | mOverScrollX = x; |
| 532 | super.scrollTo(x, y); |
| 533 | } |
| 534 | |
| 535 | mTouchX = x; |
| 536 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 537 | |
| 538 | // Update the last motion events when scrolling |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 539 | if (isReordering(true)) { |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 540 | float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 541 | mLastMotionX = p[0]; |
| 542 | mLastMotionY = p[1]; |
| 543 | updateDragViewTranslationDuringDrag(); |
| 544 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 548 | protected boolean computeScrollHelper() { |
| 549 | if (mScroller.computeScrollOffset()) { |
| 550 | // Don't bother scrolling if the page does not need to be moved |
| 551 | if (getScrollX() != mScroller.getCurrX() |
| 552 | || getScrollY() != mScroller.getCurrY() |
| 553 | || mOverScrollX != mScroller.getCurrX()) { |
| 554 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
| 555 | } |
| 556 | invalidate(); |
| 557 | return true; |
| 558 | } else if (mNextPage != INVALID_PAGE) { |
| 559 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
| 560 | mNextPage = INVALID_PAGE; |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 561 | notifyPageSwitched(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 562 | |
| 563 | // We don't want to trigger a page end moving unless the page has settled |
| 564 | // and the user has stopped scrolling |
| 565 | if (mTouchState == TOUCH_STATE_REST) { |
| 566 | pageEndMoving(); |
| 567 | } |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 568 | |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 569 | onPostReorderingAnimationCompleted(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 570 | return true; |
| 571 | } |
| 572 | return false; |
| 573 | } |
| 574 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 575 | @Override |
| 576 | public void computeScroll() { |
| 577 | computeScrollHelper(); |
| 578 | } |
| 579 | |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 580 | protected boolean shouldSetTopAlignedPivotForWidget(int childIndex) { |
| 581 | return mTopAlignPageWhenShrinkingForBouncer; |
| 582 | } |
| 583 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 584 | @Override |
| 585 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 586 | if (!mIsDataReady || getChildCount() == 0) { |
| 587 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 588 | return; |
| 589 | } |
| 590 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 591 | // We measure the dimensions of the PagedView to be larger than the pages so that when we |
| 592 | // zoom out (and scale down), the view is still contained in the parent |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 593 | View parent = (View) getParent(); |
| 594 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 595 | int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 596 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 597 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 598 | // NOTE: We multiply by 1.5f to account for the fact that depending on the offset of the |
| 599 | // viewport, we can be at most one and a half screens offset once we scale down |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 600 | DisplayMetrics dm = getResources().getDisplayMetrics(); |
| 601 | int maxSize = Math.max(dm.widthPixels, dm.heightPixels); |
| 602 | int parentWidthSize = (int) (1.5f * maxSize); |
| 603 | int parentHeightSize = maxSize; |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 604 | int scaledWidthSize = (int) (parentWidthSize / mMinScale); |
| 605 | int scaledHeightSize = (int) (parentHeightSize / mMinScale); |
| 606 | mViewport.set(0, 0, widthSize, heightSize); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 607 | |
| 608 | if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) { |
| 609 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | // Return early if we aren't given a proper dimension |
| 614 | if (widthSize <= 0 || heightSize <= 0) { |
| 615 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 620 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 621 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 622 | * each page to have the same width. |
| 623 | */ |
| 624 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 625 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
| 626 | |
| 627 | // The children are given the same width and height as the workspace |
| 628 | // unless they were set to WRAP_CONTENT |
| 629 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 630 | if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize); |
| 631 | if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 632 | if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 633 | if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 634 | final int childCount = getChildCount(); |
| 635 | for (int i = 0; i < childCount; i++) { |
| 636 | // disallowing padding in paged view (just pass 0) |
| 637 | final View child = getPageAt(i); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 638 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 639 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 640 | int childWidthMode; |
| 641 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 642 | childWidthMode = MeasureSpec.AT_MOST; |
| 643 | } else { |
| 644 | childWidthMode = MeasureSpec.EXACTLY; |
| 645 | } |
| 646 | |
| 647 | int childHeightMode; |
| 648 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 649 | childHeightMode = MeasureSpec.AT_MOST; |
| 650 | } else { |
| 651 | childHeightMode = MeasureSpec.EXACTLY; |
| 652 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 653 | |
| 654 | final int childWidthMeasureSpec = |
| 655 | MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode); |
| 656 | final int childHeightMeasureSpec = |
| 657 | MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode); |
| 658 | |
| 659 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 660 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 661 | setMeasuredDimension(scaledWidthSize, scaledHeightSize); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 662 | |
| 663 | // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions. |
| 664 | // We also wait until we set the measured dimensions before flushing the cache as well, to |
| 665 | // ensure that the cache is filled with good values. |
| 666 | invalidateCachedOffsets(); |
| 667 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 668 | if (mChildCountOnLastMeasure != getChildCount() && !mDeferringForDelete) { |
Jim Miller | 0ff7f01 | 2012-10-11 20:40:01 -0700 | [diff] [blame] | 669 | setCurrentPage(mCurrentPage); |
| 670 | } |
| 671 | mChildCountOnLastMeasure = getChildCount(); |
| 672 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 673 | if (childCount > 0) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 674 | if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getViewportWidth() + ", " |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 675 | + getChildWidth(0)); |
| 676 | |
| 677 | // Calculate the variable page spacing if necessary |
| 678 | if (mPageSpacing == AUTOMATIC_PAGE_SPACING) { |
| 679 | // The gap between pages in the PagedView should be equal to the gap from the page |
| 680 | // to the edge of the screen (so it is not visible in the current screen). To |
| 681 | // account for unequal padding on each side of the paged view, we take the maximum |
| 682 | // of the left/right gap and use that as the gap between each page. |
| 683 | int offset = getRelativeChildOffset(0); |
| 684 | int spacing = Math.max(offset, widthSize - offset - |
| 685 | getChildAt(0).getMeasuredWidth()); |
| 686 | setPageSpacing(spacing); |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | updateScrollingIndicatorPosition(); |
| 691 | |
| 692 | if (childCount > 0) { |
| 693 | mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1); |
| 694 | } else { |
| 695 | mMaxScrollX = 0; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | public void setPageSpacing(int pageSpacing) { |
| 700 | mPageSpacing = pageSpacing; |
| 701 | invalidateCachedOffsets(); |
| 702 | } |
| 703 | |
| 704 | @Override |
| 705 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 706 | if (!mIsDataReady || getChildCount() == 0) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 711 | final int childCount = getChildCount(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 712 | |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 713 | int offsetX = getViewportOffsetX(); |
| 714 | int offsetY = getViewportOffsetY(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 715 | |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 716 | // Update the viewport offsets |
| 717 | mViewport.offset(offsetX, offsetY); |
| 718 | |
| 719 | int childLeft = offsetX + getRelativeChildOffset(0); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 720 | for (int i = 0; i < childCount; i++) { |
| 721 | final View child = getPageAt(i); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 722 | int childTop = offsetY + getPaddingTop(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 723 | if (child.getVisibility() != View.GONE) { |
| 724 | final int childWidth = getScaledMeasuredWidth(child); |
| 725 | final int childHeight = child.getMeasuredHeight(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 726 | |
| 727 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
| 728 | child.layout(childLeft, childTop, |
| 729 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
| 730 | childLeft += childWidth + mPageSpacing; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 735 | setHorizontalScrollBarEnabled(false); |
| 736 | updateCurrentPageScroll(); |
| 737 | setHorizontalScrollBarEnabled(true); |
| 738 | mFirstLayout = false; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | protected void screenScrolled(int screenCenter) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | @Override |
| 746 | public void onChildViewAdded(View parent, View child) { |
| 747 | // This ensures that when children are added, they get the correct transforms / alphas |
| 748 | // in accordance with any scroll effects. |
| 749 | mForceScreenScrolled = true; |
| 750 | invalidate(); |
| 751 | invalidateCachedOffsets(); |
| 752 | } |
| 753 | |
| 754 | @Override |
| 755 | public void onChildViewRemoved(View parent, View child) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 756 | mForceScreenScrolled = true; |
Jim Miller | e5fb5e4 | 2013-04-10 16:10:06 -0700 | [diff] [blame] | 757 | invalidate(); |
| 758 | invalidateCachedOffsets(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | protected void invalidateCachedOffsets() { |
| 762 | int count = getChildCount(); |
| 763 | if (count == 0) { |
| 764 | mChildOffsets = null; |
| 765 | mChildRelativeOffsets = null; |
| 766 | mChildOffsetsWithLayoutScale = null; |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | mChildOffsets = new int[count]; |
| 771 | mChildRelativeOffsets = new int[count]; |
| 772 | mChildOffsetsWithLayoutScale = new int[count]; |
| 773 | for (int i = 0; i < count; i++) { |
| 774 | mChildOffsets[i] = -1; |
| 775 | mChildRelativeOffsets[i] = -1; |
| 776 | mChildOffsetsWithLayoutScale[i] = -1; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | protected int getChildOffset(int index) { |
Jim Miller | 0ff7f01 | 2012-10-11 20:40:01 -0700 | [diff] [blame] | 781 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 782 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 783 | int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ? |
| 784 | mChildOffsets : mChildOffsetsWithLayoutScale; |
| 785 | |
| 786 | if (childOffsets != null && childOffsets[index] != -1) { |
| 787 | return childOffsets[index]; |
| 788 | } else { |
| 789 | if (getChildCount() == 0) |
| 790 | return 0; |
| 791 | |
| 792 | int offset = getRelativeChildOffset(0); |
| 793 | for (int i = 0; i < index; ++i) { |
| 794 | offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing; |
| 795 | } |
| 796 | if (childOffsets != null) { |
| 797 | childOffsets[index] = offset; |
| 798 | } |
| 799 | return offset; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | protected int getRelativeChildOffset(int index) { |
Jim Miller | 0ff7f01 | 2012-10-11 20:40:01 -0700 | [diff] [blame] | 804 | if (index < 0 || index > getChildCount() - 1) return 0; |
| 805 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 806 | if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) { |
| 807 | return mChildRelativeOffsets[index]; |
| 808 | } else { |
| 809 | final int padding = getPaddingLeft() + getPaddingRight(); |
| 810 | final int offset = getPaddingLeft() + |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 811 | (getViewportWidth() - padding - getChildWidth(index)) / 2; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 812 | if (mChildRelativeOffsets != null) { |
| 813 | mChildRelativeOffsets[index] = offset; |
| 814 | } |
| 815 | return offset; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | protected int getScaledMeasuredWidth(View child) { |
| 820 | // This functions are called enough times that it actually makes a difference in the |
| 821 | // profiler -- so just inline the max() here |
| 822 | final int measuredWidth = child.getMeasuredWidth(); |
| 823 | final int minWidth = mMinimumWidth; |
| 824 | final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth; |
| 825 | return (int) (maxWidth * mLayoutScale + 0.5f); |
| 826 | } |
| 827 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 828 | void boundByReorderablePages(boolean isReordering, int[] range) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 829 | // Do nothing |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 832 | // TODO: Fix this |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 833 | protected void getVisiblePages(int[] range) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 834 | range[0] = 0; |
| 835 | range[1] = getPageCount() - 1; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 836 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 837 | /* |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 838 | final int pageCount = getChildCount(); |
| 839 | |
| 840 | if (pageCount > 0) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 841 | final int screenWidth = getViewportWidth(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 842 | int leftScreen = 0; |
| 843 | int rightScreen = 0; |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 844 | int offsetX = getViewportOffsetX() + getScrollX(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 845 | View currPage = getPageAt(leftScreen); |
| 846 | while (leftScreen < pageCount - 1 && |
| 847 | currPage.getX() + currPage.getWidth() - |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 848 | currPage.getPaddingRight() < offsetX) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 849 | leftScreen++; |
| 850 | currPage = getPageAt(leftScreen); |
| 851 | } |
| 852 | rightScreen = leftScreen; |
| 853 | currPage = getPageAt(rightScreen + 1); |
| 854 | while (rightScreen < pageCount - 1 && |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 855 | currPage.getX() - currPage.getPaddingLeft() < offsetX + screenWidth) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 856 | rightScreen++; |
| 857 | currPage = getPageAt(rightScreen + 1); |
| 858 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 859 | |
| 860 | // TEMP: this is a hacky way to ensure that animations to new pages are not clipped |
| 861 | // because we don't draw them while scrolling? |
| 862 | range[0] = Math.max(0, leftScreen - 1); |
| 863 | range[1] = Math.min(rightScreen + 1, getChildCount() - 1); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 864 | } else { |
| 865 | range[0] = -1; |
| 866 | range[1] = -1; |
| 867 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 868 | */ |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | protected boolean shouldDrawChild(View child) { |
| 872 | return child.getAlpha() > 0; |
| 873 | } |
| 874 | |
| 875 | @Override |
| 876 | protected void dispatchDraw(Canvas canvas) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 877 | int halfScreenSize = getViewportWidth() / 2; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 878 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. |
| 879 | // Otherwise it is equal to the scaled overscroll position. |
| 880 | int screenCenter = mOverScrollX + halfScreenSize; |
| 881 | |
| 882 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
| 883 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 884 | // set it for the next frame |
| 885 | mForceScreenScrolled = false; |
| 886 | screenScrolled(screenCenter); |
| 887 | mLastScreenCenter = screenCenter; |
| 888 | } |
| 889 | |
| 890 | // Find out which screens are visible; as an optimization we only call draw on them |
| 891 | final int pageCount = getChildCount(); |
| 892 | if (pageCount > 0) { |
| 893 | getVisiblePages(mTempVisiblePagesRange); |
| 894 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 895 | final int rightScreen = mTempVisiblePagesRange[1]; |
| 896 | if (leftScreen != -1 && rightScreen != -1) { |
| 897 | final long drawingTime = getDrawingTime(); |
| 898 | // Clip to the bounds |
| 899 | canvas.save(); |
| 900 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 901 | getScrollY() + getBottom() - getTop()); |
| 902 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 903 | // Draw all the children, leaving the drag view for last |
| 904 | for (int i = pageCount - 1; i >= 0; i--) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 905 | final View v = getPageAt(i); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 906 | if (v == mDragView) continue; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 907 | if (mForceDrawAllChildrenNextFrame || |
| 908 | (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) { |
| 909 | drawChild(canvas, v, drawingTime); |
| 910 | } |
| 911 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 912 | // Draw the drag view on top (if there is one) |
| 913 | if (mDragView != null) { |
| 914 | drawChild(canvas, mDragView, drawingTime); |
| 915 | } |
| 916 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 917 | mForceDrawAllChildrenNextFrame = false; |
| 918 | canvas.restore(); |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 925 | int page = indexToPage(indexOfChild(child)); |
| 926 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 927 | snapToPage(page); |
| 928 | return true; |
| 929 | } |
| 930 | return false; |
| 931 | } |
| 932 | |
| 933 | @Override |
| 934 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
| 935 | int focusablePage; |
| 936 | if (mNextPage != INVALID_PAGE) { |
| 937 | focusablePage = mNextPage; |
| 938 | } else { |
| 939 | focusablePage = mCurrentPage; |
| 940 | } |
| 941 | View v = getPageAt(focusablePage); |
| 942 | if (v != null) { |
| 943 | return v.requestFocus(direction, previouslyFocusedRect); |
| 944 | } |
| 945 | return false; |
| 946 | } |
| 947 | |
| 948 | @Override |
| 949 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 950 | if (direction == View.FOCUS_LEFT) { |
| 951 | if (getCurrentPage() > 0) { |
| 952 | snapToPage(getCurrentPage() - 1); |
| 953 | return true; |
| 954 | } |
| 955 | } else if (direction == View.FOCUS_RIGHT) { |
| 956 | if (getCurrentPage() < getPageCount() - 1) { |
| 957 | snapToPage(getCurrentPage() + 1); |
| 958 | return true; |
| 959 | } |
| 960 | } |
| 961 | return super.dispatchUnhandledMove(focused, direction); |
| 962 | } |
| 963 | |
| 964 | @Override |
| 965 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
| 966 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
| 967 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
| 968 | } |
| 969 | if (direction == View.FOCUS_LEFT) { |
| 970 | if (mCurrentPage > 0) { |
| 971 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
| 972 | } |
| 973 | } else if (direction == View.FOCUS_RIGHT){ |
| 974 | if (mCurrentPage < getPageCount() - 1) { |
| 975 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * If one of our descendant views decides that it could be focused now, only |
| 982 | * pass that along if it's on the current page. |
| 983 | * |
| 984 | * This happens when live folders requery, and if they're off page, they |
| 985 | * end up calling requestFocus, which pulls it on page. |
| 986 | */ |
| 987 | @Override |
| 988 | public void focusableViewAvailable(View focused) { |
| 989 | View current = getPageAt(mCurrentPage); |
| 990 | View v = focused; |
| 991 | while (true) { |
| 992 | if (v == current) { |
| 993 | super.focusableViewAvailable(focused); |
| 994 | return; |
| 995 | } |
| 996 | if (v == this) { |
| 997 | return; |
| 998 | } |
| 999 | ViewParent parent = v.getParent(); |
| 1000 | if (parent instanceof View) { |
| 1001 | v = (View)v.getParent(); |
| 1002 | } else { |
| 1003 | return; |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | /** |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1009 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 1010 | */ |
| 1011 | protected boolean hitsPreviousPage(float x, float y) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1012 | return (x < getViewportOffsetX() + getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | /** |
| 1016 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 1017 | */ |
| 1018 | protected boolean hitsNextPage(float x, float y) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1019 | return (x > (getViewportOffsetX() + getViewportWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Winson Chung | 1272e0e | 2012-11-26 17:05:37 -0800 | [diff] [blame] | 1022 | /** Returns whether x and y originated within the buffered viewport */ |
| 1023 | private boolean isTouchPointInViewportWithBuffer(int x, int y) { |
| 1024 | mTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top, |
| 1025 | mViewport.right + mViewport.width() / 2, mViewport.bottom); |
| 1026 | return mTmpRect.contains(x, y); |
| 1027 | } |
| 1028 | |
| 1029 | /** Returns whether x and y originated within the current page view bounds */ |
| 1030 | private boolean isTouchPointInCurrentPage(int x, int y) { |
| 1031 | View v = getPageAt(getCurrentPage()); |
| 1032 | if (v != null) { |
| 1033 | mTmpRect.set((v.getLeft() - getScrollX()), 0, (v.getRight() - getScrollX()), |
| 1034 | v.getBottom()); |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1035 | return mTmpRect.contains(x, y); |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1036 | } |
Winson Chung | 1272e0e | 2012-11-26 17:05:37 -0800 | [diff] [blame] | 1037 | return false; |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1040 | @Override |
| 1041 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Adam Cohen | 258d9fc | 2012-10-13 20:24:26 -0700 | [diff] [blame] | 1042 | if (DISABLE_TOUCH_INTERACTION) { |
| 1043 | return false; |
| 1044 | } |
| 1045 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1046 | /* |
| 1047 | * This method JUST determines whether we want to intercept the motion. |
| 1048 | * If we return true, onTouchEvent will be called and we do the actual |
| 1049 | * scrolling there. |
| 1050 | */ |
| 1051 | acquireVelocityTrackerAndAddMovement(ev); |
| 1052 | |
| 1053 | // Skip touch handling if there are no pages to swipe |
| 1054 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1055 | |
| 1056 | /* |
| 1057 | * Shortcut the most recurring case: the user is in the dragging |
| 1058 | * state and he is moving his finger. We want to intercept this |
| 1059 | * motion. |
| 1060 | */ |
| 1061 | final int action = ev.getAction(); |
| 1062 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1063 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
| 1067 | switch (action & MotionEvent.ACTION_MASK) { |
| 1068 | case MotionEvent.ACTION_MOVE: { |
| 1069 | /* |
| 1070 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1071 | * whether the user has moved far enough from his original down touch. |
| 1072 | */ |
| 1073 | if (mActivePointerId != INVALID_POINTER) { |
| 1074 | determineScrollingStart(ev); |
| 1075 | break; |
| 1076 | } |
| 1077 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1078 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1079 | // i.e. fall through to the next case (don't break) |
| 1080 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1081 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
| 1082 | } |
| 1083 | |
| 1084 | case MotionEvent.ACTION_DOWN: { |
| 1085 | final float x = ev.getX(); |
| 1086 | final float y = ev.getY(); |
| 1087 | // Remember location of down touch |
| 1088 | mDownMotionX = x; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1089 | mDownMotionY = y; |
| 1090 | mDownScrollX = getScrollX(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1091 | mLastMotionX = x; |
| 1092 | mLastMotionY = y; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1093 | float[] p = mapPointFromViewToParent(this, x, y); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1094 | mParentDownMotionX = p[0]; |
| 1095 | mParentDownMotionY = p[1]; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1096 | mLastMotionXRemainder = 0; |
| 1097 | mTotalMotionX = 0; |
| 1098 | mActivePointerId = ev.getPointerId(0); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1099 | |
| 1100 | // Determine if the down event is within the threshold to be an edge swipe |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1101 | int leftEdgeBoundary = getViewportOffsetX() + mEdgeSwipeRegionSize; |
| 1102 | int rightEdgeBoundary = getMeasuredWidth() - getViewportOffsetX() - mEdgeSwipeRegionSize; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1103 | if ((mDownMotionX <= leftEdgeBoundary || mDownMotionX >= rightEdgeBoundary)) { |
| 1104 | mDownEventOnEdge = true; |
| 1105 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1106 | |
| 1107 | /* |
| 1108 | * If being flinged and user touches the screen, initiate drag; |
| 1109 | * otherwise don't. mScroller.isFinished should be false when |
| 1110 | * being flinged. |
| 1111 | */ |
| 1112 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
| 1113 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 1114 | if (finishedScrolling) { |
| 1115 | mTouchState = TOUCH_STATE_REST; |
| 1116 | mScroller.abortAnimation(); |
| 1117 | } else { |
Winson Chung | 1272e0e | 2012-11-26 17:05:37 -0800 | [diff] [blame] | 1118 | if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) { |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1119 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1120 | } else { |
| 1121 | mTouchState = TOUCH_STATE_REST; |
| 1122 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | // check if this can be the beginning of a tap on the side of the pages |
| 1126 | // to scroll the current page |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1127 | if (!DISABLE_TOUCH_SIDE_PAGES) { |
| 1128 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
| 1129 | if (getChildCount() > 0) { |
| 1130 | if (hitsPreviousPage(x, y)) { |
| 1131 | mTouchState = TOUCH_STATE_PREV_PAGE; |
| 1132 | } else if (hitsNextPage(x, y)) { |
| 1133 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 1134 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | break; |
| 1139 | } |
| 1140 | |
| 1141 | case MotionEvent.ACTION_UP: |
| 1142 | case MotionEvent.ACTION_CANCEL: |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1143 | resetTouchState(); |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1144 | // Just intercept the touch event on up if we tap outside the strict viewport |
Winson Chung | 1272e0e | 2012-11-26 17:05:37 -0800 | [diff] [blame] | 1145 | if (!isTouchPointInCurrentPage((int) mLastMotionX, (int) mLastMotionY)) { |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1146 | return true; |
| 1147 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1148 | break; |
| 1149 | |
| 1150 | case MotionEvent.ACTION_POINTER_UP: |
| 1151 | onSecondaryPointerUp(ev); |
| 1152 | releaseVelocityTracker(); |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * The only time we want to intercept motion events is if we are in the |
| 1158 | * drag mode. |
| 1159 | */ |
| 1160 | return mTouchState != TOUCH_STATE_REST; |
| 1161 | } |
| 1162 | |
| 1163 | protected void determineScrollingStart(MotionEvent ev) { |
| 1164 | determineScrollingStart(ev, 1.0f); |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * Determines if we should change the touch state to start scrolling after the |
| 1169 | * user moves their touch point too far. |
| 1170 | */ |
| 1171 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1172 | // Disallow scrolling if we don't have a valid pointer index |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1173 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1174 | if (pointerIndex == -1) return; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1175 | |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1176 | // Disallow scrolling if we started the gesture from outside the viewport |
| 1177 | final float x = ev.getX(pointerIndex); |
| 1178 | final float y = ev.getY(pointerIndex); |
Winson Chung | 1272e0e | 2012-11-26 17:05:37 -0800 | [diff] [blame] | 1179 | if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1180 | |
| 1181 | // If we're only allowing edge swipes, we break out early if the down event wasn't |
| 1182 | // at the edge. |
Winson Chung | 6cf53bb | 2012-11-05 17:55:42 -0800 | [diff] [blame] | 1183 | if (mOnlyAllowEdgeSwipes && !mDownEventOnEdge) return; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1184 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1185 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 1186 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 1187 | |
| 1188 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
| 1189 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 1190 | boolean xMoved = xDiff > touchSlop; |
| 1191 | boolean yMoved = yDiff > touchSlop; |
| 1192 | |
| 1193 | if (xMoved || xPaged || yMoved) { |
| 1194 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
| 1195 | // Scroll if the user moved far enough along the X axis |
| 1196 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1197 | mTotalMotionX += Math.abs(mLastMotionX - x); |
| 1198 | mLastMotionX = x; |
| 1199 | mLastMotionXRemainder = 0; |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1200 | mTouchX = getViewportOffsetX() + getScrollX(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1201 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1202 | pageBeginMoving(); |
| 1203 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
Adam Cohen | 9ec871d | 2012-10-24 19:25:44 -0700 | [diff] [blame] | 1207 | protected float getMaxScrollProgress() { |
| 1208 | return 1.0f; |
| 1209 | } |
| 1210 | |
Adam Cohen | f9048cd | 2012-10-27 16:36:10 -0700 | [diff] [blame] | 1211 | protected float getBoundedScrollProgress(int screenCenter, View v, int page) { |
| 1212 | final int halfScreenSize = getViewportWidth() / 2; |
| 1213 | |
| 1214 | screenCenter = Math.min(mScrollX + halfScreenSize, screenCenter); |
| 1215 | screenCenter = Math.max(halfScreenSize, screenCenter); |
| 1216 | |
| 1217 | return getScrollProgress(screenCenter, v, page); |
| 1218 | } |
| 1219 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1220 | protected float getScrollProgress(int screenCenter, View v, int page) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1221 | final int halfScreenSize = getViewportWidth() / 2; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1222 | |
| 1223 | int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing; |
| 1224 | int delta = screenCenter - (getChildOffset(page) - |
| 1225 | getRelativeChildOffset(page) + halfScreenSize); |
| 1226 | |
| 1227 | float scrollProgress = delta / (totalDistance * 1.0f); |
Adam Cohen | 9ec871d | 2012-10-24 19:25:44 -0700 | [diff] [blame] | 1228 | scrollProgress = Math.min(scrollProgress, getMaxScrollProgress()); |
| 1229 | scrollProgress = Math.max(scrollProgress, - getMaxScrollProgress()); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1230 | return scrollProgress; |
| 1231 | } |
| 1232 | |
| 1233 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 1234 | // as the user pulls further and further from the bounds |
| 1235 | private float overScrollInfluenceCurve(float f) { |
| 1236 | f -= 1.0f; |
| 1237 | return f * f * f + 1.0f; |
| 1238 | } |
| 1239 | |
| 1240 | protected void acceleratedOverScroll(float amount) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1241 | int screenSize = getViewportWidth(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1242 | |
| 1243 | // We want to reach the max over scroll effect when the user has |
| 1244 | // over scrolled half the size of the screen |
| 1245 | float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize); |
| 1246 | |
| 1247 | if (f == 0) return; |
| 1248 | |
| 1249 | // Clamp this factor, f, to -1 < f < 1 |
| 1250 | if (Math.abs(f) >= 1) { |
| 1251 | f /= Math.abs(f); |
| 1252 | } |
| 1253 | |
| 1254 | int overScrollAmount = (int) Math.round(f * screenSize); |
| 1255 | if (amount < 0) { |
| 1256 | mOverScrollX = overScrollAmount; |
| 1257 | super.scrollTo(0, getScrollY()); |
| 1258 | } else { |
| 1259 | mOverScrollX = mMaxScrollX + overScrollAmount; |
| 1260 | super.scrollTo(mMaxScrollX, getScrollY()); |
| 1261 | } |
| 1262 | invalidate(); |
| 1263 | } |
| 1264 | |
| 1265 | protected void dampedOverScroll(float amount) { |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1266 | int screenSize = getViewportWidth(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1267 | |
| 1268 | float f = (amount / screenSize); |
| 1269 | |
| 1270 | if (f == 0) return; |
| 1271 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1272 | |
| 1273 | // Clamp this factor, f, to -1 < f < 1 |
| 1274 | if (Math.abs(f) >= 1) { |
| 1275 | f /= Math.abs(f); |
| 1276 | } |
| 1277 | |
| 1278 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
| 1279 | if (amount < 0) { |
| 1280 | mOverScrollX = overScrollAmount; |
| 1281 | super.scrollTo(0, getScrollY()); |
| 1282 | } else { |
| 1283 | mOverScrollX = mMaxScrollX + overScrollAmount; |
| 1284 | super.scrollTo(mMaxScrollX, getScrollY()); |
| 1285 | } |
| 1286 | invalidate(); |
| 1287 | } |
| 1288 | |
| 1289 | protected void overScroll(float amount) { |
| 1290 | dampedOverScroll(amount); |
| 1291 | } |
| 1292 | |
| 1293 | protected float maxOverScroll() { |
| 1294 | // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not |
| 1295 | // exceed). Used to find out how much extra wallpaper we need for the over scroll effect |
| 1296 | float f = 1.0f; |
| 1297 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1298 | return OVERSCROLL_DAMP_FACTOR * f; |
| 1299 | } |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1300 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1301 | @Override |
| 1302 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | 258d9fc | 2012-10-13 20:24:26 -0700 | [diff] [blame] | 1303 | if (DISABLE_TOUCH_INTERACTION) { |
| 1304 | return false; |
| 1305 | } |
| 1306 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1307 | // Skip touch handling if there are no pages to swipe |
| 1308 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1309 | |
| 1310 | acquireVelocityTrackerAndAddMovement(ev); |
| 1311 | |
| 1312 | final int action = ev.getAction(); |
| 1313 | |
| 1314 | switch (action & MotionEvent.ACTION_MASK) { |
| 1315 | case MotionEvent.ACTION_DOWN: |
| 1316 | /* |
| 1317 | * If being flinged and user touches, stop the fling. isFinished |
| 1318 | * will be false if being flinged. |
| 1319 | */ |
| 1320 | if (!mScroller.isFinished()) { |
| 1321 | mScroller.abortAnimation(); |
| 1322 | } |
| 1323 | |
| 1324 | // Remember where the motion event started |
| 1325 | mDownMotionX = mLastMotionX = ev.getX(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1326 | mDownMotionY = mLastMotionY = ev.getY(); |
| 1327 | mDownScrollX = getScrollX(); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1328 | float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1329 | mParentDownMotionX = p[0]; |
| 1330 | mParentDownMotionY = p[1]; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1331 | mLastMotionXRemainder = 0; |
| 1332 | mTotalMotionX = 0; |
| 1333 | mActivePointerId = ev.getPointerId(0); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1334 | |
| 1335 | // Determine if the down event is within the threshold to be an edge swipe |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1336 | int leftEdgeBoundary = getViewportOffsetX() + mEdgeSwipeRegionSize; |
| 1337 | int rightEdgeBoundary = getMeasuredWidth() - getViewportOffsetX() - mEdgeSwipeRegionSize; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1338 | if ((mDownMotionX <= leftEdgeBoundary || mDownMotionX >= rightEdgeBoundary)) { |
| 1339 | mDownEventOnEdge = true; |
| 1340 | } |
| 1341 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1342 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1343 | pageBeginMoving(); |
| 1344 | } |
| 1345 | break; |
| 1346 | |
| 1347 | case MotionEvent.ACTION_MOVE: |
| 1348 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1349 | // Scroll to follow the motion event |
| 1350 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Jim Miller | d794e64 | 2013-05-22 15:53:24 -0700 | [diff] [blame] | 1351 | |
| 1352 | if (pointerIndex == -1) return true; |
| 1353 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1354 | final float x = ev.getX(pointerIndex); |
| 1355 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
| 1356 | |
| 1357 | mTotalMotionX += Math.abs(deltaX); |
| 1358 | |
| 1359 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1360 | // keep the remainder because we are actually testing if we've moved from the last |
| 1361 | // scrolled position (which is discrete). |
| 1362 | if (Math.abs(deltaX) >= 1.0f) { |
| 1363 | mTouchX += deltaX; |
| 1364 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1365 | if (!mDeferScrollUpdate) { |
| 1366 | scrollBy((int) deltaX, 0); |
| 1367 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
| 1368 | } else { |
| 1369 | invalidate(); |
| 1370 | } |
| 1371 | mLastMotionX = x; |
| 1372 | mLastMotionXRemainder = deltaX - (int) deltaX; |
| 1373 | } else { |
| 1374 | awakenScrollBars(); |
| 1375 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1376 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
| 1377 | // Update the last motion position |
| 1378 | mLastMotionX = ev.getX(); |
| 1379 | mLastMotionY = ev.getY(); |
| 1380 | |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1381 | // Update the parent down so that our zoom animations take this new movement into |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1382 | // account |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1383 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1384 | mParentDownMotionX = pt[0]; |
| 1385 | mParentDownMotionY = pt[1]; |
| 1386 | updateDragViewTranslationDuringDrag(); |
| 1387 | |
| 1388 | // Find the closest page to the touch point |
| 1389 | final int dragViewIndex = indexOfChild(mDragView); |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1390 | int bufferSize = (int) (REORDERING_SIDE_PAGE_BUFFER_PERCENTAGE * |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1391 | getViewportWidth()); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1392 | int leftBufferEdge = (int) (mapPointFromViewToParent(this, mViewport.left, 0)[0] |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1393 | + bufferSize); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1394 | int rightBufferEdge = (int) (mapPointFromViewToParent(this, mViewport.right, 0)[0] |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1395 | - bufferSize); |
| 1396 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1397 | // Change the drag view if we are hovering over the drop target |
| 1398 | boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget( |
| 1399 | (int) mParentDownMotionX, (int) mParentDownMotionY); |
| 1400 | setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete); |
| 1401 | |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1402 | if (DEBUG) Log.d(TAG, "leftBufferEdge: " + leftBufferEdge); |
| 1403 | if (DEBUG) Log.d(TAG, "rightBufferEdge: " + rightBufferEdge); |
| 1404 | if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX); |
| 1405 | if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY); |
| 1406 | if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX); |
| 1407 | if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY); |
| 1408 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1409 | float parentX = mParentDownMotionX; |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1410 | int pageIndexToSnapTo = -1; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1411 | if (parentX < leftBufferEdge && dragViewIndex > 0) { |
| 1412 | pageIndexToSnapTo = dragViewIndex - 1; |
| 1413 | } else if (parentX > rightBufferEdge && dragViewIndex < getChildCount() - 1) { |
| 1414 | pageIndexToSnapTo = dragViewIndex + 1; |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1415 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1416 | |
| 1417 | final int pageUnderPointIndex = pageIndexToSnapTo; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1418 | if (pageUnderPointIndex > -1 && !isHoveringOverDelete) { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1419 | mTempVisiblePagesRange[0] = 0; |
| 1420 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
| 1421 | boundByReorderablePages(true, mTempVisiblePagesRange); |
| 1422 | if (mTempVisiblePagesRange[0] <= pageUnderPointIndex && |
| 1423 | pageUnderPointIndex <= mTempVisiblePagesRange[1] && |
| 1424 | pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1425 | mSidePageHoverIndex = pageUnderPointIndex; |
| 1426 | mSidePageHoverRunnable = new Runnable() { |
| 1427 | @Override |
| 1428 | public void run() { |
| 1429 | // Update the down scroll position to account for the fact that the |
| 1430 | // current page is moved |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1431 | mDownScrollX = getChildOffset(pageUnderPointIndex) |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1432 | - getRelativeChildOffset(pageUnderPointIndex); |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1433 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1434 | // Setup the scroll to the correct page before we swap the views |
| 1435 | snapToPage(pageUnderPointIndex); |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1436 | |
| 1437 | // For each of the pages between the paged view and the drag view, |
| 1438 | // animate them from the previous position to the new position in |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1439 | // the layout (as a result of the drag view moving in the layout) |
| 1440 | int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1; |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1441 | int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1442 | dragViewIndex + 1 : pageUnderPointIndex; |
| 1443 | int upperIndex = (dragViewIndex > pageUnderPointIndex) ? |
| 1444 | dragViewIndex - 1 : pageUnderPointIndex; |
| 1445 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 1446 | View v = getChildAt(i); |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1447 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 1448 | // drag view all subsequent views to pageUnderPointIndex will |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1449 | // shift down. |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1450 | int oldX = getViewportOffsetX() + getChildOffset(i); |
| 1451 | int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1452 | |
| 1453 | // Animate the view translation from its old position to its new |
| 1454 | // position |
| 1455 | AnimatorSet anim = (AnimatorSet) v.getTag(); |
| 1456 | if (anim != null) { |
| 1457 | anim.cancel(); |
| 1458 | } |
| 1459 | |
| 1460 | v.setTranslationX(oldX - newX); |
| 1461 | anim = new AnimatorSet(); |
| 1462 | anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION); |
| 1463 | anim.playTogether( |
| 1464 | ObjectAnimator.ofFloat(v, "translationX", 0f)); |
| 1465 | anim.start(); |
| 1466 | v.setTag(anim); |
| 1467 | } |
| 1468 | |
| 1469 | removeView(mDragView); |
Michael Jurka | 75b5cfb | 2012-11-15 18:22:47 -0800 | [diff] [blame] | 1470 | onRemoveView(mDragView, false); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1471 | addView(mDragView, pageUnderPointIndex); |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1472 | onAddView(mDragView, pageUnderPointIndex); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1473 | mSidePageHoverIndex = -1; |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1474 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1475 | }; |
| 1476 | postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT); |
| 1477 | } |
| 1478 | } else { |
| 1479 | removeCallbacks(mSidePageHoverRunnable); |
| 1480 | mSidePageHoverIndex = -1; |
| 1481 | } |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1482 | } else { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1483 | determineScrollingStart(ev); |
| 1484 | } |
| 1485 | break; |
| 1486 | |
| 1487 | case MotionEvent.ACTION_UP: |
| 1488 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1489 | final int activePointerId = mActivePointerId; |
| 1490 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1491 | final float x = ev.getX(pointerIndex); |
| 1492 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1493 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1494 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
| 1495 | final int deltaX = (int) (x - mDownMotionX); |
| 1496 | final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage)); |
| 1497 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1498 | SIGNIFICANT_MOVE_THRESHOLD; |
| 1499 | |
| 1500 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1501 | |
| 1502 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
| 1503 | Math.abs(velocityX) > mFlingThresholdVelocity; |
| 1504 | |
| 1505 | // In the case that the page is moved far to one direction and then is flung |
| 1506 | // in the opposite direction, we use a threshold to determine whether we should |
| 1507 | // just return to the starting page, or if we should skip one further. |
| 1508 | boolean returnToOriginalPage = false; |
| 1509 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
| 1510 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
| 1511 | returnToOriginalPage = true; |
| 1512 | } |
| 1513 | |
| 1514 | int finalPage; |
| 1515 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1516 | // test for a large move if a fling has been registered. That is, a large |
| 1517 | // move to the left and fling to the right will register as a fling to the right. |
| 1518 | if (((isSignificantMove && deltaX > 0 && !isFling) || |
| 1519 | (isFling && velocityX > 0)) && mCurrentPage > 0) { |
| 1520 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1521 | snapToPageWithVelocity(finalPage, velocityX); |
| 1522 | } else if (((isSignificantMove && deltaX < 0 && !isFling) || |
| 1523 | (isFling && velocityX < 0)) && |
| 1524 | mCurrentPage < getChildCount() - 1) { |
| 1525 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1526 | snapToPageWithVelocity(finalPage, velocityX); |
| 1527 | } else { |
| 1528 | snapToDestination(); |
| 1529 | } |
| 1530 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
| 1531 | // at this point we have not moved beyond the touch slop |
| 1532 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1533 | // we can just page |
| 1534 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1535 | if (nextPage != mCurrentPage) { |
| 1536 | snapToPage(nextPage); |
| 1537 | } else { |
| 1538 | snapToDestination(); |
| 1539 | } |
| 1540 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
| 1541 | // at this point we have not moved beyond the touch slop |
| 1542 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1543 | // we can just page |
| 1544 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1545 | if (nextPage != mCurrentPage) { |
| 1546 | snapToPage(nextPage); |
| 1547 | } else { |
| 1548 | snapToDestination(); |
| 1549 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1550 | } else if (mTouchState == TOUCH_STATE_REORDERING) { |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1551 | // Update the last motion position |
| 1552 | mLastMotionX = ev.getX(); |
| 1553 | mLastMotionY = ev.getY(); |
| 1554 | |
| 1555 | // Update the parent down so that our zoom animations take this new movement into |
| 1556 | // account |
| 1557 | float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY); |
| 1558 | mParentDownMotionX = pt[0]; |
| 1559 | mParentDownMotionY = pt[1]; |
| 1560 | updateDragViewTranslationDuringDrag(); |
| 1561 | boolean handledFling = false; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1562 | if (!DISABLE_FLING_TO_DELETE) { |
| 1563 | // Check the velocity and see if we are flinging-to-delete |
| 1564 | PointF flingToDeleteVector = isFlingingToDelete(); |
| 1565 | if (flingToDeleteVector != null) { |
| 1566 | onFlingToDelete(flingToDeleteVector); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1567 | handledFling = true; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1570 | if (!handledFling && isHoveringOverDeleteDropTarget((int) mParentDownMotionX, |
| 1571 | (int) mParentDownMotionY)) { |
| 1572 | onDropToDelete(); |
| 1573 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1574 | } else { |
| 1575 | onUnhandledTap(ev); |
| 1576 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1577 | |
| 1578 | // Remove the callback to wait for the side page hover timeout |
| 1579 | removeCallbacks(mSidePageHoverRunnable); |
| 1580 | // End any intermediate reordering states |
| 1581 | resetTouchState(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1582 | break; |
| 1583 | |
| 1584 | case MotionEvent.ACTION_CANCEL: |
| 1585 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1586 | snapToDestination(); |
| 1587 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1588 | resetTouchState(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1589 | break; |
| 1590 | |
| 1591 | case MotionEvent.ACTION_POINTER_UP: |
| 1592 | onSecondaryPointerUp(ev); |
| 1593 | break; |
| 1594 | } |
| 1595 | |
| 1596 | return true; |
| 1597 | } |
| 1598 | |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1599 | //public abstract void onFlingToDelete(View v); |
Michael Jurka | 75b5cfb | 2012-11-15 18:22:47 -0800 | [diff] [blame] | 1600 | public abstract void onRemoveView(View v, boolean deletePermanently); |
Winson Chung | 4752e7d | 2012-11-20 17:06:04 -0800 | [diff] [blame] | 1601 | public abstract void onRemoveViewAnimationCompleted(); |
Michael Jurka | 1254f2f | 2012-10-25 11:44:31 -0700 | [diff] [blame] | 1602 | public abstract void onAddView(View v, int index); |
| 1603 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1604 | private void resetTouchState() { |
| 1605 | releaseVelocityTracker(); |
| 1606 | endReordering(); |
| 1607 | mTouchState = TOUCH_STATE_REST; |
| 1608 | mActivePointerId = INVALID_POINTER; |
| 1609 | mDownEventOnEdge = false; |
| 1610 | } |
| 1611 | |
| 1612 | protected void onUnhandledTap(MotionEvent ev) {} |
| 1613 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1614 | @Override |
| 1615 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1616 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1617 | switch (event.getAction()) { |
| 1618 | case MotionEvent.ACTION_SCROLL: { |
| 1619 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1620 | final float vscroll; |
| 1621 | final float hscroll; |
| 1622 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1623 | vscroll = 0; |
| 1624 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1625 | } else { |
| 1626 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1627 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1628 | } |
| 1629 | if (hscroll != 0 || vscroll != 0) { |
| 1630 | if (hscroll > 0 || vscroll > 0) { |
| 1631 | scrollRight(); |
| 1632 | } else { |
| 1633 | scrollLeft(); |
| 1634 | } |
| 1635 | return true; |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | } |
| 1640 | return super.onGenericMotionEvent(event); |
| 1641 | } |
| 1642 | |
| 1643 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1644 | if (mVelocityTracker == null) { |
| 1645 | mVelocityTracker = VelocityTracker.obtain(); |
| 1646 | } |
| 1647 | mVelocityTracker.addMovement(ev); |
| 1648 | } |
| 1649 | |
| 1650 | private void releaseVelocityTracker() { |
| 1651 | if (mVelocityTracker != null) { |
| 1652 | mVelocityTracker.recycle(); |
| 1653 | mVelocityTracker = null; |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1658 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1659 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1660 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1661 | if (pointerId == mActivePointerId) { |
| 1662 | // This was our active pointer going up. Choose a new |
| 1663 | // active pointer and adjust accordingly. |
| 1664 | // TODO: Make this decision more intelligent. |
| 1665 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1666 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1667 | mLastMotionY = ev.getY(newPointerIndex); |
| 1668 | mLastMotionXRemainder = 0; |
| 1669 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1670 | if (mVelocityTracker != null) { |
| 1671 | mVelocityTracker.clear(); |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1676 | @Override |
| 1677 | public void requestChildFocus(View child, View focused) { |
| 1678 | super.requestChildFocus(child, focused); |
| 1679 | int page = indexToPage(indexOfChild(child)); |
| 1680 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
| 1681 | snapToPage(page); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 1686 | final int childCount = getChildCount(); |
| 1687 | int left; |
| 1688 | int right; |
| 1689 | for (int i = 0; i < childCount; ++i) { |
| 1690 | left = getRelativeChildOffset(i); |
| 1691 | right = (left + getScaledMeasuredWidth(getPageAt(i))); |
| 1692 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1693 | return i; |
| 1694 | } |
| 1695 | } |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
| 1699 | protected int getChildWidth(int index) { |
| 1700 | // This functions are called enough times that it actually makes a difference in the |
| 1701 | // profiler -- so just inline the max() here |
| 1702 | final int measuredWidth = getPageAt(index).getMeasuredWidth(); |
| 1703 | final int minWidth = mMinimumWidth; |
| 1704 | return (minWidth > measuredWidth) ? minWidth : measuredWidth; |
| 1705 | } |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1706 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1707 | int getPageNearestToPoint(float x) { |
| 1708 | int index = 0; |
| 1709 | for (int i = 0; i < getChildCount(); ++i) { |
| 1710 | if (x < getChildAt(i).getRight() - getScrollX()) { |
| 1711 | return index; |
| 1712 | } else { |
| 1713 | index++; |
| 1714 | } |
| 1715 | } |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 1716 | return Math.min(index, getChildCount() - 1); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 1717 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1718 | |
| 1719 | int getPageNearestToCenterOfScreen() { |
| 1720 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
| 1721 | int minDistanceFromScreenCenterIndex = -1; |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1722 | int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1723 | final int childCount = getChildCount(); |
| 1724 | for (int i = 0; i < childCount; ++i) { |
| 1725 | View layout = (View) getPageAt(i); |
| 1726 | int childWidth = getScaledMeasuredWidth(layout); |
| 1727 | int halfChildWidth = (childWidth / 2); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1728 | int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1729 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1730 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1731 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1732 | minDistanceFromScreenCenterIndex = i; |
| 1733 | } |
| 1734 | } |
| 1735 | return minDistanceFromScreenCenterIndex; |
| 1736 | } |
| 1737 | |
| 1738 | protected void snapToDestination() { |
| 1739 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
| 1740 | } |
| 1741 | |
| 1742 | private static class ScrollInterpolator implements Interpolator { |
| 1743 | public ScrollInterpolator() { |
| 1744 | } |
| 1745 | |
| 1746 | public float getInterpolation(float t) { |
| 1747 | t -= 1.0f; |
| 1748 | return t*t*t*t*t + 1; |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1753 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1754 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1755 | // of travel has on the overall snap duration. |
| 1756 | float distanceInfluenceForSnapDuration(float f) { |
| 1757 | f -= 0.5f; // center the values about 0. |
| 1758 | f *= 0.3f * Math.PI / 2.0f; |
| 1759 | return (float) Math.sin(f); |
| 1760 | } |
| 1761 | |
| 1762 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
| 1763 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1764 | int halfScreenSize = getViewportWidth() / 2; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1765 | |
| 1766 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1767 | if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): " |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1768 | + getViewportWidth() + ", " + getChildWidth(whichPage)); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1769 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1770 | int delta = newX - mUnboundedScrollX; |
| 1771 | int duration = 0; |
| 1772 | |
| 1773 | if (Math.abs(velocity) < mMinFlingVelocity) { |
| 1774 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1775 | // as opposed to an apparent physical response to flinging |
| 1776 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1777 | return; |
| 1778 | } |
| 1779 | |
| 1780 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1781 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1782 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1783 | // duration as a function of the distance the page needs to travel. |
| 1784 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
| 1785 | float distance = halfScreenSize + halfScreenSize * |
| 1786 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1787 | |
| 1788 | velocity = Math.abs(velocity); |
| 1789 | velocity = Math.max(mMinSnapVelocity, velocity); |
| 1790 | |
| 1791 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1792 | // user flings, so we scale the duration by a value near to the derivative of the scroll |
| 1793 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1794 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
| 1795 | |
| 1796 | snapToPage(whichPage, delta, duration); |
| 1797 | } |
| 1798 | |
| 1799 | protected void snapToPage(int whichPage) { |
| 1800 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1801 | } |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1802 | protected void snapToPageImmediately(int whichPage) { |
| 1803 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true); |
| 1804 | } |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1805 | |
| 1806 | protected void snapToPage(int whichPage, int duration) { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1807 | snapToPage(whichPage, duration, false); |
| 1808 | } |
| 1809 | protected void snapToPage(int whichPage, int duration, boolean immediate) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1810 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
| 1811 | |
| 1812 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 1813 | if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getViewportWidth() + ", " |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1814 | + getChildWidth(whichPage)); |
| 1815 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1816 | final int sX = mUnboundedScrollX; |
| 1817 | final int delta = newX - sX; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1818 | snapToPage(whichPage, delta, duration, immediate); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | protected void snapToPage(int whichPage, int delta, int duration) { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1822 | snapToPage(whichPage, delta, duration, false); |
| 1823 | } |
| 1824 | protected void snapToPage(int whichPage, int delta, int duration, boolean immediate) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1825 | mNextPage = whichPage; |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 1826 | notifyPageSwitching(whichPage); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1827 | View focusedChild = getFocusedChild(); |
Svetoslav Ganov | 45942ca | 2012-10-31 19:46:24 -0700 | [diff] [blame] | 1828 | if (focusedChild != null && whichPage != mCurrentPage && |
| 1829 | focusedChild == getPageAt(mCurrentPage)) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1830 | focusedChild.clearFocus(); |
| 1831 | } |
| 1832 | |
| 1833 | pageBeginMoving(); |
| 1834 | awakenScrollBars(duration); |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1835 | if (immediate) { |
| 1836 | duration = 0; |
| 1837 | } else if (duration == 0) { |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1838 | duration = Math.abs(delta); |
| 1839 | } |
| 1840 | |
| 1841 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
| 1842 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
| 1843 | |
John Spurlock | bb5c941 | 2012-10-31 09:46:15 -0400 | [diff] [blame] | 1844 | notifyPageSwitched(); |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 1845 | |
| 1846 | // Trigger a compute() to finish switching pages if necessary |
| 1847 | if (immediate) { |
| 1848 | computeScroll(); |
| 1849 | } |
| 1850 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 1851 | mForceScreenScrolled = true; |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1852 | invalidate(); |
| 1853 | } |
| 1854 | |
| 1855 | public void scrollLeft() { |
| 1856 | if (mScroller.isFinished()) { |
| 1857 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
| 1858 | } else { |
| 1859 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | public void scrollRight() { |
| 1864 | if (mScroller.isFinished()) { |
| 1865 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
| 1866 | } else { |
| 1867 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
| 1868 | } |
| 1869 | } |
| 1870 | |
| 1871 | public int getPageForView(View v) { |
| 1872 | int result = -1; |
| 1873 | if (v != null) { |
| 1874 | ViewParent vp = v.getParent(); |
| 1875 | int count = getChildCount(); |
| 1876 | for (int i = 0; i < count; i++) { |
| 1877 | if (vp == getPageAt(i)) { |
| 1878 | return i; |
| 1879 | } |
| 1880 | } |
| 1881 | } |
| 1882 | return result; |
| 1883 | } |
| 1884 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 1885 | public static class SavedState extends BaseSavedState { |
| 1886 | int currentPage = -1; |
| 1887 | |
| 1888 | SavedState(Parcelable superState) { |
| 1889 | super(superState); |
| 1890 | } |
| 1891 | |
| 1892 | private SavedState(Parcel in) { |
| 1893 | super(in); |
| 1894 | currentPage = in.readInt(); |
| 1895 | } |
| 1896 | |
| 1897 | @Override |
| 1898 | public void writeToParcel(Parcel out, int flags) { |
| 1899 | super.writeToParcel(out, flags); |
| 1900 | out.writeInt(currentPage); |
| 1901 | } |
| 1902 | |
| 1903 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1904 | new Parcelable.Creator<SavedState>() { |
| 1905 | public SavedState createFromParcel(Parcel in) { |
| 1906 | return new SavedState(in); |
| 1907 | } |
| 1908 | |
| 1909 | public SavedState[] newArray(int size) { |
| 1910 | return new SavedState[size]; |
| 1911 | } |
| 1912 | }; |
| 1913 | } |
| 1914 | |
| 1915 | protected View getScrollingIndicator() { |
| 1916 | return null; |
| 1917 | } |
| 1918 | |
| 1919 | protected boolean isScrollingIndicatorEnabled() { |
| 1920 | return false; |
| 1921 | } |
| 1922 | |
| 1923 | Runnable hideScrollingIndicatorRunnable = new Runnable() { |
| 1924 | @Override |
| 1925 | public void run() { |
| 1926 | hideScrollingIndicator(false); |
| 1927 | } |
| 1928 | }; |
| 1929 | |
| 1930 | protected void flashScrollingIndicator(boolean animated) { |
| 1931 | removeCallbacks(hideScrollingIndicatorRunnable); |
| 1932 | showScrollingIndicator(!animated); |
| 1933 | postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration); |
| 1934 | } |
| 1935 | |
| 1936 | protected void showScrollingIndicator(boolean immediately) { |
| 1937 | mShouldShowScrollIndicator = true; |
| 1938 | mShouldShowScrollIndicatorImmediately = true; |
| 1939 | if (getChildCount() <= 1) return; |
| 1940 | if (!isScrollingIndicatorEnabled()) return; |
| 1941 | |
| 1942 | mShouldShowScrollIndicator = false; |
| 1943 | getScrollingIndicator(); |
| 1944 | if (mScrollIndicator != null) { |
| 1945 | // Fade the indicator in |
| 1946 | updateScrollingIndicatorPosition(); |
| 1947 | mScrollIndicator.setVisibility(View.VISIBLE); |
| 1948 | cancelScrollingIndicatorAnimations(); |
| 1949 | if (immediately) { |
| 1950 | mScrollIndicator.setAlpha(1f); |
| 1951 | } else { |
| 1952 | mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 1f); |
| 1953 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration); |
| 1954 | mScrollIndicatorAnimator.start(); |
| 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | protected void cancelScrollingIndicatorAnimations() { |
| 1960 | if (mScrollIndicatorAnimator != null) { |
| 1961 | mScrollIndicatorAnimator.cancel(); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | protected void hideScrollingIndicator(boolean immediately) { |
| 1966 | if (getChildCount() <= 1) return; |
| 1967 | if (!isScrollingIndicatorEnabled()) return; |
| 1968 | |
| 1969 | getScrollingIndicator(); |
| 1970 | if (mScrollIndicator != null) { |
| 1971 | // Fade the indicator out |
| 1972 | updateScrollingIndicatorPosition(); |
| 1973 | cancelScrollingIndicatorAnimations(); |
| 1974 | if (immediately) { |
| 1975 | mScrollIndicator.setVisibility(View.INVISIBLE); |
| 1976 | mScrollIndicator.setAlpha(0f); |
| 1977 | } else { |
| 1978 | mScrollIndicatorAnimator = ObjectAnimator.ofFloat(mScrollIndicator, "alpha", 0f); |
| 1979 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration); |
| 1980 | mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() { |
| 1981 | private boolean cancelled = false; |
| 1982 | @Override |
| 1983 | public void onAnimationCancel(android.animation.Animator animation) { |
| 1984 | cancelled = true; |
| 1985 | } |
| 1986 | @Override |
| 1987 | public void onAnimationEnd(Animator animation) { |
| 1988 | if (!cancelled) { |
| 1989 | mScrollIndicator.setVisibility(View.INVISIBLE); |
| 1990 | } |
| 1991 | } |
| 1992 | }); |
| 1993 | mScrollIndicatorAnimator.start(); |
| 1994 | } |
| 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | /** |
| 1999 | * To be overridden by subclasses to determine whether the scroll indicator should stretch to |
| 2000 | * fill its space on the track or not. |
| 2001 | */ |
| 2002 | protected boolean hasElasticScrollIndicator() { |
| 2003 | return true; |
| 2004 | } |
| 2005 | |
| 2006 | private void updateScrollingIndicator() { |
| 2007 | if (getChildCount() <= 1) return; |
| 2008 | if (!isScrollingIndicatorEnabled()) return; |
| 2009 | |
| 2010 | getScrollingIndicator(); |
| 2011 | if (mScrollIndicator != null) { |
| 2012 | updateScrollingIndicatorPosition(); |
| 2013 | } |
| 2014 | if (mShouldShowScrollIndicator) { |
| 2015 | showScrollingIndicator(mShouldShowScrollIndicatorImmediately); |
| 2016 | } |
| 2017 | } |
| 2018 | |
| 2019 | private void updateScrollingIndicatorPosition() { |
| 2020 | if (!isScrollingIndicatorEnabled()) return; |
| 2021 | if (mScrollIndicator == null) return; |
| 2022 | int numPages = getChildCount(); |
Winson Chung | efc4925 | 2012-10-26 15:41:27 -0700 | [diff] [blame] | 2023 | int pageWidth = getViewportWidth(); |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 2024 | int lastChildIndex = Math.max(0, getChildCount() - 1); |
| 2025 | int maxScrollX = getChildOffset(lastChildIndex) - getRelativeChildOffset(lastChildIndex); |
| 2026 | int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight; |
| 2027 | int indicatorWidth = mScrollIndicator.getMeasuredWidth() - |
| 2028 | mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight(); |
| 2029 | |
| 2030 | float offset = Math.max(0f, Math.min(1f, (float) getScrollX() / maxScrollX)); |
| 2031 | int indicatorSpace = trackWidth / numPages; |
| 2032 | int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft; |
| 2033 | if (hasElasticScrollIndicator()) { |
| 2034 | if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) { |
| 2035 | mScrollIndicator.getLayoutParams().width = indicatorSpace; |
| 2036 | mScrollIndicator.requestLayout(); |
| 2037 | } |
| 2038 | } else { |
| 2039 | int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2; |
| 2040 | indicatorPos += indicatorCenterOffset; |
| 2041 | } |
| 2042 | mScrollIndicator.setTranslationX(indicatorPos); |
| 2043 | } |
| 2044 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2045 | // Animate the drag view back to the original position |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 2046 | void animateDragViewToOriginalPosition() { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2047 | if (mDragView != null) { |
| 2048 | AnimatorSet anim = new AnimatorSet(); |
| 2049 | anim.setDuration(REORDERING_DROP_REPOSITION_DURATION); |
| 2050 | anim.playTogether( |
| 2051 | ObjectAnimator.ofFloat(mDragView, "translationX", 0f), |
| 2052 | ObjectAnimator.ofFloat(mDragView, "translationY", 0f)); |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 2053 | anim.addListener(new AnimatorListenerAdapter() { |
| 2054 | @Override |
| 2055 | public void onAnimationEnd(Animator animation) { |
| 2056 | onPostReorderingAnimationCompleted(); |
| 2057 | } |
| 2058 | }); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2059 | anim.start(); |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | // "Zooms out" the PagedView to reveal more side pages |
Adam Cohen | 70009e4 | 2012-10-30 16:48:22 -0700 | [diff] [blame] | 2064 | protected boolean zoomOut() { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2065 | if (mZoomInOutAnim != null && mZoomInOutAnim.isRunning()) { |
| 2066 | mZoomInOutAnim.cancel(); |
| 2067 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2068 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2069 | if (!(getScaleX() < 1f || getScaleY() < 1f)) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2070 | mZoomInOutAnim = new AnimatorSet(); |
| 2071 | mZoomInOutAnim.setDuration(REORDERING_ZOOM_IN_OUT_DURATION); |
| 2072 | mZoomInOutAnim.playTogether( |
| 2073 | ObjectAnimator.ofFloat(this, "scaleX", mMinScale), |
| 2074 | ObjectAnimator.ofFloat(this, "scaleY", mMinScale)); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2075 | mZoomInOutAnim.addListener(new AnimatorListenerAdapter() { |
| 2076 | @Override |
| 2077 | public void onAnimationStart(Animator animation) { |
| 2078 | // Show the delete drop target |
| 2079 | if (mDeleteDropTarget != null) { |
| 2080 | mDeleteDropTarget.setVisibility(View.VISIBLE); |
| 2081 | mDeleteDropTarget.animate().alpha(1f) |
| 2082 | .setDuration(REORDERING_DELETE_DROP_TARGET_FADE_DURATION) |
| 2083 | .setListener(new AnimatorListenerAdapter() { |
| 2084 | @Override |
| 2085 | public void onAnimationStart(Animator animation) { |
| 2086 | mDeleteDropTarget.setAlpha(0f); |
| 2087 | } |
| 2088 | }); |
| 2089 | } |
| 2090 | } |
| 2091 | }); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2092 | mZoomInOutAnim.start(); |
| 2093 | return true; |
| 2094 | } |
| 2095 | return false; |
| 2096 | } |
| 2097 | |
| 2098 | protected void onStartReordering() { |
Svetoslav Ganov | c4842c1 | 2012-10-31 14:33:32 -0700 | [diff] [blame] | 2099 | if (AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 2100 | announceForAccessibility(mContext.getString( |
| 2101 | R.string.keyguard_accessibility_widget_reorder_start)); |
| 2102 | } |
| 2103 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2104 | // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.) |
| 2105 | mTouchState = TOUCH_STATE_REORDERING; |
| 2106 | mIsReordering = true; |
| 2107 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2108 | // Mark all the non-widget pages as invisible |
| 2109 | getVisiblePages(mTempVisiblePagesRange); |
| 2110 | boundByReorderablePages(true, mTempVisiblePagesRange); |
| 2111 | for (int i = 0; i < getPageCount(); ++i) { |
| 2112 | if (i < mTempVisiblePagesRange[0] || i > mTempVisiblePagesRange[1]) { |
| 2113 | getPageAt(i).setAlpha(0f); |
| 2114 | } |
| 2115 | } |
| 2116 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2117 | // We must invalidate to trigger a redraw to update the layers such that the drag view |
| 2118 | // is always drawn on top |
| 2119 | invalidate(); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2120 | } |
| 2121 | |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 2122 | private void onPostReorderingAnimationCompleted() { |
| 2123 | // Trigger the callback when reordering has settled |
| 2124 | --mPostReorderingPreZoomInRemainingAnimationCount; |
| 2125 | if (mPostReorderingPreZoomInRunnable != null && |
| 2126 | mPostReorderingPreZoomInRemainingAnimationCount == 0) { |
| 2127 | mPostReorderingPreZoomInRunnable.run(); |
| 2128 | mPostReorderingPreZoomInRunnable = null; |
| 2129 | } |
| 2130 | } |
| 2131 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2132 | protected void onEndReordering() { |
Svetoslav Ganov | c4842c1 | 2012-10-31 14:33:32 -0700 | [diff] [blame] | 2133 | if (AccessibilityManager.getInstance(mContext).isEnabled()) { |
| 2134 | announceForAccessibility(mContext.getString( |
| 2135 | R.string.keyguard_accessibility_widget_reorder_end)); |
| 2136 | } |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2137 | mIsReordering = false; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2138 | |
| 2139 | // Mark all the non-widget pages as visible again |
| 2140 | getVisiblePages(mTempVisiblePagesRange); |
| 2141 | boundByReorderablePages(true, mTempVisiblePagesRange); |
| 2142 | for (int i = 0; i < getPageCount(); ++i) { |
| 2143 | if (i < mTempVisiblePagesRange[0] || i > mTempVisiblePagesRange[1]) { |
| 2144 | getPageAt(i).setAlpha(1f); |
| 2145 | } |
| 2146 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2147 | } |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 2148 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2149 | public boolean startReordering() { |
| 2150 | int dragViewIndex = getPageNearestToCenterOfScreen(); |
| 2151 | mTempVisiblePagesRange[0] = 0; |
| 2152 | mTempVisiblePagesRange[1] = getPageCount() - 1; |
| 2153 | boundByReorderablePages(true, mTempVisiblePagesRange); |
| 2154 | mReorderingStarted = true; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2155 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2156 | // Check if we are within the reordering range |
| 2157 | if (mTempVisiblePagesRange[0] <= dragViewIndex && |
| 2158 | dragViewIndex <= mTempVisiblePagesRange[1]) { |
| 2159 | if (zoomOut()) { |
| 2160 | // Find the drag view under the pointer |
| 2161 | mDragView = getChildAt(dragViewIndex); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2162 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2163 | onStartReordering(); |
| 2164 | } |
| 2165 | return true; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2166 | } |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2167 | return false; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2170 | boolean isReordering(boolean testTouchState) { |
| 2171 | boolean state = mIsReordering; |
| 2172 | if (testTouchState) { |
| 2173 | state &= (mTouchState == TOUCH_STATE_REORDERING); |
| 2174 | } |
| 2175 | return state; |
| 2176 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2177 | void endReordering() { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2178 | // For simplicity, we call endReordering sometimes even if reordering was never started. |
| 2179 | // In that case, we don't want to do anything. |
| 2180 | if (!mReorderingStarted) return; |
| 2181 | mReorderingStarted = false; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2182 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2183 | // If we haven't flung-to-delete the current child, then we just animate the drag view |
| 2184 | // back into position |
Winson Chung | 70aa528 | 2012-10-30 10:56:37 -0700 | [diff] [blame] | 2185 | final Runnable onCompleteRunnable = new Runnable() { |
| 2186 | @Override |
| 2187 | public void run() { |
| 2188 | onEndReordering(); |
| 2189 | } |
| 2190 | }; |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2191 | if (!mDeferringForDelete) { |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 2192 | mPostReorderingPreZoomInRunnable = new Runnable() { |
| 2193 | public void run() { |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 2194 | zoomIn(onCompleteRunnable); |
| 2195 | }; |
| 2196 | }; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2197 | |
Winson Chung | 9dc9923 | 2012-10-29 17:43:18 -0700 | [diff] [blame] | 2198 | mPostReorderingPreZoomInRemainingAnimationCount = |
| 2199 | NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT; |
| 2200 | // Snap to the current page |
| 2201 | snapToPage(indexOfChild(mDragView), 0); |
| 2202 | // Animate the drag view back to the front position |
| 2203 | animateDragViewToOriginalPosition(); |
Winson Chung | 70aa528 | 2012-10-30 10:56:37 -0700 | [diff] [blame] | 2204 | } else { |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2205 | // Handled in post-delete-animation-callbacks |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2209 | // "Zooms in" the PagedView to highlight the current page |
Adam Cohen | 70009e4 | 2012-10-30 16:48:22 -0700 | [diff] [blame] | 2210 | protected boolean zoomIn(final Runnable onCompleteRunnable) { |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2211 | if (mZoomInOutAnim != null && mZoomInOutAnim.isRunning()) { |
| 2212 | mZoomInOutAnim.cancel(); |
| 2213 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2214 | if (getScaleX() < 1f || getScaleY() < 1f) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2215 | mZoomInOutAnim = new AnimatorSet(); |
| 2216 | mZoomInOutAnim.setDuration(REORDERING_ZOOM_IN_OUT_DURATION); |
| 2217 | mZoomInOutAnim.playTogether( |
| 2218 | ObjectAnimator.ofFloat(this, "scaleX", 1f), |
| 2219 | ObjectAnimator.ofFloat(this, "scaleY", 1f)); |
| 2220 | mZoomInOutAnim.addListener(new AnimatorListenerAdapter() { |
| 2221 | @Override |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2222 | public void onAnimationStart(Animator animation) { |
| 2223 | // Hide the delete drop target |
| 2224 | if (mDeleteDropTarget != null) { |
| 2225 | mDeleteDropTarget.animate().alpha(0f) |
| 2226 | .setDuration(REORDERING_DELETE_DROP_TARGET_FADE_DURATION) |
| 2227 | .setListener(new AnimatorListenerAdapter() { |
| 2228 | @Override |
| 2229 | public void onAnimationEnd(Animator animation) { |
| 2230 | mDeleteDropTarget.setVisibility(View.GONE); |
| 2231 | } |
| 2232 | }); |
| 2233 | } |
| 2234 | } |
| 2235 | @Override |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2236 | public void onAnimationCancel(Animator animation) { |
| 2237 | mDragView = null; |
| 2238 | } |
| 2239 | @Override |
| 2240 | public void onAnimationEnd(Animator animation) { |
| 2241 | mDragView = null; |
| 2242 | if (onCompleteRunnable != null) { |
| 2243 | onCompleteRunnable.run(); |
| 2244 | } |
| 2245 | } |
| 2246 | }); |
| 2247 | mZoomInOutAnim.start(); |
| 2248 | return true; |
Jim Miller | 19a5267 | 2012-10-23 19:52:04 -0700 | [diff] [blame] | 2249 | } else { |
| 2250 | if (onCompleteRunnable != null) { |
| 2251 | onCompleteRunnable.run(); |
| 2252 | } |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2253 | } |
| 2254 | return false; |
| 2255 | } |
| 2256 | |
| 2257 | /* |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 2258 | * Flinging to delete - IN PROGRESS |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2259 | */ |
| 2260 | private PointF isFlingingToDelete() { |
| 2261 | ViewConfiguration config = ViewConfiguration.get(getContext()); |
| 2262 | mVelocityTracker.computeCurrentVelocity(1000, config.getScaledMaximumFlingVelocity()); |
| 2263 | |
| 2264 | if (mVelocityTracker.getYVelocity() < mFlingToDeleteThresholdVelocity) { |
| 2265 | // Do a quick dot product test to ensure that we are flinging upwards |
| 2266 | PointF vel = new PointF(mVelocityTracker.getXVelocity(), |
| 2267 | mVelocityTracker.getYVelocity()); |
| 2268 | PointF upVec = new PointF(0f, -1f); |
| 2269 | float theta = (float) Math.acos(((vel.x * upVec.x) + (vel.y * upVec.y)) / |
| 2270 | (vel.length() * upVec.length())); |
| 2271 | if (theta <= Math.toRadians(FLING_TO_DELETE_MAX_FLING_DEGREES)) { |
| 2272 | return vel; |
| 2273 | } |
| 2274 | } |
| 2275 | return null; |
| 2276 | } |
| 2277 | |
| 2278 | /** |
| 2279 | * Creates an animation from the current drag view along its current velocity vector. |
| 2280 | * For this animation, the alpha runs for a fixed duration and we update the position |
| 2281 | * progressively. |
| 2282 | */ |
| 2283 | private static class FlingAlongVectorAnimatorUpdateListener implements AnimatorUpdateListener { |
| 2284 | private View mDragView; |
| 2285 | private PointF mVelocity; |
| 2286 | private Rect mFrom; |
| 2287 | private long mPrevTime; |
| 2288 | private float mFriction; |
| 2289 | |
| 2290 | private final TimeInterpolator mAlphaInterpolator = new DecelerateInterpolator(0.75f); |
| 2291 | |
| 2292 | public FlingAlongVectorAnimatorUpdateListener(View dragView, PointF vel, Rect from, |
| 2293 | long startTime, float friction) { |
| 2294 | mDragView = dragView; |
| 2295 | mVelocity = vel; |
| 2296 | mFrom = from; |
| 2297 | mPrevTime = startTime; |
| 2298 | mFriction = 1f - (mDragView.getResources().getDisplayMetrics().density * friction); |
| 2299 | } |
| 2300 | |
| 2301 | @Override |
| 2302 | public void onAnimationUpdate(ValueAnimator animation) { |
| 2303 | float t = ((Float) animation.getAnimatedValue()).floatValue(); |
| 2304 | long curTime = AnimationUtils.currentAnimationTimeMillis(); |
| 2305 | |
| 2306 | mFrom.left += (mVelocity.x * (curTime - mPrevTime) / 1000f); |
| 2307 | mFrom.top += (mVelocity.y * (curTime - mPrevTime) / 1000f); |
| 2308 | |
| 2309 | mDragView.setTranslationX(mFrom.left); |
| 2310 | mDragView.setTranslationY(mFrom.top); |
| 2311 | mDragView.setAlpha(1f - mAlphaInterpolator.getInterpolation(t)); |
| 2312 | |
| 2313 | mVelocity.x *= mFriction; |
| 2314 | mVelocity.y *= mFriction; |
| 2315 | mPrevTime = curTime; |
| 2316 | } |
| 2317 | }; |
| 2318 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2319 | private Runnable createPostDeleteAnimationRunnable(final View dragView) { |
| 2320 | return new Runnable() { |
| 2321 | @Override |
| 2322 | public void run() { |
| 2323 | int dragViewIndex = indexOfChild(dragView); |
| 2324 | |
| 2325 | // For each of the pages around the drag view, animate them from the previous |
| 2326 | // position to the new position in the layout (as a result of the drag view moving |
| 2327 | // in the layout) |
| 2328 | // NOTE: We can make an assumption here because we have side-bound pages that we |
| 2329 | // will always have pages to animate in from the left |
| 2330 | getVisiblePages(mTempVisiblePagesRange); |
| 2331 | boundByReorderablePages(true, mTempVisiblePagesRange); |
| 2332 | boolean isLastWidgetPage = (mTempVisiblePagesRange[0] == mTempVisiblePagesRange[1]); |
| 2333 | boolean slideFromLeft = (isLastWidgetPage || |
| 2334 | dragViewIndex > mTempVisiblePagesRange[0]); |
| 2335 | |
| 2336 | // Setup the scroll to the correct page before we swap the views |
| 2337 | if (slideFromLeft) { |
| 2338 | snapToPageImmediately(dragViewIndex - 1); |
| 2339 | } |
| 2340 | |
| 2341 | int firstIndex = (isLastWidgetPage ? 0 : mTempVisiblePagesRange[0]); |
| 2342 | int lastIndex = Math.min(mTempVisiblePagesRange[1], getPageCount() - 1); |
| 2343 | int lowerIndex = (slideFromLeft ? firstIndex : dragViewIndex + 1 ); |
| 2344 | int upperIndex = (slideFromLeft ? dragViewIndex - 1 : lastIndex); |
| 2345 | ArrayList<Animator> animations = new ArrayList<Animator>(); |
| 2346 | for (int i = lowerIndex; i <= upperIndex; ++i) { |
| 2347 | View v = getChildAt(i); |
| 2348 | // dragViewIndex < pageUnderPointIndex, so after we remove the |
| 2349 | // drag view all subsequent views to pageUnderPointIndex will |
| 2350 | // shift down. |
| 2351 | int oldX = 0; |
| 2352 | int newX = 0; |
| 2353 | if (slideFromLeft) { |
| 2354 | if (i == 0) { |
| 2355 | // Simulate the page being offscreen with the page spacing |
| 2356 | oldX = getViewportOffsetX() + getChildOffset(i) - getChildWidth(i) |
| 2357 | - mPageSpacing; |
| 2358 | } else { |
| 2359 | oldX = getViewportOffsetX() + getChildOffset(i - 1); |
| 2360 | } |
| 2361 | newX = getViewportOffsetX() + getChildOffset(i); |
| 2362 | } else { |
| 2363 | oldX = getChildOffset(i) - getChildOffset(i - 1); |
| 2364 | newX = 0; |
| 2365 | } |
| 2366 | |
| 2367 | // Animate the view translation from its old position to its new |
| 2368 | // position |
| 2369 | AnimatorSet anim = (AnimatorSet) v.getTag(); |
| 2370 | if (anim != null) { |
| 2371 | anim.cancel(); |
| 2372 | } |
| 2373 | |
| 2374 | // Note: Hacky, but we want to skip any optimizations to not draw completely |
| 2375 | // hidden views |
| 2376 | v.setAlpha(Math.max(v.getAlpha(), 0.01f)); |
| 2377 | v.setTranslationX(oldX - newX); |
| 2378 | anim = new AnimatorSet(); |
| 2379 | anim.playTogether( |
| 2380 | ObjectAnimator.ofFloat(v, "translationX", 0f), |
| 2381 | ObjectAnimator.ofFloat(v, "alpha", 1f)); |
| 2382 | animations.add(anim); |
| 2383 | v.setTag(anim); |
| 2384 | } |
| 2385 | |
| 2386 | AnimatorSet slideAnimations = new AnimatorSet(); |
| 2387 | slideAnimations.playTogether(animations); |
| 2388 | slideAnimations.setDuration(DELETE_SLIDE_IN_SIDE_PAGE_DURATION); |
| 2389 | slideAnimations.addListener(new AnimatorListenerAdapter() { |
| 2390 | @Override |
| 2391 | public void onAnimationEnd(Animator animation) { |
| 2392 | final Runnable onCompleteRunnable = new Runnable() { |
| 2393 | @Override |
| 2394 | public void run() { |
| 2395 | mDeferringForDelete = false; |
| 2396 | onEndReordering(); |
Winson Chung | 4752e7d | 2012-11-20 17:06:04 -0800 | [diff] [blame] | 2397 | onRemoveViewAnimationCompleted(); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2398 | } |
| 2399 | }; |
| 2400 | zoomIn(onCompleteRunnable); |
| 2401 | } |
| 2402 | }); |
| 2403 | slideAnimations.start(); |
| 2404 | |
| 2405 | removeView(dragView); |
Michael Jurka | 75b5cfb | 2012-11-15 18:22:47 -0800 | [diff] [blame] | 2406 | onRemoveView(dragView, true); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2407 | } |
| 2408 | }; |
| 2409 | } |
| 2410 | |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2411 | public void onFlingToDelete(PointF vel) { |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2412 | final long startTime = AnimationUtils.currentAnimationTimeMillis(); |
| 2413 | |
| 2414 | // NOTE: Because it takes time for the first frame of animation to actually be |
| 2415 | // called and we expect the animation to be a continuation of the fling, we have |
| 2416 | // to account for the time that has elapsed since the fling finished. And since |
| 2417 | // we don't have a startDelay, we will always get call to update when we call |
| 2418 | // start() (which we want to ignore). |
| 2419 | final TimeInterpolator tInterpolator = new TimeInterpolator() { |
| 2420 | private int mCount = -1; |
| 2421 | private long mStartTime; |
| 2422 | private float mOffset; |
| 2423 | /* Anonymous inner class ctor */ { |
| 2424 | mStartTime = startTime; |
| 2425 | } |
| 2426 | |
| 2427 | @Override |
| 2428 | public float getInterpolation(float t) { |
| 2429 | if (mCount < 0) { |
| 2430 | mCount++; |
| 2431 | } else if (mCount == 0) { |
| 2432 | mOffset = Math.min(0.5f, (float) (AnimationUtils.currentAnimationTimeMillis() - |
| 2433 | mStartTime) / FLING_TO_DELETE_FADE_OUT_DURATION); |
| 2434 | mCount++; |
| 2435 | } |
| 2436 | return Math.min(1f, mOffset + t); |
| 2437 | } |
| 2438 | }; |
| 2439 | |
| 2440 | final Rect from = new Rect(); |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 2441 | final View dragView = mDragView; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2442 | from.left = (int) dragView.getTranslationX(); |
| 2443 | from.top = (int) dragView.getTranslationY(); |
Jim Miller | b5f3b70 | 2012-10-21 19:09:23 -0700 | [diff] [blame] | 2444 | AnimatorUpdateListener updateCb = new FlingAlongVectorAnimatorUpdateListener(dragView, vel, |
| 2445 | from, startTime, FLING_TO_DELETE_FRICTION); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2446 | |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2447 | final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView); |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2448 | |
| 2449 | // Create and start the animation |
| 2450 | ValueAnimator mDropAnim = new ValueAnimator(); |
| 2451 | mDropAnim.setInterpolator(tInterpolator); |
| 2452 | mDropAnim.setDuration(FLING_TO_DELETE_FADE_OUT_DURATION); |
| 2453 | mDropAnim.setFloatValues(0f, 1f); |
| 2454 | mDropAnim.addUpdateListener(updateCb); |
| 2455 | mDropAnim.addListener(new AnimatorListenerAdapter() { |
| 2456 | public void onAnimationEnd(Animator animation) { |
| 2457 | onAnimationEndRunnable.run(); |
| 2458 | } |
| 2459 | }); |
| 2460 | mDropAnim.start(); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2461 | mDeferringForDelete = true; |
| 2462 | } |
| 2463 | |
| 2464 | /* Drag to delete */ |
| 2465 | private boolean isHoveringOverDeleteDropTarget(int x, int y) { |
| 2466 | if (mDeleteDropTarget != null) { |
Winson Chung | c065a5d | 2012-11-07 17:17:33 -0800 | [diff] [blame] | 2467 | mAltTmpRect.set(0, 0, 0, 0); |
| 2468 | View parent = (View) mDeleteDropTarget.getParent(); |
| 2469 | if (parent != null) { |
| 2470 | parent.getGlobalVisibleRect(mAltTmpRect); |
| 2471 | } |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2472 | mDeleteDropTarget.getGlobalVisibleRect(mTmpRect); |
Winson Chung | c065a5d | 2012-11-07 17:17:33 -0800 | [diff] [blame] | 2473 | mTmpRect.offset(-mAltTmpRect.left, -mAltTmpRect.top); |
Winson Chung | f3b9ec8 | 2012-11-01 14:48:51 -0700 | [diff] [blame] | 2474 | return mTmpRect.contains(x, y); |
| 2475 | } |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
| 2479 | protected void setPageHoveringOverDeleteDropTarget(int viewIndex, boolean isHovering) {} |
| 2480 | |
| 2481 | private void onDropToDelete() { |
| 2482 | final View dragView = mDragView; |
| 2483 | |
| 2484 | final float toScale = 0f; |
| 2485 | final float toAlpha = 0f; |
| 2486 | |
| 2487 | // Create and start the complex animation |
| 2488 | ArrayList<Animator> animations = new ArrayList<Animator>(); |
| 2489 | AnimatorSet motionAnim = new AnimatorSet(); |
| 2490 | motionAnim.setInterpolator(new DecelerateInterpolator(2)); |
| 2491 | motionAnim.playTogether( |
| 2492 | ObjectAnimator.ofFloat(dragView, "scaleX", toScale), |
| 2493 | ObjectAnimator.ofFloat(dragView, "scaleY", toScale)); |
| 2494 | animations.add(motionAnim); |
| 2495 | |
| 2496 | AnimatorSet alphaAnim = new AnimatorSet(); |
| 2497 | alphaAnim.setInterpolator(new LinearInterpolator()); |
| 2498 | alphaAnim.playTogether( |
| 2499 | ObjectAnimator.ofFloat(dragView, "alpha", toAlpha)); |
| 2500 | animations.add(alphaAnim); |
| 2501 | |
| 2502 | final Runnable onAnimationEndRunnable = createPostDeleteAnimationRunnable(dragView); |
| 2503 | |
| 2504 | AnimatorSet anim = new AnimatorSet(); |
| 2505 | anim.playTogether(animations); |
| 2506 | anim.setDuration(DRAG_TO_DELETE_FADE_OUT_DURATION); |
| 2507 | anim.addListener(new AnimatorListenerAdapter() { |
| 2508 | public void onAnimationEnd(Animator animation) { |
| 2509 | onAnimationEndRunnable.run(); |
| 2510 | } |
| 2511 | }); |
| 2512 | anim.start(); |
| 2513 | |
| 2514 | mDeferringForDelete = true; |
Jim Miller | d6523da | 2012-10-21 16:47:02 -0700 | [diff] [blame] | 2515 | } |
| 2516 | |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 2517 | /* Accessibility */ |
Jim Miller | dcb3d84 | 2012-08-23 19:18:12 -0700 | [diff] [blame] | 2518 | @Override |
| 2519 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 2520 | super.onInitializeAccessibilityNodeInfo(info); |
| 2521 | info.setScrollable(getPageCount() > 1); |
| 2522 | if (getCurrentPage() < getPageCount() - 1) { |
| 2523 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 2524 | } |
| 2525 | if (getCurrentPage() > 0) { |
| 2526 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | @Override |
| 2531 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 2532 | super.onInitializeAccessibilityEvent(event); |
| 2533 | event.setScrollable(true); |
| 2534 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 2535 | event.setFromIndex(mCurrentPage); |
| 2536 | event.setToIndex(mCurrentPage); |
| 2537 | event.setItemCount(getChildCount()); |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | @Override |
| 2542 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 2543 | if (super.performAccessibilityAction(action, arguments)) { |
| 2544 | return true; |
| 2545 | } |
| 2546 | switch (action) { |
| 2547 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 2548 | if (getCurrentPage() < getPageCount() - 1) { |
| 2549 | scrollRight(); |
| 2550 | return true; |
| 2551 | } |
| 2552 | } break; |
| 2553 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 2554 | if (getCurrentPage() > 0) { |
| 2555 | scrollLeft(); |
| 2556 | return true; |
| 2557 | } |
| 2558 | } break; |
| 2559 | } |
| 2560 | return false; |
| 2561 | } |
| 2562 | |
| 2563 | @Override |
| 2564 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 2565 | return true; |
| 2566 | } |
| 2567 | } |