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