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