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