Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
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; |
Winson Chung | bb6f6a5 | 2011-07-25 17:55:44 -0700 | [diff] [blame] | 21 | import android.animation.ValueAnimator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Rect; |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 26 | import android.os.Bundle; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
| 29 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 31 | import android.view.InputDevice; |
| 32 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 33 | import android.view.MotionEvent; |
| 34 | import android.view.VelocityTracker; |
| 35 | import android.view.View; |
| 36 | import android.view.ViewConfiguration; |
| 37 | import android.view.ViewGroup; |
| 38 | import android.view.ViewParent; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 39 | import android.view.accessibility.AccessibilityEvent; |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 40 | import android.view.accessibility.AccessibilityManager; |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 41 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 42 | import android.view.animation.Interpolator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 43 | import android.widget.Scroller; |
| 44 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 45 | import com.android.launcher3.R; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 46 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 49 | /** |
| 50 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 51 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 52 | */ |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 53 | public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 54 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 55 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 56 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 57 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 58 | // 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] | 59 | private static final int MIN_LENGTH_FOR_FLING = 25; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 60 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 61 | protected static final int PAGE_SNAP_ANIMATION_DURATION = 550; |
Chet Haase | 97687ef | 2012-10-25 16:01:11 -0700 | [diff] [blame] | 62 | protected static final int MAX_PAGE_SNAP_DURATION = 750; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 63 | protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 64 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 65 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 66 | private static final float OVERSCROLL_ACCELERATE_FACTOR = 2; |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 67 | private static final float OVERSCROLL_DAMP_FACTOR = 0.14f; |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 68 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 69 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 70 | // The page is moved more than halfway, automatically move to the next page on touch up. |
| 71 | private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 72 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 73 | // The following constants need to be scaled based on density. The scaled versions will be |
| 74 | // assigned to the corresponding member variables below. |
| 75 | private static final int FLING_THRESHOLD_VELOCITY = 500; |
| 76 | private static final int MIN_SNAP_VELOCITY = 1500; |
| 77 | private static final int MIN_FLING_VELOCITY = 250; |
| 78 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 79 | static final int AUTOMATIC_PAGE_SPACING = -1; |
| 80 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 81 | protected int mFlingThresholdVelocity; |
| 82 | protected int mMinFlingVelocity; |
| 83 | protected int mMinSnapVelocity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 84 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 85 | protected float mDensity; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 86 | protected float mSmoothingTime; |
| 87 | protected float mTouchX; |
| 88 | |
| 89 | protected boolean mFirstLayout = true; |
| 90 | |
| 91 | protected int mCurrentPage; |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 92 | protected int mChildCountOnLastMeasure; |
| 93 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 94 | protected int mNextPage = INVALID_PAGE; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 95 | protected int mMaxScrollX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 96 | protected Scroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 97 | private VelocityTracker mVelocityTracker; |
| 98 | |
| 99 | private float mDownMotionX; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 100 | protected float mLastMotionX; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 101 | protected float mLastMotionXRemainder; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 102 | protected float mLastMotionY; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 103 | protected float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 104 | private int mLastScreenCenter = -1; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 105 | private int[] mChildOffsets; |
| 106 | private int[] mChildRelativeOffsets; |
| 107 | private int[] mChildOffsetsWithLayoutScale; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 108 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 109 | protected final static int TOUCH_STATE_REST = 0; |
| 110 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 111 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 112 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 113 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 114 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 115 | protected int mTouchState = TOUCH_STATE_REST; |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 116 | protected boolean mForceScreenScrolled = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 117 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 118 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 119 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 120 | protected boolean mAllowLongPress = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 121 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 122 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 123 | private int mPagingTouchSlop; |
| 124 | private int mMaximumVelocity; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 125 | private int mMinimumWidth; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 126 | protected int mPageSpacing; |
| 127 | protected int mPageLayoutPaddingTop; |
| 128 | protected int mPageLayoutPaddingBottom; |
| 129 | protected int mPageLayoutPaddingLeft; |
| 130 | protected int mPageLayoutPaddingRight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 131 | protected int mPageLayoutWidthGap; |
| 132 | protected int mPageLayoutHeightGap; |
Michael Jurka | 87b1490 | 2011-05-25 22:13:09 -0700 | [diff] [blame] | 133 | protected int mCellCountX = 0; |
| 134 | protected int mCellCountY = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 135 | protected boolean mCenterPagesVertically; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 136 | protected boolean mAllowOverScroll = true; |
| 137 | protected int mUnboundedScrollX; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 138 | protected int[] mTempVisiblePagesRange = new int[2]; |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 139 | protected boolean mForceDrawAllChildrenNextFrame; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 140 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 141 | // 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] | 142 | // it is equal to the scaled overscroll position. We use a separate value so as to prevent |
| 143 | // the screens from continuing to translate beyond the normal bounds. |
| 144 | protected int mOverScrollX; |
| 145 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 146 | // parameter that adjusts the layout to be optimized for pages with that scale factor |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 147 | protected float mLayoutScale = 1.0f; |
| 148 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 149 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 150 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 151 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 152 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 153 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 154 | |
Michael Jurka | e326f18 | 2011-11-21 14:05:46 -0800 | [diff] [blame] | 155 | protected ArrayList<Boolean> mDirtyPageContent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 156 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 157 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 158 | protected boolean mContentIsRefreshable = true; |
| 159 | |
| 160 | // If true, modify alpha of neighboring pages as user scrolls left/right |
| 161 | protected boolean mFadeInAdjacentScreens = true; |
| 162 | |
| 163 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 164 | // to switch to a new page |
| 165 | protected boolean mUsePagingTouchSlop = true; |
| 166 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 167 | // 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] | 168 | // (SmoothPagedView does this) |
| 169 | protected boolean mDeferScrollUpdate = false; |
| 170 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 171 | protected boolean mIsPageMoving = false; |
| 172 | |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 173 | // All syncs and layout passes are deferred until data is ready. |
| 174 | protected boolean mIsDataReady = false; |
| 175 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 176 | // Scrolling indicator |
Winson Chung | bb6f6a5 | 2011-07-25 17:55:44 -0700 | [diff] [blame] | 177 | private ValueAnimator mScrollIndicatorAnimator; |
Michael Jurka | afaa050 | 2011-12-13 18:22:50 -0800 | [diff] [blame] | 178 | private View mScrollIndicator; |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 179 | private int mScrollIndicatorPaddingLeft; |
| 180 | private int mScrollIndicatorPaddingRight; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 181 | private boolean mHasScrollIndicator = true; |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 182 | private boolean mShouldShowScrollIndicator = false; |
| 183 | private boolean mShouldShowScrollIndicatorImmediately = false; |
Winson Chung | a6427b1 | 2011-07-27 10:53:39 -0700 | [diff] [blame] | 184 | protected static final int sScrollIndicatorFadeInDuration = 150; |
| 185 | protected static final int sScrollIndicatorFadeOutDuration = 650; |
| 186 | protected static final int sScrollIndicatorFlashDuration = 650; |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 187 | private boolean mScrollingPaused = false; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 188 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 189 | // If set, will defer loading associated pages until the scrolling settles |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 190 | private boolean mDeferLoadAssociatedPagesUntilScrollCompletes; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 191 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 192 | public interface PageSwitchListener { |
| 193 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 196 | public PagedView(Context context) { |
| 197 | this(context, null); |
| 198 | } |
| 199 | |
| 200 | public PagedView(Context context, AttributeSet attrs) { |
| 201 | this(context, attrs, 0); |
| 202 | } |
| 203 | |
| 204 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 205 | super(context, attrs, defStyle); |
| 206 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 207 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 208 | R.styleable.PagedView, defStyle, 0); |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 209 | setPageSpacing(a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0)); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 210 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 211 | R.styleable.PagedView_pageLayoutPaddingTop, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 212 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 213 | R.styleable.PagedView_pageLayoutPaddingBottom, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 214 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 215 | R.styleable.PagedView_pageLayoutPaddingLeft, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 216 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 217 | R.styleable.PagedView_pageLayoutPaddingRight, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 218 | mPageLayoutWidthGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 219 | R.styleable.PagedView_pageLayoutWidthGap, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 220 | mPageLayoutHeightGap = a.getDimensionPixelSize( |
Winson Chung | 7d7541e | 2011-09-16 20:14:36 -0700 | [diff] [blame] | 221 | R.styleable.PagedView_pageLayoutHeightGap, 0); |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 222 | mScrollIndicatorPaddingLeft = |
| 223 | a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingLeft, 0); |
| 224 | mScrollIndicatorPaddingRight = |
| 225 | a.getDimensionPixelSize(R.styleable.PagedView_scrollIndicatorPaddingRight, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 226 | a.recycle(); |
| 227 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 228 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 229 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Initializes various states for this workspace. |
| 234 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 235 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 236 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 237 | mDirtyPageContent.ensureCapacity(32); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 238 | mScroller = new Scroller(getContext(), new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 239 | mCurrentPage = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 240 | mCenterPagesVertically = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 241 | |
| 242 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 243 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 244 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 245 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 246 | mDensity = getResources().getDisplayMetrics().density; |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 247 | |
| 248 | mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * mDensity); |
| 249 | mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * mDensity); |
| 250 | mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * mDensity); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 251 | setOnHierarchyChangeListener(this); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 254 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 255 | mPageSwitchListener = pageSwitchListener; |
| 256 | if (mPageSwitchListener != null) { |
| 257 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 262 | * Note: this is a reimplementation of View.isLayoutRtl() since that is currently hidden api. |
| 263 | */ |
| 264 | public boolean isLayoutRtl() { |
| 265 | return (getLayoutDirection() == LAYOUT_DIRECTION_RTL); |
| 266 | } |
| 267 | |
| 268 | /** |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 269 | * Called by subclasses to mark that data is ready, and that we can begin loading and laying |
| 270 | * out pages. |
| 271 | */ |
| 272 | protected void setDataIsReady() { |
| 273 | mIsDataReady = true; |
| 274 | } |
| 275 | protected boolean isDataReady() { |
| 276 | return mIsDataReady; |
| 277 | } |
| 278 | |
| 279 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 280 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 281 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 282 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 283 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 284 | int getCurrentPage() { |
| 285 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 286 | } |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 287 | int getNextPage() { |
| 288 | return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage; |
| 289 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 290 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 291 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 292 | return getChildCount(); |
| 293 | } |
| 294 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 295 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 296 | return getChildAt(index); |
| 297 | } |
| 298 | |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 299 | protected int indexToPage(int index) { |
| 300 | return index; |
| 301 | } |
| 302 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 303 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 304 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 305 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 306 | * the previous tab page. |
| 307 | */ |
| 308 | protected void updateCurrentPageScroll() { |
Winson Chung | 1ef2123 | 2012-11-26 14:08:38 -0800 | [diff] [blame] | 309 | // If the current page is invalid, just reset the scroll position to zero |
| 310 | int newX = 0; |
| 311 | if (0 <= mCurrentPage && mCurrentPage < getPageCount()) { |
| 312 | int offset = getChildOffset(mCurrentPage); |
| 313 | int relOffset = getRelativeChildOffset(mCurrentPage); |
| 314 | newX = offset - relOffset; |
| 315 | } |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 316 | scrollTo(newX, 0); |
| 317 | mScroller.setFinalX(newX); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 318 | mScroller.forceFinished(true); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 322 | * Called during AllApps/Home transitions to avoid unnecessary work. When that other animation |
| 323 | * ends, {@link #resumeScrolling()} should be called, along with |
| 324 | * {@link #updateCurrentPageScroll()} to correctly set the final state and re-enable scrolling. |
| 325 | */ |
| 326 | void pauseScrolling() { |
| 327 | mScroller.forceFinished(true); |
| 328 | cancelScrollingIndicatorAnimations(); |
| 329 | mScrollingPaused = true; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Enables scrolling again. |
| 334 | * @see #pauseScrolling() |
| 335 | */ |
| 336 | void resumeScrolling() { |
| 337 | mScrollingPaused = false; |
| 338 | } |
| 339 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 340 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 341 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 342 | void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 343 | if (!mScroller.isFinished()) { |
| 344 | mScroller.abortAnimation(); |
| 345 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 346 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 347 | // the default |
| 348 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 349 | return; |
| 350 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 351 | |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 352 | mForceScreenScrolled = true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 353 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 354 | updateCurrentPageScroll(); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 355 | updateScrollingIndicator(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 356 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 357 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 360 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 361 | if (mPageSwitchListener != null) { |
| 362 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 366 | protected void pageBeginMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 367 | if (!mIsPageMoving) { |
| 368 | mIsPageMoving = true; |
| 369 | onPageBeginMoving(); |
| 370 | } |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 373 | protected void pageEndMoving() { |
Michael Jurka | d74c984 | 2011-07-10 12:44:21 -0700 | [diff] [blame] | 374 | if (mIsPageMoving) { |
| 375 | mIsPageMoving = false; |
| 376 | onPageEndMoving(); |
| 377 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 380 | protected boolean isPageMoving() { |
| 381 | return mIsPageMoving; |
| 382 | } |
| 383 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 384 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 385 | protected void onPageBeginMoving() { |
| 386 | } |
| 387 | |
| 388 | // a method that subclasses can override to add behavior |
| 389 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 392 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 393 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 394 | * |
| 395 | * @param l The listener used to respond to long clicks. |
| 396 | */ |
| 397 | @Override |
| 398 | public void setOnLongClickListener(OnLongClickListener l) { |
| 399 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 400 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 401 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 402 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
| 406 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 407 | public void scrollBy(int x, int y) { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 408 | scrollTo(mUnboundedScrollX + x, getScrollY() + y); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 412 | public void scrollTo(int x, int y) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 413 | final boolean isRtl = isLayoutRtl(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 414 | mUnboundedScrollX = x; |
| 415 | |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 416 | boolean isXBeforeFirstPage = isRtl ? (x > mMaxScrollX) : (x < 0); |
| 417 | boolean isXAfterLastPage = isRtl ? (x < 0) : (x > mMaxScrollX); |
| 418 | if (isXBeforeFirstPage) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 419 | super.scrollTo(0, y); |
| 420 | if (mAllowOverScroll) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 421 | if (isRtl) { |
| 422 | overScroll(x - mMaxScrollX); |
| 423 | } else { |
| 424 | overScroll(x); |
| 425 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 426 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 427 | } else if (isXAfterLastPage) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 428 | super.scrollTo(mMaxScrollX, y); |
| 429 | if (mAllowOverScroll) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 430 | if (isRtl) { |
| 431 | overScroll(x); |
| 432 | } else { |
| 433 | overScroll(x - mMaxScrollX); |
| 434 | } |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 435 | } |
| 436 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 437 | mOverScrollX = x; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 438 | super.scrollTo(x, y); |
| 439 | } |
| 440 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 441 | mTouchX = x; |
| 442 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 443 | } |
| 444 | |
| 445 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 446 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 447 | if (mScroller.computeScrollOffset()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 448 | // 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] | 449 | if (getScrollX() != mScroller.getCurrX() |
| 450 | || getScrollY() != mScroller.getCurrY() |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 451 | || mOverScrollX != mScroller.getCurrX()) { |
Winson Chung | 557d6ed | 2011-07-08 15:34:52 -0700 | [diff] [blame] | 452 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
| 453 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 454 | invalidate(); |
| 455 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 456 | } else if (mNextPage != INVALID_PAGE) { |
| 457 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 458 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 459 | notifyPageSwitchListener(); |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 460 | |
| 461 | // Load the associated pages if necessary |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 462 | if (mDeferLoadAssociatedPagesUntilScrollCompletes) { |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 463 | loadAssociatedPages(mCurrentPage); |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 464 | mDeferLoadAssociatedPagesUntilScrollCompletes = false; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 467 | // We don't want to trigger a page end moving unless the page has settled |
| 468 | // and the user has stopped scrolling |
| 469 | if (mTouchState == TOUCH_STATE_REST) { |
| 470 | pageEndMoving(); |
| 471 | } |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 472 | |
| 473 | // Notify the user when the page changes |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 474 | AccessibilityManager accessibilityManager = (AccessibilityManager) |
| 475 | getContext().getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 476 | if (accessibilityManager.isEnabled()) { |
Winson Chung | c27d1bb | 2011-09-29 12:07:42 -0700 | [diff] [blame] | 477 | AccessibilityEvent ev = |
| 478 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED); |
| 479 | ev.getText().add(getCurrentPageDescription()); |
| 480 | sendAccessibilityEventUnchecked(ev); |
| 481 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 482 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 483 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public void computeScroll() { |
| 489 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | @Override |
| 493 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Adam Cohen | dcd297f | 2013-06-18 13:13:40 -0700 | [diff] [blame] | 494 | if (!mIsDataReady || getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 495 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 496 | return; |
| 497 | } |
| 498 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 499 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 500 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 501 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 502 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 503 | if (widthMode != MeasureSpec.EXACTLY) { |
| 504 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 505 | } |
| 506 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 507 | // Return early if we aren't given a proper dimension |
| 508 | if (widthSize <= 0 || heightSize <= 0) { |
| 509 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 510 | return; |
| 511 | } |
| 512 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 513 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 514 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 515 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 516 | * each page to have the same width. |
| 517 | */ |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 518 | int maxChildHeight = 0; |
| 519 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 520 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
| 521 | final int horizontalPadding = getPaddingLeft() + getPaddingRight(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 522 | |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 523 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 524 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 525 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 526 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 527 | final int childCount = getChildCount(); |
| 528 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 529 | // disallowing padding in paged view (just pass 0) |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 530 | final View child = getPageAt(i); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 531 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 532 | |
| 533 | int childWidthMode; |
| 534 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 535 | childWidthMode = MeasureSpec.AT_MOST; |
| 536 | } else { |
| 537 | childWidthMode = MeasureSpec.EXACTLY; |
| 538 | } |
| 539 | |
| 540 | int childHeightMode; |
| 541 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 542 | childHeightMode = MeasureSpec.AT_MOST; |
| 543 | } else { |
| 544 | childHeightMode = MeasureSpec.EXACTLY; |
| 545 | } |
| 546 | |
| 547 | final int childWidthMeasureSpec = |
Winson Chung | ea359c6 | 2011-08-03 17:06:35 -0700 | [diff] [blame] | 548 | MeasureSpec.makeMeasureSpec(widthSize - horizontalPadding, childWidthMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 549 | final int childHeightMeasureSpec = |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 550 | MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 551 | |
| 552 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 553 | maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 554 | if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", " |
| 555 | + child.getMeasuredHeight()); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (heightMode == MeasureSpec.AT_MOST) { |
| 559 | heightSize = maxChildHeight + verticalPadding; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 560 | } |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 561 | |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 562 | setMeasuredDimension(widthSize, heightSize); |
| 563 | |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 564 | // We can't call getChildOffset/getRelativeChildOffset until we set the measured dimensions. |
| 565 | // We also wait until we set the measured dimensions before flushing the cache as well, to |
| 566 | // ensure that the cache is filled with good values. |
| 567 | invalidateCachedOffsets(); |
| 568 | |
Adam Cohen | e61a9a2 | 2013-06-11 15:45:31 -0700 | [diff] [blame] | 569 | if (mChildCountOnLastMeasure != getChildCount()) { |
| 570 | setCurrentPage(mCurrentPage); |
| 571 | } |
| 572 | mChildCountOnLastMeasure = getChildCount(); |
| 573 | |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 574 | if (childCount > 0) { |
| 575 | if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 576 | + getChildWidth(0)); |
| 577 | |
| 578 | // Calculate the variable page spacing if necessary |
Winson Chung | 8aad610 | 2012-05-11 16:27:49 -0700 | [diff] [blame] | 579 | if (mPageSpacing == AUTOMATIC_PAGE_SPACING) { |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 580 | // The gap between pages in the PagedView should be equal to the gap from the page |
| 581 | // to the edge of the screen (so it is not visible in the current screen). To |
| 582 | // account for unequal padding on each side of the paged view, we take the maximum |
| 583 | // of the left/right gap and use that as the gap between each page. |
| 584 | int offset = getRelativeChildOffset(0); |
| 585 | int spacing = Math.max(offset, widthSize - offset - |
| 586 | getChildAt(0).getMeasuredWidth()); |
| 587 | setPageSpacing(spacing); |
| 588 | } |
| 589 | } |
| 590 | |
Adam Cohen | 25b2995 | 2011-11-02 14:49:06 -0700 | [diff] [blame] | 591 | updateScrollingIndicatorPosition(); |
| 592 | |
Adam Cohen | faa2830 | 2010-11-19 12:02:18 -0800 | [diff] [blame] | 593 | if (childCount > 0) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 594 | final int index = isLayoutRtl() ? 0 : childCount - 1; |
| 595 | mMaxScrollX = getChildOffset(index) - getRelativeChildOffset(index); |
Adam Cohen | faa2830 | 2010-11-19 12:02:18 -0800 | [diff] [blame] | 596 | } else { |
| 597 | mMaxScrollX = 0; |
| 598 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 601 | protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 602 | int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 603 | int delta = newX - getScrollX(); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 604 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 605 | final int pageCount = getChildCount(); |
| 606 | for (int i = 0; i < pageCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 607 | View page = (View) getPageAt(i); |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 608 | page.setX(page.getX() + delta); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 609 | } |
| 610 | setCurrentPage(newCurrentPage); |
| 611 | } |
| 612 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 613 | // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a |
| 614 | // scale of 1.0f. A layout scale of 0.8f assumes the pages have a scale of 0.8f, and |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 615 | // tightens the layout accordingly |
| 616 | public void setLayoutScale(float childrenScale) { |
| 617 | mLayoutScale = childrenScale; |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 618 | invalidateCachedOffsets(); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 619 | |
| 620 | // Now we need to do a re-layout, but preserving absolute X and Y coordinates |
| 621 | int childCount = getChildCount(); |
| 622 | float childrenX[] = new float[childCount]; |
| 623 | float childrenY[] = new float[childCount]; |
| 624 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 625 | final View child = getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 626 | childrenX[i] = child.getX(); |
| 627 | childrenY[i] = child.getY(); |
| 628 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 629 | // Trigger a full re-layout (never just call onLayout directly!) |
| 630 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY); |
| 631 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY); |
| 632 | requestLayout(); |
| 633 | measure(widthSpec, heightSpec); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 634 | layout(getLeft(), getTop(), getRight(), getBottom()); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 635 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 636 | final View child = getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 637 | child.setX(childrenX[i]); |
| 638 | child.setY(childrenY[i]); |
| 639 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 640 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 641 | // Also, the page offset has changed (since the pages are now smaller); |
| 642 | // update the page offset, but again preserving absolute X and Y coordinates |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 643 | scrollToNewPageWithoutMovingPages(mCurrentPage); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Adam Cohen | 60b0712 | 2011-11-14 17:26:06 -0800 | [diff] [blame] | 646 | public void setPageSpacing(int pageSpacing) { |
| 647 | mPageSpacing = pageSpacing; |
| 648 | invalidateCachedOffsets(); |
| 649 | } |
| 650 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 651 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 652 | 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] | 653 | if (!mIsDataReady || getChildCount() == 0) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 654 | return; |
| 655 | } |
| 656 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 657 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 658 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 659 | final int childCount = getChildCount(); |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 660 | final boolean isRtl = isLayoutRtl(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 661 | |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 662 | final int startIndex = isRtl ? childCount - 1 : 0; |
| 663 | final int endIndex = isRtl ? -1 : childCount; |
| 664 | final int delta = isRtl ? -1 : 1; |
| 665 | int childLeft = getRelativeChildOffset(startIndex); |
| 666 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 667 | final View child = getPageAt(i); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 668 | if (child.getVisibility() != View.GONE) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 669 | final int childWidth = getScaledMeasuredWidth(child); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 670 | final int childHeight = child.getMeasuredHeight(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 671 | int childTop = getPaddingTop(); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 672 | if (mCenterPagesVertically) { |
| 673 | childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2; |
| 674 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 675 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 676 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 677 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 678 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 679 | childLeft += childWidth + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 680 | } |
| 681 | } |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 682 | |
| 683 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 684 | setHorizontalScrollBarEnabled(false); |
Michael Jurka | dd6c091 | 2012-01-16 06:46:20 -0800 | [diff] [blame] | 685 | updateCurrentPageScroll(); |
Winson Chung | c3665fa | 2011-09-14 17:56:27 -0700 | [diff] [blame] | 686 | setHorizontalScrollBarEnabled(true); |
| 687 | mFirstLayout = false; |
| 688 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 691 | protected void screenScrolled(int screenCenter) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 692 | if (isScrollingIndicatorEnabled()) { |
| 693 | updateScrollingIndicator(); |
| 694 | } |
Michael Jurka | 869390b | 2012-05-06 15:55:19 -0700 | [diff] [blame] | 695 | boolean isInOverscroll = mOverScrollX < 0 || mOverScrollX > mMaxScrollX; |
| 696 | |
| 697 | if (mFadeInAdjacentScreens && !isInOverscroll) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 698 | for (int i = 0; i < getChildCount(); i++) { |
| 699 | View child = getChildAt(i); |
| 700 | if (child != null) { |
| 701 | float scrollProgress = getScrollProgress(screenCenter, child, i); |
| 702 | float alpha = 1 - Math.abs(scrollProgress); |
Michael Jurka | 7372c59 | 2012-01-16 04:21:35 -0800 | [diff] [blame] | 703 | child.setAlpha(alpha); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | invalidate(); |
| 707 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 710 | @Override |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 711 | public void onChildViewAdded(View parent, View child) { |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 712 | // This ensures that when children are added, they get the correct transforms / alphas |
| 713 | // in accordance with any scroll effects. |
| 714 | mForceScreenScrolled = true; |
| 715 | invalidate(); |
Adam Cohen | 25b2995 | 2011-11-02 14:49:06 -0700 | [diff] [blame] | 716 | invalidateCachedOffsets(); |
Adam Cohen | 2591f6a | 2011-10-25 14:36:40 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 719 | @Override |
| 720 | public void onChildViewRemoved(View parent, View child) { |
| 721 | } |
| 722 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 723 | protected void invalidateCachedOffsets() { |
| 724 | int count = getChildCount(); |
Adam Cohen | 25b2995 | 2011-11-02 14:49:06 -0700 | [diff] [blame] | 725 | if (count == 0) { |
| 726 | mChildOffsets = null; |
| 727 | mChildRelativeOffsets = null; |
| 728 | mChildOffsetsWithLayoutScale = null; |
| 729 | return; |
| 730 | } |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 731 | |
| 732 | mChildOffsets = new int[count]; |
| 733 | mChildRelativeOffsets = new int[count]; |
| 734 | mChildOffsetsWithLayoutScale = new int[count]; |
| 735 | for (int i = 0; i < count; i++) { |
| 736 | mChildOffsets[i] = -1; |
| 737 | mChildRelativeOffsets[i] = -1; |
| 738 | mChildOffsetsWithLayoutScale[i] = -1; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | protected int getChildOffset(int index) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 743 | final boolean isRtl = isLayoutRtl(); |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 744 | int[] childOffsets = Float.compare(mLayoutScale, 1f) == 0 ? |
| 745 | mChildOffsets : mChildOffsetsWithLayoutScale; |
| 746 | |
| 747 | if (childOffsets != null && childOffsets[index] != -1) { |
| 748 | return childOffsets[index]; |
| 749 | } else { |
| 750 | if (getChildCount() == 0) |
| 751 | return 0; |
| 752 | |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 753 | final int startIndex = isRtl ? getChildCount() - 1 : 0; |
| 754 | final int endIndex = isRtl ? index : index; |
| 755 | final int delta = isRtl ? -1 : 1; |
| 756 | int offset = getRelativeChildOffset(startIndex); |
| 757 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 758 | offset += getScaledMeasuredWidth(getPageAt(i)) + mPageSpacing; |
| 759 | } |
| 760 | if (childOffsets != null) { |
| 761 | childOffsets[index] = offset; |
| 762 | } |
| 763 | return offset; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | protected int getRelativeChildOffset(int index) { |
| 768 | if (mChildRelativeOffsets != null && mChildRelativeOffsets[index] != -1) { |
| 769 | return mChildRelativeOffsets[index]; |
| 770 | } else { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 771 | final int padding = getPaddingLeft() + getPaddingRight(); |
| 772 | final int offset = getPaddingLeft() + |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 773 | (getMeasuredWidth() - padding - getChildWidth(index)) / 2; |
| 774 | if (mChildRelativeOffsets != null) { |
| 775 | mChildRelativeOffsets[index] = offset; |
| 776 | } |
| 777 | return offset; |
| 778 | } |
| 779 | } |
| 780 | |
Adam Cohen | 7389496 | 2011-10-31 13:17:17 -0700 | [diff] [blame] | 781 | protected int getScaledMeasuredWidth(View child) { |
| 782 | // This functions are called enough times that it actually makes a difference in the |
| 783 | // profiler -- so just inline the max() here |
| 784 | final int measuredWidth = child.getMeasuredWidth(); |
| 785 | final int minWidth = mMinimumWidth; |
| 786 | final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth; |
| 787 | return (int) (maxWidth * mLayoutScale + 0.5f); |
| 788 | } |
| 789 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 790 | protected void getVisiblePages(int[] range) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 791 | final boolean isRtl = isLayoutRtl(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 792 | final int pageCount = getChildCount(); |
Michael Jurka | 4ff7d79 | 2012-04-02 03:46:50 -0700 | [diff] [blame] | 793 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 794 | if (pageCount > 0) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 795 | final int screenWidth = getMeasuredWidth(); |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 796 | int leftScreen = isRtl ? pageCount - 1 : 0; |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 797 | int rightScreen = 0; |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 798 | int endIndex = isRtl ? 0 : pageCount - 1; |
| 799 | int delta = isRtl ? -1 : 1; |
Michael Jurka | 47f7474 | 2012-03-02 13:39:13 -0800 | [diff] [blame] | 800 | View currPage = getPageAt(leftScreen); |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 801 | while (leftScreen != endIndex && |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 802 | currPage.getX() + currPage.getWidth() - |
| 803 | currPage.getPaddingRight() < getScrollX()) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 804 | leftScreen += delta; |
Michael Jurka | 47f7474 | 2012-03-02 13:39:13 -0800 | [diff] [blame] | 805 | currPage = getPageAt(leftScreen); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 806 | } |
| 807 | rightScreen = leftScreen; |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 808 | currPage = getPageAt(rightScreen + delta); |
| 809 | while (rightScreen != endIndex && |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 810 | currPage.getX() - currPage.getPaddingLeft() < getScrollX() + screenWidth) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 811 | rightScreen += delta; |
| 812 | currPage = getPageAt(rightScreen + delta); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 813 | } |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 814 | range[0] = Math.min(leftScreen, rightScreen); |
| 815 | range[1] = Math.max(leftScreen, rightScreen); |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 816 | } else { |
| 817 | range[0] = -1; |
| 818 | range[1] = -1; |
| 819 | } |
| 820 | } |
| 821 | |
Michael Jurka | 920d7f4 | 2012-05-14 16:29:55 -0700 | [diff] [blame] | 822 | protected boolean shouldDrawChild(View child) { |
| 823 | return child.getAlpha() > 0; |
| 824 | } |
| 825 | |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 826 | @Override |
| 827 | protected void dispatchDraw(Canvas canvas) { |
| 828 | int halfScreenSize = getMeasuredWidth() / 2; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 829 | // mOverScrollX is equal to getScrollX() when we're within the normal scroll range. |
| 830 | // Otherwise it is equal to the scaled overscroll position. |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 831 | int screenCenter = mOverScrollX + halfScreenSize; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 832 | |
| 833 | if (screenCenter != mLastScreenCenter || mForceScreenScrolled) { |
Michael Jurka | b06d95f | 2012-04-02 06:26:53 -0700 | [diff] [blame] | 834 | // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can |
| 835 | // set it for the next frame |
| 836 | mForceScreenScrolled = false; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 837 | screenScrolled(screenCenter); |
| 838 | mLastScreenCenter = screenCenter; |
Michael Jurka | dde558b | 2011-11-09 22:09:06 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // Find out which screens are visible; as an optimization we only call draw on them |
| 842 | final int pageCount = getChildCount(); |
| 843 | if (pageCount > 0) { |
| 844 | getVisiblePages(mTempVisiblePagesRange); |
| 845 | final int leftScreen = mTempVisiblePagesRange[0]; |
| 846 | final int rightScreen = mTempVisiblePagesRange[1]; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 847 | if (leftScreen != -1 && rightScreen != -1) { |
| 848 | final long drawingTime = getDrawingTime(); |
| 849 | // Clip to the bounds |
| 850 | canvas.save(); |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 851 | canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(), |
| 852 | getScrollY() + getBottom() - getTop()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 853 | |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 854 | for (int i = getChildCount() - 1; i >= 0; i--) { |
| 855 | final View v = getPageAt(i); |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 856 | if (mForceDrawAllChildrenNextFrame || |
| 857 | (leftScreen <= i && i <= rightScreen && shouldDrawChild(v))) { |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 858 | drawChild(canvas, v, drawingTime); |
Michael Jurka | 80c6985 | 2011-12-16 14:16:32 -0800 | [diff] [blame] | 859 | } |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 860 | } |
Michael Jurka | 5e368ff | 2012-05-14 23:13:15 -0700 | [diff] [blame] | 861 | mForceDrawAllChildrenNextFrame = false; |
Winson Chung | c6f10b9 | 2011-11-14 11:39:07 -0800 | [diff] [blame] | 862 | canvas.restore(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 863 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 864 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | @Override |
| 868 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 869 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 870 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 871 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 872 | return true; |
| 873 | } |
| 874 | return false; |
| 875 | } |
| 876 | |
| 877 | @Override |
| 878 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 879 | int focusablePage; |
| 880 | if (mNextPage != INVALID_PAGE) { |
| 881 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 882 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 883 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 884 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 885 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 886 | if (v != null) { |
Adam Cohen | 76fc085 | 2011-06-17 13:26:23 -0700 | [diff] [blame] | 887 | return v.requestFocus(direction, previouslyFocusedRect); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 888 | } |
| 889 | return false; |
| 890 | } |
| 891 | |
| 892 | @Override |
| 893 | public boolean dispatchUnhandledMove(View focused, int direction) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 894 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 895 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 896 | if (getCurrentPage() > 0) { |
| 897 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 898 | return true; |
| 899 | } |
| 900 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 901 | if (getCurrentPage() < getPageCount() - 1) { |
| 902 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 903 | return true; |
| 904 | } |
| 905 | } |
| 906 | return super.dispatchUnhandledMove(focused, direction); |
| 907 | } |
| 908 | |
| 909 | @Override |
| 910 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 911 | // XXX-RTL: This will be fixed in a future CL |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 912 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 913 | getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 914 | } |
| 915 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 916 | if (mCurrentPage > 0) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 917 | getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 918 | } |
| 919 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 920 | if (mCurrentPage < getPageCount() - 1) { |
alanv | af51995 | 2012-05-07 17:33:22 -0700 | [diff] [blame] | 921 | getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * 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] | 928 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 929 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 930 | * This happens when live folders requery, and if they're off page, they |
| 931 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 932 | */ |
| 933 | @Override |
| 934 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 935 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 936 | View v = focused; |
| 937 | while (true) { |
| 938 | if (v == current) { |
| 939 | super.focusableViewAvailable(focused); |
| 940 | return; |
| 941 | } |
| 942 | if (v == this) { |
| 943 | return; |
| 944 | } |
| 945 | ViewParent parent = v.getParent(); |
| 946 | if (parent instanceof View) { |
| 947 | v = (View)v.getParent(); |
| 948 | } else { |
| 949 | return; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
| 954 | /** |
| 955 | * {@inheritDoc} |
| 956 | */ |
| 957 | @Override |
| 958 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 959 | if (disallowIntercept) { |
| 960 | // We need to make sure to cancel our long press if |
| 961 | // a scrollable widget takes over touch events |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 962 | final View currentPage = getPageAt(mCurrentPage); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 963 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 964 | } |
| 965 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 966 | } |
| 967 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 968 | /** |
| 969 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 970 | */ |
| 971 | protected boolean hitsPreviousPage(float x, float y) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 972 | if (isLayoutRtl()) { |
| 973 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 974 | } else { |
| 975 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 976 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | /** |
| 980 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 981 | */ |
| 982 | protected boolean hitsNextPage(float x, float y) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 983 | if (isLayoutRtl()) { |
| 984 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 985 | } else { |
| 986 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 987 | } |
| 988 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 989 | } |
| 990 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 991 | @Override |
| 992 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 993 | /* |
| 994 | * This method JUST determines whether we want to intercept the motion. |
| 995 | * If we return true, onTouchEvent will be called and we do the actual |
| 996 | * scrolling there. |
| 997 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 998 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 999 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1000 | // Skip touch handling if there are no pages to swipe |
| 1001 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 1002 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1003 | /* |
| 1004 | * Shortcut the most recurring case: the user is in the dragging |
| 1005 | * state and he is moving his finger. We want to intercept this |
| 1006 | * motion. |
| 1007 | */ |
| 1008 | final int action = ev.getAction(); |
| 1009 | if ((action == MotionEvent.ACTION_MOVE) && |
| 1010 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1014 | switch (action & MotionEvent.ACTION_MASK) { |
| 1015 | case MotionEvent.ACTION_MOVE: { |
| 1016 | /* |
| 1017 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 1018 | * whether the user has moved far enough from his original down touch. |
| 1019 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 1020 | if (mActivePointerId != INVALID_POINTER) { |
| 1021 | determineScrollingStart(ev); |
| 1022 | break; |
| 1023 | } |
| 1024 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 1025 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 1026 | // i.e. fall through to the next case (don't break) |
| 1027 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 1028 | // while it's small- this was causing a crash before we checked for INVALID_POINTER) |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | case MotionEvent.ACTION_DOWN: { |
| 1032 | final float x = ev.getX(); |
| 1033 | final float y = ev.getY(); |
| 1034 | // Remember location of down touch |
| 1035 | mDownMotionX = x; |
| 1036 | mLastMotionX = x; |
| 1037 | mLastMotionY = y; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1038 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1039 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1040 | mActivePointerId = ev.getPointerId(0); |
| 1041 | mAllowLongPress = true; |
| 1042 | |
| 1043 | /* |
| 1044 | * If being flinged and user touches the screen, initiate drag; |
| 1045 | * otherwise don't. mScroller.isFinished should be false when |
| 1046 | * being flinged. |
| 1047 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 1048 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1049 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 1050 | if (finishedScrolling) { |
| 1051 | mTouchState = TOUCH_STATE_REST; |
| 1052 | mScroller.abortAnimation(); |
| 1053 | } else { |
| 1054 | mTouchState = TOUCH_STATE_SCROLLING; |
| 1055 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1056 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1057 | // 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] | 1058 | // to scroll the current page |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1059 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1060 | if (getChildCount() > 0) { |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1061 | if (hitsPreviousPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1062 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1063 | } else if (hitsNextPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1064 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | break; |
| 1069 | } |
| 1070 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1071 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1072 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1073 | mTouchState = TOUCH_STATE_REST; |
| 1074 | mAllowLongPress = false; |
| 1075 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1076 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case MotionEvent.ACTION_POINTER_UP: |
| 1080 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1081 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
| 1086 | * The only time we want to intercept motion events is if we are in the |
| 1087 | * drag mode. |
| 1088 | */ |
| 1089 | return mTouchState != TOUCH_STATE_REST; |
| 1090 | } |
| 1091 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1092 | protected void determineScrollingStart(MotionEvent ev) { |
| 1093 | determineScrollingStart(ev, 1.0f); |
| 1094 | } |
| 1095 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1096 | /* |
| 1097 | * Determines if we should change the touch state to start scrolling after the |
| 1098 | * user moves their touch point too far. |
| 1099 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1100 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1101 | /* |
| 1102 | * Locally do absolute value. mLastMotionX is set to the y value |
| 1103 | * of the down event. |
| 1104 | */ |
| 1105 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
Michael Jurka | 2698db4 | 2011-07-13 18:25:14 -0700 | [diff] [blame] | 1106 | if (pointerIndex == -1) { |
| 1107 | return; |
| 1108 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1109 | final float x = ev.getX(pointerIndex); |
| 1110 | final float y = ev.getY(pointerIndex); |
| 1111 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 1112 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 1113 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1114 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1115 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 1116 | boolean xMoved = xDiff > touchSlop; |
| 1117 | boolean yMoved = yDiff > touchSlop; |
| 1118 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1119 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1120 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1121 | // Scroll if the user moved far enough along the X axis |
| 1122 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 1123 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1124 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1125 | mLastMotionXRemainder = 0; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1126 | mTouchX = getScrollX(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1127 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1128 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1129 | } |
| 1130 | // Either way, cancel any pending longpress |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 1131 | cancelCurrentPageLongPress(); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | protected void cancelCurrentPageLongPress() { |
| 1136 | if (mAllowLongPress) { |
| 1137 | mAllowLongPress = false; |
| 1138 | // Try canceling the long press. It could also have been scheduled |
| 1139 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 1140 | // everything |
| 1141 | final View currentPage = getPageAt(mCurrentPage); |
| 1142 | if (currentPage != null) { |
| 1143 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1148 | protected float getScrollProgress(int screenCenter, View v, int page) { |
| 1149 | final int halfScreenSize = getMeasuredWidth() / 2; |
| 1150 | |
| 1151 | int totalDistance = getScaledMeasuredWidth(v) + mPageSpacing; |
| 1152 | int delta = screenCenter - (getChildOffset(page) - |
| 1153 | getRelativeChildOffset(page) + halfScreenSize); |
| 1154 | |
| 1155 | float scrollProgress = delta / (totalDistance * 1.0f); |
| 1156 | scrollProgress = Math.min(scrollProgress, 1.0f); |
| 1157 | scrollProgress = Math.max(scrollProgress, -1.0f); |
| 1158 | return scrollProgress; |
| 1159 | } |
| 1160 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1161 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 1162 | // as the user pulls further and further from the bounds |
| 1163 | private float overScrollInfluenceCurve(float f) { |
| 1164 | f -= 1.0f; |
| 1165 | return f * f * f + 1.0f; |
| 1166 | } |
| 1167 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1168 | protected void acceleratedOverScroll(float amount) { |
| 1169 | int screenSize = getMeasuredWidth(); |
| 1170 | |
| 1171 | // We want to reach the max over scroll effect when the user has |
| 1172 | // over scrolled half the size of the screen |
| 1173 | float f = OVERSCROLL_ACCELERATE_FACTOR * (amount / screenSize); |
| 1174 | |
| 1175 | if (f == 0) return; |
| 1176 | |
| 1177 | // Clamp this factor, f, to -1 < f < 1 |
| 1178 | if (Math.abs(f) >= 1) { |
| 1179 | f /= Math.abs(f); |
| 1180 | } |
| 1181 | |
| 1182 | int overScrollAmount = (int) Math.round(f * screenSize); |
| 1183 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1184 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1185 | super.scrollTo(0, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1186 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1187 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1188 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1189 | } |
| 1190 | invalidate(); |
| 1191 | } |
| 1192 | |
| 1193 | protected void dampedOverScroll(float amount) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1194 | int screenSize = getMeasuredWidth(); |
| 1195 | |
| 1196 | float f = (amount / screenSize); |
| 1197 | |
| 1198 | if (f == 0) return; |
| 1199 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1200 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 1201 | // Clamp this factor, f, to -1 < f < 1 |
| 1202 | if (Math.abs(f) >= 1) { |
| 1203 | f /= Math.abs(f); |
| 1204 | } |
| 1205 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1206 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1207 | if (amount < 0) { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1208 | mOverScrollX = overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1209 | super.scrollTo(0, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1210 | } else { |
Adam Cohen | ebea84d | 2011-11-09 17:20:41 -0800 | [diff] [blame] | 1211 | mOverScrollX = mMaxScrollX + overScrollAmount; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1212 | super.scrollTo(mMaxScrollX, getScrollY()); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1213 | } |
| 1214 | invalidate(); |
| 1215 | } |
| 1216 | |
Adam Cohen | b5ba097 | 2011-09-07 18:02:31 -0700 | [diff] [blame] | 1217 | protected void overScroll(float amount) { |
| 1218 | dampedOverScroll(amount); |
| 1219 | } |
| 1220 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 1221 | protected float maxOverScroll() { |
| 1222 | // 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] | 1223 | // 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] | 1224 | float f = 1.0f; |
| 1225 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 1226 | return OVERSCROLL_DAMP_FACTOR * f; |
| 1227 | } |
| 1228 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1229 | @Override |
| 1230 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 1231 | // Skip touch handling if there are no pages to swipe |
| 1232 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 1233 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1234 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1235 | |
| 1236 | final int action = ev.getAction(); |
| 1237 | |
| 1238 | switch (action & MotionEvent.ACTION_MASK) { |
| 1239 | case MotionEvent.ACTION_DOWN: |
| 1240 | /* |
| 1241 | * If being flinged and user touches, stop the fling. isFinished |
| 1242 | * will be false if being flinged. |
| 1243 | */ |
| 1244 | if (!mScroller.isFinished()) { |
| 1245 | mScroller.abortAnimation(); |
| 1246 | } |
| 1247 | |
| 1248 | // Remember where the motion event started |
| 1249 | mDownMotionX = mLastMotionX = ev.getX(); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1250 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1251 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1252 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1253 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1254 | pageBeginMoving(); |
| 1255 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1256 | break; |
| 1257 | |
| 1258 | case MotionEvent.ACTION_MOVE: |
| 1259 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1260 | // Scroll to follow the motion event |
| 1261 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 1262 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1263 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1264 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1265 | mTotalMotionX += Math.abs(deltaX); |
| 1266 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1267 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1268 | // keep the remainder because we are actually testing if we've moved from the last |
| 1269 | // scrolled position (which is discrete). |
| 1270 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1271 | mTouchX += deltaX; |
| 1272 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1273 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1274 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1275 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1276 | } else { |
| 1277 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1278 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1279 | mLastMotionX = x; |
| 1280 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1281 | } else { |
| 1282 | awakenScrollBars(); |
| 1283 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1284 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1285 | determineScrollingStart(ev); |
| 1286 | } |
| 1287 | break; |
| 1288 | |
| 1289 | case MotionEvent.ACTION_UP: |
| 1290 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1291 | final int activePointerId = mActivePointerId; |
| 1292 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1293 | final float x = ev.getX(pointerIndex); |
| 1294 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1295 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1296 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1297 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1298 | final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage)); |
| 1299 | boolean isSignificantMove = Math.abs(deltaX) > pageWidth * |
| 1300 | SIGNIFICANT_MOVE_THRESHOLD; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1301 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1302 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1303 | |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1304 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1305 | Math.abs(velocityX) > mFlingThresholdVelocity; |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1306 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1307 | // In the case that the page is moved far to one direction and then is flung |
| 1308 | // in the opposite direction, we use a threshold to determine whether we should |
| 1309 | // just return to the starting page, or if we should skip one further. |
| 1310 | boolean returnToOriginalPage = false; |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1311 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
Adam Cohen | 00481b3 | 2011-11-18 12:03:48 -0800 | [diff] [blame] | 1312 | Math.signum(velocityX) != Math.signum(deltaX) && isFling) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1313 | returnToOriginalPage = true; |
| 1314 | } |
| 1315 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1316 | int finalPage; |
| 1317 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1318 | // test for a large move if a fling has been registered. That is, a large |
| 1319 | // move to the left and fling to the right will register as a fling to the right. |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1320 | final boolean isRtl = isLayoutRtl(); |
| 1321 | boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0; |
| 1322 | boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0; |
| 1323 | if (((isSignificantMove && !isDeltaXLeft && !isFling) || |
| 1324 | (isFling && !isVelocityXLeft)) && mCurrentPage > 0) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1325 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1326 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1327 | } else if (((isSignificantMove && isDeltaXLeft && !isFling) || |
| 1328 | (isFling && isVelocityXLeft)) && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1329 | mCurrentPage < getChildCount() - 1) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1330 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1331 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1332 | } else { |
| 1333 | snapToDestination(); |
| 1334 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1335 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1336 | // at this point we have not moved beyond the touch slop |
| 1337 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1338 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1339 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1340 | if (nextPage != mCurrentPage) { |
| 1341 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1342 | } else { |
| 1343 | snapToDestination(); |
| 1344 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1345 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1346 | // at this point we have not moved beyond the touch slop |
| 1347 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1348 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1349 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1350 | if (nextPage != mCurrentPage) { |
| 1351 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1352 | } else { |
| 1353 | snapToDestination(); |
| 1354 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1355 | } else { |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1356 | onUnhandledTap(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1357 | } |
| 1358 | mTouchState = TOUCH_STATE_REST; |
| 1359 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1360 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1361 | break; |
| 1362 | |
| 1363 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1364 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1365 | snapToDestination(); |
| 1366 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1367 | mTouchState = TOUCH_STATE_REST; |
| 1368 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1369 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1370 | break; |
| 1371 | |
| 1372 | case MotionEvent.ACTION_POINTER_UP: |
| 1373 | onSecondaryPointerUp(ev); |
| 1374 | break; |
| 1375 | } |
| 1376 | |
| 1377 | return true; |
| 1378 | } |
| 1379 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1380 | @Override |
| 1381 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1382 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1383 | switch (event.getAction()) { |
| 1384 | case MotionEvent.ACTION_SCROLL: { |
| 1385 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1386 | final float vscroll; |
| 1387 | final float hscroll; |
| 1388 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1389 | vscroll = 0; |
| 1390 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1391 | } else { |
| 1392 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1393 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1394 | } |
| 1395 | if (hscroll != 0 || vscroll != 0) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1396 | boolean isForwardScroll = isLayoutRtl() ? (hscroll < 0 || vscroll < 0) |
| 1397 | : (hscroll > 0 || vscroll > 0); |
| 1398 | if (isForwardScroll) { |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1399 | scrollRight(); |
| 1400 | } else { |
| 1401 | scrollLeft(); |
| 1402 | } |
| 1403 | return true; |
| 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | return super.onGenericMotionEvent(event); |
| 1409 | } |
| 1410 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1411 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1412 | if (mVelocityTracker == null) { |
| 1413 | mVelocityTracker = VelocityTracker.obtain(); |
| 1414 | } |
| 1415 | mVelocityTracker.addMovement(ev); |
| 1416 | } |
| 1417 | |
| 1418 | private void releaseVelocityTracker() { |
| 1419 | if (mVelocityTracker != null) { |
| 1420 | mVelocityTracker.recycle(); |
| 1421 | mVelocityTracker = null; |
| 1422 | } |
| 1423 | } |
| 1424 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1425 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1426 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1427 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1428 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1429 | if (pointerId == mActivePointerId) { |
| 1430 | // This was our active pointer going up. Choose a new |
| 1431 | // active pointer and adjust accordingly. |
| 1432 | // TODO: Make this decision more intelligent. |
| 1433 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1434 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1435 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1436 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1437 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1438 | if (mVelocityTracker != null) { |
| 1439 | mVelocityTracker.clear(); |
| 1440 | } |
| 1441 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
Michael Jurka | d771c96 | 2011-08-09 15:00:48 -0700 | [diff] [blame] | 1444 | protected void onUnhandledTap(MotionEvent ev) {} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1445 | |
| 1446 | @Override |
| 1447 | public void requestChildFocus(View child, View focused) { |
| 1448 | super.requestChildFocus(child, focused); |
Adam Cohen | ae4f155 | 2011-10-20 00:15:42 -0700 | [diff] [blame] | 1449 | int page = indexToPage(indexOfChild(child)); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1450 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1451 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1455 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1456 | final boolean isRtl = isLayoutRtl(); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1457 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1458 | int left; |
| 1459 | int right; |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1460 | final int startIndex = isRtl ? childCount - 1 : 0; |
| 1461 | final int endIndex = isRtl ? -1 : childCount; |
| 1462 | final int delta = isRtl ? -1 : 1; |
| 1463 | for (int i = startIndex; i != endIndex; i += delta) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1464 | left = getRelativeChildOffset(i); |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1465 | right = (left + getScaledMeasuredWidth(getPageAt(i))); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1466 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1467 | return i; |
| 1468 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1469 | } |
| 1470 | return -1; |
| 1471 | } |
| 1472 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1473 | protected int getChildWidth(int index) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1474 | // This functions are called enough times that it actually makes a difference in the |
| 1475 | // profiler -- so just inline the max() here |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1476 | final int measuredWidth = getPageAt(index).getMeasuredWidth(); |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1477 | final int minWidth = mMinimumWidth; |
| 1478 | return (minWidth > measuredWidth) ? minWidth : measuredWidth; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1481 | int getPageNearestToCenterOfScreen() { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1482 | int minDistanceFromScreenCenter = Integer.MAX_VALUE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1483 | int minDistanceFromScreenCenterIndex = -1; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1484 | int screenCenter = getScrollX() + (getMeasuredWidth() / 2); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1485 | final int childCount = getChildCount(); |
| 1486 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1487 | View layout = (View) getPageAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1488 | int childWidth = getScaledMeasuredWidth(layout); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1489 | int halfChildWidth = (childWidth / 2); |
| 1490 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 1491 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1492 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1493 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1494 | minDistanceFromScreenCenterIndex = i; |
| 1495 | } |
| 1496 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1497 | return minDistanceFromScreenCenterIndex; |
| 1498 | } |
| 1499 | |
| 1500 | protected void snapToDestination() { |
| 1501 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1504 | private static class ScrollInterpolator implements Interpolator { |
| 1505 | public ScrollInterpolator() { |
| 1506 | } |
| 1507 | |
| 1508 | public float getInterpolation(float t) { |
| 1509 | t -= 1.0f; |
| 1510 | return t*t*t*t*t + 1; |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1515 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1516 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1517 | // of travel has on the overall snap duration. |
| 1518 | float distanceInfluenceForSnapDuration(float f) { |
| 1519 | f -= 0.5f; // center the values about 0. |
| 1520 | f *= 0.3f * Math.PI / 2.0f; |
| 1521 | return (float) Math.sin(f); |
| 1522 | } |
| 1523 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1524 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1525 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 1526 | int halfScreenSize = getMeasuredWidth() / 2; |
| 1527 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1528 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1529 | if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): " |
| 1530 | + getMeasuredWidth() + ", " + getChildWidth(whichPage)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1531 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1532 | int delta = newX - mUnboundedScrollX; |
| 1533 | int duration = 0; |
| 1534 | |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1535 | if (Math.abs(velocity) < mMinFlingVelocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1536 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1537 | // as opposed to an apparent physical response to flinging |
| 1538 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1539 | return; |
| 1540 | } |
| 1541 | |
| 1542 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1543 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1544 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1545 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1546 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1547 | float distance = halfScreenSize + halfScreenSize * |
| 1548 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1549 | |
| 1550 | velocity = Math.abs(velocity); |
Adam Cohen | 265b9a6 | 2011-12-07 14:37:18 -0800 | [diff] [blame] | 1551 | velocity = Math.max(mMinSnapVelocity, velocity); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1552 | |
| 1553 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1554 | // 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] | 1555 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1556 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Chet Haase | 97687ef | 2012-10-25 16:01:11 -0700 | [diff] [blame] | 1557 | duration = Math.min(duration, MAX_PAGE_SNAP_DURATION); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1558 | |
| 1559 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1563 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1564 | } |
| 1565 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1566 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1567 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1568 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1569 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1570 | if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 1571 | + getChildWidth(whichPage)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1572 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1573 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1574 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1575 | snapToPage(whichPage, delta, duration); |
| 1576 | } |
| 1577 | |
| 1578 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1579 | mNextPage = whichPage; |
| 1580 | |
| 1581 | View focusedChild = getFocusedChild(); |
| 1582 | if (focusedChild != null && whichPage != mCurrentPage && |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1583 | focusedChild == getPageAt(mCurrentPage)) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1584 | focusedChild.clearFocus(); |
| 1585 | } |
| 1586 | |
| 1587 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1588 | awakenScrollBars(duration); |
| 1589 | if (duration == 0) { |
| 1590 | duration = Math.abs(delta); |
| 1591 | } |
| 1592 | |
| 1593 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1594 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1595 | |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1596 | // Load associated pages immediately if someone else is handling the scroll, otherwise defer |
| 1597 | // loading associated pages until the scroll settles |
| 1598 | if (mDeferScrollUpdate) { |
| 1599 | loadAssociatedPages(mNextPage); |
| 1600 | } else { |
Winson Chung | 4e07654 | 2011-06-23 13:04:10 -0700 | [diff] [blame] | 1601 | mDeferLoadAssociatedPagesUntilScrollCompletes = true; |
Winson Chung | b44b524 | 2011-06-13 11:32:14 -0700 | [diff] [blame] | 1602 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1603 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1604 | invalidate(); |
| 1605 | } |
| 1606 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1607 | public void scrollLeft() { |
| 1608 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1609 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1610 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1611 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | public void scrollRight() { |
| 1616 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1617 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1618 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1619 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1623 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1624 | int result = -1; |
| 1625 | if (v != null) { |
| 1626 | ViewParent vp = v.getParent(); |
| 1627 | int count = getChildCount(); |
| 1628 | for (int i = 0; i < count; i++) { |
Adam Cohen | 22f823d | 2011-09-01 17:22:18 -0700 | [diff] [blame] | 1629 | if (vp == getPageAt(i)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1630 | return i; |
| 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | return result; |
| 1635 | } |
| 1636 | |
| 1637 | /** |
| 1638 | * @return True is long presses are still allowed for the current touch |
| 1639 | */ |
| 1640 | public boolean allowLongPress() { |
| 1641 | return mAllowLongPress; |
| 1642 | } |
| 1643 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1644 | /** |
| 1645 | * Set true to allow long-press events to be triggered, usually checked by |
| 1646 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1647 | */ |
| 1648 | public void setAllowLongPress(boolean allowLongPress) { |
| 1649 | mAllowLongPress = allowLongPress; |
| 1650 | } |
| 1651 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1652 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1653 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1654 | |
| 1655 | SavedState(Parcelable superState) { |
| 1656 | super(superState); |
| 1657 | } |
| 1658 | |
| 1659 | private SavedState(Parcel in) { |
| 1660 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1661 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | @Override |
| 1665 | public void writeToParcel(Parcel out, int flags) { |
| 1666 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1667 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1671 | new Parcelable.Creator<SavedState>() { |
| 1672 | public SavedState createFromParcel(Parcel in) { |
| 1673 | return new SavedState(in); |
| 1674 | } |
| 1675 | |
| 1676 | public SavedState[] newArray(int size) { |
| 1677 | return new SavedState[size]; |
| 1678 | } |
| 1679 | }; |
| 1680 | } |
| 1681 | |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1682 | protected void loadAssociatedPages(int page) { |
| 1683 | loadAssociatedPages(page, false); |
| 1684 | } |
| 1685 | protected void loadAssociatedPages(int page, boolean immediateAndOnly) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1686 | if (mContentIsRefreshable) { |
| 1687 | final int count = getChildCount(); |
| 1688 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1689 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1690 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1691 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 1692 | + upperPageBound); |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 1693 | // First, clear any pages that should no longer be loaded |
| 1694 | for (int i = 0; i < count; ++i) { |
| 1695 | Page layout = (Page) getPageAt(i); |
Michael Jurka | 2a4b1a8 | 2011-12-07 14:00:02 -0800 | [diff] [blame] | 1696 | if ((i < lowerPageBound) || (i > upperPageBound)) { |
Michael Jurka | 0cad111 | 2011-11-16 20:43:29 -0800 | [diff] [blame] | 1697 | if (layout.getPageChildCount() > 0) { |
| 1698 | layout.removeAllViewsOnPage(); |
| 1699 | } |
| 1700 | mDirtyPageContent.set(i, true); |
| 1701 | } |
| 1702 | } |
| 1703 | // Next, load any new pages |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1704 | for (int i = 0; i < count; ++i) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1705 | if ((i != page) && immediateAndOnly) { |
| 1706 | continue; |
| 1707 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1708 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1709 | if (mDirtyPageContent.get(i)) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1710 | syncPageItems(i, (i == page) && immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1711 | mDirtyPageContent.set(i, false); |
| 1712 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1713 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1714 | } |
| 1715 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
| 1718 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1719 | protected int getAssociatedLowerPageBound(int page) { |
| 1720 | return Math.max(0, page - 1); |
| 1721 | } |
| 1722 | protected int getAssociatedUpperPageBound(int page) { |
| 1723 | final int count = getChildCount(); |
| 1724 | return Math.min(page + 1, count - 1); |
| 1725 | } |
| 1726 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1727 | /** |
| 1728 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1729 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1730 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1731 | * and therefore, individual page items do not need to be updated in this method. |
| 1732 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1733 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1734 | |
| 1735 | /** |
| 1736 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1737 | * the page can be reused, then they should be updated within this method. |
| 1738 | */ |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1739 | public abstract void syncPageItems(int page, boolean immediate); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1740 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 1741 | protected void invalidatePageData() { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1742 | invalidatePageData(-1, false); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1743 | } |
| 1744 | protected void invalidatePageData(int currentPage) { |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1745 | invalidatePageData(currentPage, false); |
| 1746 | } |
| 1747 | protected void invalidatePageData(int currentPage, boolean immediateAndOnly) { |
Winson Chung | f0ea4d3 | 2011-06-06 14:27:16 -0700 | [diff] [blame] | 1748 | if (!mIsDataReady) { |
| 1749 | return; |
| 1750 | } |
| 1751 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1752 | if (mContentIsRefreshable) { |
Adam Cohen | 0cd3b64 | 2011-10-14 14:58:00 -0700 | [diff] [blame] | 1753 | // Force all scrolling-related behavior to end |
| 1754 | mScroller.forceFinished(true); |
| 1755 | mNextPage = INVALID_PAGE; |
| 1756 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1757 | // Update all the pages |
| 1758 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1759 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1760 | // We must force a measure after we've loaded the pages to update the content width and |
| 1761 | // to determine the full scroll width |
| 1762 | measure(MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY), |
| 1763 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY)); |
| 1764 | |
| 1765 | // Set a new page as the current page if necessary |
| 1766 | if (currentPage > -1) { |
Winson Chung | 5afbf7b | 2011-07-25 11:53:08 -0700 | [diff] [blame] | 1767 | setCurrentPage(Math.min(getPageCount() - 1, currentPage)); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1770 | // Mark each of the pages as dirty |
| 1771 | final int count = getChildCount(); |
| 1772 | mDirtyPageContent.clear(); |
| 1773 | for (int i = 0; i < count; ++i) { |
| 1774 | mDirtyPageContent.add(true); |
| 1775 | } |
| 1776 | |
| 1777 | // Load any pages that are necessary for the current window of views |
Winson Chung | f314b0e | 2011-08-16 11:54:27 -0700 | [diff] [blame] | 1778 | loadAssociatedPages(mCurrentPage, immediateAndOnly); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1779 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1780 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1781 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1782 | |
Michael Jurka | afaa050 | 2011-12-13 18:22:50 -0800 | [diff] [blame] | 1783 | protected View getScrollingIndicator() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1784 | // We use mHasScrollIndicator to prevent future lookups if there is no sibling indicator |
| 1785 | // found |
| 1786 | if (mHasScrollIndicator && mScrollIndicator == null) { |
| 1787 | ViewGroup parent = (ViewGroup) getParent(); |
Winson Chung | a128a7b | 2012-04-30 15:23:15 -0700 | [diff] [blame] | 1788 | if (parent != null) { |
| 1789 | mScrollIndicator = (View) (parent.findViewById(R.id.paged_view_indicator)); |
| 1790 | mHasScrollIndicator = mScrollIndicator != null; |
| 1791 | if (mHasScrollIndicator) { |
| 1792 | mScrollIndicator.setVisibility(View.VISIBLE); |
| 1793 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | return mScrollIndicator; |
| 1797 | } |
| 1798 | |
| 1799 | protected boolean isScrollingIndicatorEnabled() { |
Michael Jurka | b1dfe25 | 2012-09-26 10:53:55 -0700 | [diff] [blame] | 1800 | return true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1803 | Runnable hideScrollingIndicatorRunnable = new Runnable() { |
| 1804 | @Override |
| 1805 | public void run() { |
| 1806 | hideScrollingIndicator(false); |
| 1807 | } |
| 1808 | }; |
Michael Jurka | b737ee6 | 2011-11-15 15:57:22 -0800 | [diff] [blame] | 1809 | protected void flashScrollingIndicator(boolean animated) { |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1810 | removeCallbacks(hideScrollingIndicatorRunnable); |
Michael Jurka | b737ee6 | 2011-11-15 15:57:22 -0800 | [diff] [blame] | 1811 | showScrollingIndicator(!animated); |
Winson Chung | 5a80835 | 2011-06-27 19:08:49 -0700 | [diff] [blame] | 1812 | postDelayed(hideScrollingIndicatorRunnable, sScrollIndicatorFlashDuration); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1815 | protected void showScrollingIndicator(boolean immediately) { |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1816 | mShouldShowScrollIndicator = true; |
| 1817 | mShouldShowScrollIndicatorImmediately = true; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1818 | if (getChildCount() <= 1) return; |
| 1819 | if (!isScrollingIndicatorEnabled()) return; |
| 1820 | |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1821 | mShouldShowScrollIndicator = false; |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1822 | getScrollingIndicator(); |
| 1823 | if (mScrollIndicator != null) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1824 | // Fade the indicator in |
| 1825 | updateScrollingIndicatorPosition(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1826 | mScrollIndicator.setVisibility(View.VISIBLE); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1827 | cancelScrollingIndicatorAnimations(); |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 1828 | if (immediately || mScrollingPaused) { |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1829 | mScrollIndicator.setAlpha(1f); |
| 1830 | } else { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1831 | mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 1f); |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1832 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeInDuration); |
| 1833 | mScrollIndicatorAnimator.start(); |
| 1834 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1838 | protected void cancelScrollingIndicatorAnimations() { |
| 1839 | if (mScrollIndicatorAnimator != null) { |
| 1840 | mScrollIndicatorAnimator.cancel(); |
| 1841 | } |
| 1842 | } |
| 1843 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1844 | protected void hideScrollingIndicator(boolean immediately) { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1845 | if (getChildCount() <= 1) return; |
| 1846 | if (!isScrollingIndicatorEnabled()) return; |
| 1847 | |
| 1848 | getScrollingIndicator(); |
| 1849 | if (mScrollIndicator != null) { |
| 1850 | // Fade the indicator out |
| 1851 | updateScrollingIndicatorPosition(); |
Adam Cohen | 21b4110 | 2011-11-01 17:29:52 -0700 | [diff] [blame] | 1852 | cancelScrollingIndicatorAnimations(); |
Chet Haase | bc2f082 | 2012-10-26 17:59:53 -0700 | [diff] [blame] | 1853 | if (immediately || mScrollingPaused) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1854 | mScrollIndicator.setVisibility(View.INVISIBLE); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1855 | mScrollIndicator.setAlpha(0f); |
| 1856 | } else { |
Michael Jurka | 2ecf995 | 2012-06-18 12:52:28 -0700 | [diff] [blame] | 1857 | mScrollIndicatorAnimator = LauncherAnimUtils.ofFloat(mScrollIndicator, "alpha", 0f); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1858 | mScrollIndicatorAnimator.setDuration(sScrollIndicatorFadeOutDuration); |
| 1859 | mScrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() { |
| 1860 | private boolean cancelled = false; |
| 1861 | @Override |
| 1862 | public void onAnimationCancel(android.animation.Animator animation) { |
| 1863 | cancelled = true; |
| 1864 | } |
| 1865 | @Override |
| 1866 | public void onAnimationEnd(Animator animation) { |
| 1867 | if (!cancelled) { |
Michael Jurka | 81efbad | 2011-11-03 13:50:45 -0700 | [diff] [blame] | 1868 | mScrollIndicator.setVisibility(View.INVISIBLE); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | }); |
| 1872 | mScrollIndicatorAnimator.start(); |
| 1873 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1874 | } |
| 1875 | } |
| 1876 | |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1877 | /** |
| 1878 | * To be overridden by subclasses to determine whether the scroll indicator should stretch to |
| 1879 | * fill its space on the track or not. |
| 1880 | */ |
| 1881 | protected boolean hasElasticScrollIndicator() { |
Winson Chung | dea74b7 | 2011-09-13 18:06:43 -0700 | [diff] [blame] | 1882 | return true; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1883 | } |
| 1884 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1885 | private void updateScrollingIndicator() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1886 | if (getChildCount() <= 1) return; |
| 1887 | if (!isScrollingIndicatorEnabled()) return; |
| 1888 | |
| 1889 | getScrollingIndicator(); |
| 1890 | if (mScrollIndicator != null) { |
| 1891 | updateScrollingIndicatorPosition(); |
| 1892 | } |
Michael Jurka | bed61d2 | 2012-02-14 22:51:29 -0800 | [diff] [blame] | 1893 | if (mShouldShowScrollIndicator) { |
| 1894 | showScrollingIndicator(mShouldShowScrollIndicatorImmediately); |
| 1895 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1896 | } |
| 1897 | |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1898 | private void updateScrollingIndicatorPosition() { |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1899 | final boolean isRtl = isLayoutRtl(); |
Winson Chung | 649723c | 2011-07-06 20:41:23 -0700 | [diff] [blame] | 1900 | if (!isScrollingIndicatorEnabled()) return; |
Michael Jurka | 430e8a5 | 2011-08-08 15:52:14 -0700 | [diff] [blame] | 1901 | if (mScrollIndicator == null) return; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1902 | int numPages = getChildCount(); |
| 1903 | int pageWidth = getMeasuredWidth(); |
Winson Chung | f5f8cef | 2011-07-22 11:16:13 -0700 | [diff] [blame] | 1904 | int trackWidth = pageWidth - mScrollIndicatorPaddingLeft - mScrollIndicatorPaddingRight; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1905 | int indicatorWidth = mScrollIndicator.getMeasuredWidth() - |
| 1906 | mScrollIndicator.getPaddingLeft() - mScrollIndicator.getPaddingRight(); |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1907 | |
Winson Chung | 52aee60 | 2013-01-30 12:01:02 -0800 | [diff] [blame] | 1908 | float scrollPos = isRtl ? mMaxScrollX - getScrollX() : getScrollX(); |
| 1909 | float offset = Math.max(0f, Math.min(1f, (float) scrollPos / mMaxScrollX)); |
| 1910 | if (isRtl) { |
| 1911 | offset = 1f - offset; |
| 1912 | } |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1913 | int indicatorSpace = trackWidth / numPages; |
Winson Chung | ae890b8 | 2011-09-13 18:08:54 -0700 | [diff] [blame] | 1914 | int indicatorPos = (int) (offset * (trackWidth - indicatorSpace)) + mScrollIndicatorPaddingLeft; |
Winson Chung | 32174c8 | 2011-07-19 15:47:55 -0700 | [diff] [blame] | 1915 | if (hasElasticScrollIndicator()) { |
| 1916 | if (mScrollIndicator.getMeasuredWidth() != indicatorSpace) { |
| 1917 | mScrollIndicator.getLayoutParams().width = indicatorSpace; |
| 1918 | mScrollIndicator.requestLayout(); |
| 1919 | } |
| 1920 | } else { |
| 1921 | int indicatorCenterOffset = indicatorSpace / 2 - indicatorWidth / 2; |
| 1922 | indicatorPos += indicatorCenterOffset; |
| 1923 | } |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1924 | mScrollIndicator.setTranslationX(indicatorPos); |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1925 | } |
| 1926 | |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1927 | public void showScrollIndicatorTrack() { |
Winson Chung | 3ac74c5 | 2011-06-30 17:39:37 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | public void hideScrollIndicatorTrack() { |
Winson Chung | 007c698 | 2011-06-14 13:27:53 -0700 | [diff] [blame] | 1931 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1932 | |
| 1933 | /* Accessibility */ |
| 1934 | @Override |
| 1935 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 1936 | super.onInitializeAccessibilityNodeInfo(info); |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1937 | info.setScrollable(getPageCount() > 1); |
| 1938 | if (getCurrentPage() < getPageCount() - 1) { |
| 1939 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 1940 | } |
| 1941 | if (getCurrentPage() > 0) { |
| 1942 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 1943 | } |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | @Override |
| 1947 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 1948 | super.onInitializeAccessibilityEvent(event); |
| 1949 | event.setScrollable(true); |
| 1950 | if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { |
| 1951 | event.setFromIndex(mCurrentPage); |
| 1952 | event.setToIndex(mCurrentPage); |
| 1953 | event.setItemCount(getChildCount()); |
| 1954 | } |
| 1955 | } |
| 1956 | |
Svetoslav Ganov | 08055f6 | 2012-05-15 11:06:36 -0700 | [diff] [blame] | 1957 | @Override |
| 1958 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 1959 | if (super.performAccessibilityAction(action, arguments)) { |
| 1960 | return true; |
| 1961 | } |
| 1962 | switch (action) { |
| 1963 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 1964 | if (getCurrentPage() < getPageCount() - 1) { |
| 1965 | scrollRight(); |
| 1966 | return true; |
| 1967 | } |
| 1968 | } break; |
| 1969 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 1970 | if (getCurrentPage() > 0) { |
| 1971 | scrollLeft(); |
| 1972 | return true; |
| 1973 | } |
| 1974 | } break; |
| 1975 | } |
| 1976 | return false; |
| 1977 | } |
| 1978 | |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1979 | protected String getCurrentPageDescription() { |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 1980 | return String.format(getContext().getString(R.string.default_scroll_format), |
Winson Chung | 360e63f | 2012-04-27 13:48:05 -0700 | [diff] [blame] | 1981 | getNextPage() + 1, getChildCount()); |
Winson Chung | 6a0f57d | 2011-06-29 20:10:49 -0700 | [diff] [blame] | 1982 | } |
Winson Chung | d11265e | 2011-08-30 13:37:23 -0700 | [diff] [blame] | 1983 | |
| 1984 | @Override |
| 1985 | public boolean onHoverEvent(android.view.MotionEvent event) { |
| 1986 | return true; |
| 1987 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1988 | } |