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 | |
| 17 | package com.android.launcher2; |
| 18 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 19 | import java.util.ArrayList; |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 20 | |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 21 | import android.animation.Animator; |
| 22 | import android.animation.AnimatorInflater; |
| 23 | import android.animation.AnimatorListenerAdapter; |
| 24 | import android.animation.ObjectAnimator; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 26 | import android.content.res.TypedArray; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
| 28 | import android.graphics.Rect; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
| 31 | import android.util.AttributeSet; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 32 | import android.util.Log; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 33 | import android.view.ActionMode; |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 34 | import android.view.InputDevice; |
| 35 | import android.view.KeyEvent; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 36 | import android.view.MotionEvent; |
| 37 | import android.view.VelocityTracker; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewConfiguration; |
| 40 | import android.view.ViewGroup; |
| 41 | import android.view.ViewParent; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 42 | import android.view.animation.Interpolator; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 43 | import android.widget.Checkable; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 44 | import android.widget.Scroller; |
| 45 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 46 | import com.android.launcher.R; |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 47 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 48 | /** |
| 49 | * An abstraction of the original Workspace which supports browsing through a |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 50 | * sequential list of "pages" |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 51 | */ |
| 52 | public abstract class PagedView extends ViewGroup { |
| 53 | private static final String TAG = "PagedView"; |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 54 | private static final boolean DEBUG = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 55 | protected static final int INVALID_PAGE = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 56 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 57 | // 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] | 58 | private static final int MIN_LENGTH_FOR_FLING = 25; |
| 59 | // The min drag distance to trigger a page shift (regardless of velocity) |
| 60 | private static final int MIN_LENGTH_FOR_MOVE = 200; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 61 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 62 | private static final int PAGE_SNAP_ANIMATION_DURATION = 550; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 63 | protected static final float NANOTIME_DIV = 1000000000.0f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 64 | |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 65 | private static final float OVERSCROLL_DAMP_FACTOR = 0.14f; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 66 | private static final int MINIMUM_SNAP_VELOCITY = 2200; |
| 67 | private static final int MIN_FLING_VELOCITY = 250; |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 68 | private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 69 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 70 | // the velocity at which a fling gesture will cause us to snap to the next page |
| 71 | protected int mSnapVelocity = 500; |
| 72 | |
| 73 | protected float mSmoothingTime; |
| 74 | protected float mTouchX; |
| 75 | |
| 76 | protected boolean mFirstLayout = true; |
| 77 | |
| 78 | protected int mCurrentPage; |
| 79 | protected int mNextPage = INVALID_PAGE; |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 80 | protected int mRestorePage = -1; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 81 | protected int mMaxScrollX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 82 | protected Scroller mScroller; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 83 | private VelocityTracker mVelocityTracker; |
| 84 | |
| 85 | private float mDownMotionX; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 86 | protected float mLastMotionX; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 87 | protected float mLastMotionXRemainder; |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 88 | protected float mLastMotionY; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 89 | protected float mTotalMotionX; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 90 | private int mLastScreenCenter = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 91 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 92 | protected final static int TOUCH_STATE_REST = 0; |
| 93 | protected final static int TOUCH_STATE_SCROLLING = 1; |
| 94 | protected final static int TOUCH_STATE_PREV_PAGE = 2; |
| 95 | protected final static int TOUCH_STATE_NEXT_PAGE = 3; |
Adam Cohen | e45440e | 2010-10-14 18:33:38 -0700 | [diff] [blame] | 96 | protected final static float ALPHA_QUANTIZE_LEVEL = 0.0001f; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 97 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 98 | protected int mTouchState = TOUCH_STATE_REST; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 99 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 100 | protected OnLongClickListener mLongClickListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 101 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 102 | protected boolean mAllowLongPress = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 103 | |
Michael Jurka | 7426c42 | 2010-11-11 15:23:47 -0800 | [diff] [blame] | 104 | protected int mTouchSlop; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 105 | private int mPagingTouchSlop; |
| 106 | private int mMaximumVelocity; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 107 | private int mMinimumWidth; |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 108 | protected int mPageSpacing; |
| 109 | protected int mPageLayoutPaddingTop; |
| 110 | protected int mPageLayoutPaddingBottom; |
| 111 | protected int mPageLayoutPaddingLeft; |
| 112 | protected int mPageLayoutPaddingRight; |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 113 | protected int mPageLayoutWidthGap; |
| 114 | protected int mPageLayoutHeightGap; |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 115 | protected int mPageLayoutMaxHeight; |
Michael Jurka | 87b1490 | 2011-05-25 22:13:09 -0700 | [diff] [blame] | 116 | protected int mCellCountX = 0; |
| 117 | protected int mCellCountY = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 118 | protected boolean mCenterPagesVertically; |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 119 | protected boolean mAllowOverScroll = true; |
| 120 | protected int mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 121 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 122 | // 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] | 123 | protected float mLayoutScale = 1.0f; |
| 124 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 125 | protected static final int INVALID_POINTER = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 126 | |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 127 | protected int mActivePointerId = INVALID_POINTER; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 128 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 129 | private PageSwitchListener mPageSwitchListener; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 130 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 131 | private ArrayList<Boolean> mDirtyPageContent; |
| 132 | private boolean mDirtyPageAlpha; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 133 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 134 | // choice modes |
| 135 | protected static final int CHOICE_MODE_NONE = 0; |
| 136 | protected static final int CHOICE_MODE_SINGLE = 1; |
| 137 | // Multiple selection mode is not supported by all Launcher actions atm |
| 138 | protected static final int CHOICE_MODE_MULTIPLE = 2; |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 139 | |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 140 | protected int mChoiceMode; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 141 | private ActionMode mActionMode; |
| 142 | |
Winson Chung | 0499834 | 2011-01-05 13:54:43 -0800 | [diff] [blame] | 143 | // NOTE: This is a shared icon cache across all the PagedViews. Currently it is only used in |
| 144 | // AllApps and Customize, and allows them to share holographic icons for the application view |
| 145 | // (which is in both). |
| 146 | protected static PagedViewIconCache mPageViewIconCache = new PagedViewIconCache(); |
Winson Chung | 241c3b4 | 2010-08-25 16:53:03 -0700 | [diff] [blame] | 147 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 148 | // If true, syncPages and syncPageItems will be called to refresh pages |
| 149 | protected boolean mContentIsRefreshable = true; |
| 150 | |
| 151 | // If true, modify alpha of neighboring pages as user scrolls left/right |
| 152 | protected boolean mFadeInAdjacentScreens = true; |
| 153 | |
| 154 | // It true, use a different slop parameter (pagingTouchSlop = 2 * touchSlop) for deciding |
| 155 | // to switch to a new page |
| 156 | protected boolean mUsePagingTouchSlop = true; |
| 157 | |
| 158 | // If true, the subclass should directly update mScrollX itself in its computeScroll method |
| 159 | // (SmoothPagedView does this) |
| 160 | protected boolean mDeferScrollUpdate = false; |
| 161 | |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 162 | protected boolean mIsPageMoving = false; |
| 163 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 164 | public interface PageSwitchListener { |
| 165 | void onPageSwitch(View newPage, int newPageIndex); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 168 | public PagedView(Context context) { |
| 169 | this(context, null); |
| 170 | } |
| 171 | |
| 172 | public PagedView(Context context, AttributeSet attrs) { |
| 173 | this(context, attrs, 0); |
| 174 | } |
| 175 | |
| 176 | public PagedView(Context context, AttributeSet attrs, int defStyle) { |
| 177 | super(context, attrs, defStyle); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 178 | mChoiceMode = CHOICE_MODE_NONE; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 179 | |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 180 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 181 | R.styleable.PagedView, defStyle, 0); |
| 182 | mPageSpacing = a.getDimensionPixelSize(R.styleable.PagedView_pageSpacing, 0); |
| 183 | mPageLayoutPaddingTop = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 184 | R.styleable.PagedView_pageLayoutPaddingTop, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 185 | mPageLayoutPaddingBottom = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 186 | R.styleable.PagedView_pageLayoutPaddingBottom, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 187 | mPageLayoutPaddingLeft = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 188 | R.styleable.PagedView_pageLayoutPaddingLeft, 0); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 189 | mPageLayoutPaddingRight = a.getDimensionPixelSize( |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 190 | R.styleable.PagedView_pageLayoutPaddingRight, 0); |
Winson Chung | df4b83d | 2010-10-20 17:49:27 -0700 | [diff] [blame] | 191 | mPageLayoutWidthGap = a.getDimensionPixelSize( |
| 192 | R.styleable.PagedView_pageLayoutWidthGap, -1); |
| 193 | mPageLayoutHeightGap = a.getDimensionPixelSize( |
| 194 | R.styleable.PagedView_pageLayoutHeightGap, -1); |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 195 | mPageLayoutMaxHeight = a.getDimensionPixelSize( |
| 196 | R.styleable.PagedView_pageLayoutMaxHeight, -1); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 197 | a.recycle(); |
| 198 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 199 | setHapticFeedbackEnabled(false); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 200 | init(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Initializes various states for this workspace. |
| 205 | */ |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 206 | protected void init() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 207 | mDirtyPageContent = new ArrayList<Boolean>(); |
| 208 | mDirtyPageContent.ensureCapacity(32); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 209 | mScroller = new Scroller(getContext(), new ScrollInterpolator()); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 210 | mCurrentPage = 0; |
Winson Chung | 7da1025 | 2010-10-28 16:07:04 -0700 | [diff] [blame] | 211 | mCenterPagesVertically = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 212 | |
| 213 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
| 214 | mTouchSlop = configuration.getScaledTouchSlop(); |
| 215 | mPagingTouchSlop = configuration.getScaledPagingTouchSlop(); |
| 216 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 217 | } |
| 218 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 219 | public void setPageSwitchListener(PageSwitchListener pageSwitchListener) { |
| 220 | mPageSwitchListener = pageSwitchListener; |
| 221 | if (mPageSwitchListener != null) { |
| 222 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 227 | * Returns the index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 228 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 229 | * @return The index of the currently displayed page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 230 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 231 | int getCurrentPage() { |
| 232 | return mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 235 | int getPageCount() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 236 | return getChildCount(); |
| 237 | } |
| 238 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 239 | View getPageAt(int index) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 240 | return getChildAt(index); |
| 241 | } |
| 242 | |
| 243 | int getScrollWidth() { |
| 244 | return getWidth(); |
| 245 | } |
| 246 | |
Adam Cohen | be90934 | 2011-01-28 17:02:58 -0800 | [diff] [blame] | 247 | public int getTouchState() { |
| 248 | return mTouchState; |
| 249 | } |
| 250 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 251 | /** |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 252 | * Updates the scroll of the current page immediately to its final scroll position. We use this |
| 253 | * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of |
| 254 | * the previous tab page. |
| 255 | */ |
| 256 | protected void updateCurrentPageScroll() { |
| 257 | int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage); |
| 258 | scrollTo(newX, 0); |
| 259 | mScroller.setFinalX(newX); |
| 260 | } |
| 261 | |
| 262 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 263 | * Sets the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 264 | */ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 265 | void setCurrentPage(int currentPage) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 266 | if (!mScroller.isFinished()) { |
| 267 | mScroller.abortAnimation(); |
| 268 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 269 | // don't introduce any checks like mCurrentPage == currentPage here-- if we change the |
| 270 | // the default |
| 271 | if (getChildCount() == 0) { |
Patrick Dubroy | 72e0d34 | 2010-11-09 15:23:28 -0800 | [diff] [blame] | 272 | return; |
| 273 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 274 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 275 | mCurrentPage = Math.max(0, Math.min(currentPage, getPageCount() - 1)); |
Winson Chung | bbc60d8 | 2010-11-11 16:34:41 -0800 | [diff] [blame] | 276 | updateCurrentPageScroll(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 277 | notifyPageSwitchListener(); |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 278 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 281 | protected void notifyPageSwitchListener() { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 282 | if (mPageSwitchListener != null) { |
| 283 | mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 287 | protected void pageBeginMoving() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 288 | mIsPageMoving = true; |
| 289 | onPageBeginMoving(); |
| 290 | } |
| 291 | |
Michael Jurka | ce7e05f | 2011-02-01 22:02:35 -0800 | [diff] [blame] | 292 | protected void pageEndMoving() { |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 293 | onPageEndMoving(); |
| 294 | mIsPageMoving = false; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Adam Cohen | 26976d9 | 2011-03-22 15:33:33 -0700 | [diff] [blame] | 297 | protected boolean isPageMoving() { |
| 298 | return mIsPageMoving; |
| 299 | } |
| 300 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 301 | // a method that subclasses can override to add behavior |
Patrick Dubroy | 1262e36 | 2010-10-06 15:49:50 -0700 | [diff] [blame] | 302 | protected void onPageBeginMoving() { |
| 303 | } |
| 304 | |
| 305 | // a method that subclasses can override to add behavior |
| 306 | protected void onPageEndMoving() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 309 | /** |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 310 | * Registers the specified listener on each page contained in this workspace. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 311 | * |
| 312 | * @param l The listener used to respond to long clicks. |
| 313 | */ |
| 314 | @Override |
| 315 | public void setOnLongClickListener(OnLongClickListener l) { |
| 316 | mLongClickListener = l; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 317 | final int count = getPageCount(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 318 | for (int i = 0; i < count; i++) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 319 | getPageAt(i).setOnLongClickListener(l); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | @Override |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 324 | public void scrollBy(int x, int y) { |
| 325 | scrollTo(mUnboundedScrollX + x, mScrollY + y); |
| 326 | } |
| 327 | |
| 328 | @Override |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 329 | public void scrollTo(int x, int y) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 330 | mUnboundedScrollX = x; |
| 331 | |
| 332 | if (x < 0) { |
| 333 | super.scrollTo(0, y); |
| 334 | if (mAllowOverScroll) { |
| 335 | overScroll(x); |
| 336 | } |
| 337 | } else if (x > mMaxScrollX) { |
| 338 | super.scrollTo(mMaxScrollX, y); |
| 339 | if (mAllowOverScroll) { |
| 340 | overScroll(x - mMaxScrollX); |
| 341 | } |
| 342 | } else { |
| 343 | super.scrollTo(x, y); |
| 344 | } |
| 345 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 346 | mTouchX = x; |
| 347 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 348 | } |
| 349 | |
| 350 | // we moved this functionality to a helper function so SmoothPagedView can reuse it |
| 351 | protected boolean computeScrollHelper() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 352 | if (mScroller.computeScrollOffset()) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 353 | mDirtyPageAlpha = true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 354 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 355 | invalidate(); |
| 356 | return true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 357 | } else if (mNextPage != INVALID_PAGE) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 358 | mDirtyPageAlpha = true; |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 359 | mCurrentPage = Math.max(0, Math.min(mNextPage, getPageCount() - 1)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 360 | mNextPage = INVALID_PAGE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 361 | notifyPageSwitchListener(); |
Adam Cohen | 73aa975 | 2010-11-24 16:26:58 -0800 | [diff] [blame] | 362 | // We don't want to trigger a page end moving unless the page has settled |
| 363 | // and the user has stopped scrolling |
| 364 | if (mTouchState == TOUCH_STATE_REST) { |
| 365 | pageEndMoving(); |
| 366 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 367 | return true; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 368 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public void computeScroll() { |
| 374 | computeScrollHelper(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | @Override |
| 378 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 379 | final int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 380 | final int widthSize = MeasureSpec.getSize(widthMeasureSpec); |
| 381 | if (widthMode != MeasureSpec.EXACTLY) { |
| 382 | throw new IllegalStateException("Workspace can only be used in EXACTLY mode."); |
| 383 | } |
| 384 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 385 | /* Allow the height to be set as WRAP_CONTENT. This allows the particular case |
| 386 | * of the All apps view on XLarge displays to not take up more space then it needs. Width |
| 387 | * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect |
| 388 | * each page to have the same width. |
| 389 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 390 | final int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 391 | int heightSize = MeasureSpec.getSize(heightMeasureSpec); |
| 392 | int maxChildHeight = 0; |
| 393 | |
| 394 | final int verticalPadding = mPaddingTop + mPaddingBottom; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 395 | |
Michael Jurka | 36fcb74 | 2011-04-06 17:08:58 -0700 | [diff] [blame] | 396 | if (mPageLayoutMaxHeight != -1) { |
| 397 | heightSize = Math.min(mPageLayoutMaxHeight, heightSize); |
| 398 | } |
| 399 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 400 | // The children are given the same width and height as the workspace |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 401 | // unless they were set to WRAP_CONTENT |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 402 | if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 403 | final int childCount = getChildCount(); |
| 404 | for (int i = 0; i < childCount; i++) { |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 405 | // disallowing padding in paged view (just pass 0) |
| 406 | final View child = getChildAt(i); |
| 407 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 408 | |
| 409 | int childWidthMode; |
| 410 | if (lp.width == LayoutParams.WRAP_CONTENT) { |
| 411 | childWidthMode = MeasureSpec.AT_MOST; |
| 412 | } else { |
| 413 | childWidthMode = MeasureSpec.EXACTLY; |
| 414 | } |
| 415 | |
| 416 | int childHeightMode; |
| 417 | if (lp.height == LayoutParams.WRAP_CONTENT) { |
| 418 | childHeightMode = MeasureSpec.AT_MOST; |
| 419 | } else { |
| 420 | childHeightMode = MeasureSpec.EXACTLY; |
| 421 | } |
| 422 | |
| 423 | final int childWidthMeasureSpec = |
| 424 | MeasureSpec.makeMeasureSpec(widthSize, childWidthMode); |
| 425 | final int childHeightMeasureSpec = |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 426 | MeasureSpec.makeMeasureSpec(heightSize - verticalPadding, childHeightMode); |
Michael Jurka | 5f1c509 | 2010-09-03 14:15:02 -0700 | [diff] [blame] | 427 | |
| 428 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 429 | maxChildHeight = Math.max(maxChildHeight, child.getMeasuredHeight()); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 430 | if (DEBUG) Log.d(TAG, "\tmeasure-child" + i + ": " + child.getMeasuredWidth() + ", " |
| 431 | + child.getMeasuredHeight()); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | if (heightMode == MeasureSpec.AT_MOST) { |
| 435 | heightSize = maxChildHeight + verticalPadding; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 436 | } |
Adam Cohen | faa2830 | 2010-11-19 12:02:18 -0800 | [diff] [blame] | 437 | if (childCount > 0) { |
| 438 | mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1); |
| 439 | } else { |
| 440 | mMaxScrollX = 0; |
| 441 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 442 | |
| 443 | setMeasuredDimension(widthSize, heightSize); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 446 | protected void scrollToNewPageWithoutMovingPages(int newCurrentPage) { |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 447 | int newX = getChildOffset(newCurrentPage) - getRelativeChildOffset(newCurrentPage); |
| 448 | int delta = newX - mScrollX; |
| 449 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 450 | final int pageCount = getChildCount(); |
| 451 | for (int i = 0; i < pageCount; i++) { |
| 452 | View page = (View) getChildAt(i); |
| 453 | page.setX(page.getX() + delta); |
Michael Jurka | af91de0 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 454 | } |
| 455 | setCurrentPage(newCurrentPage); |
| 456 | } |
| 457 | |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 458 | // A layout scale of 1.0f assumes that the pages, in their unshrunken state, have a |
| 459 | // 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] | 460 | // tightens the layout accordingly |
| 461 | public void setLayoutScale(float childrenScale) { |
| 462 | mLayoutScale = childrenScale; |
| 463 | |
| 464 | // Now we need to do a re-layout, but preserving absolute X and Y coordinates |
| 465 | int childCount = getChildCount(); |
| 466 | float childrenX[] = new float[childCount]; |
| 467 | float childrenY[] = new float[childCount]; |
| 468 | for (int i = 0; i < childCount; i++) { |
| 469 | final View child = getChildAt(i); |
| 470 | childrenX[i] = child.getX(); |
| 471 | childrenY[i] = child.getY(); |
| 472 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 473 | // Trigger a full re-layout (never just call onLayout directly!) |
| 474 | int widthSpec = MeasureSpec.makeMeasureSpec(getMeasuredWidth(), MeasureSpec.EXACTLY); |
| 475 | int heightSpec = MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.EXACTLY); |
| 476 | requestLayout(); |
| 477 | measure(widthSpec, heightSpec); |
| 478 | layout(mLeft, mTop, mRight, mBottom); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 479 | for (int i = 0; i < childCount; i++) { |
| 480 | final View child = getChildAt(i); |
| 481 | child.setX(childrenX[i]); |
| 482 | child.setY(childrenY[i]); |
| 483 | } |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 484 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 485 | // Also, the page offset has changed (since the pages are now smaller); |
| 486 | // update the page offset, but again preserving absolute X and Y coordinates |
Michael Jurka | 8c920dd | 2011-01-20 14:16:56 -0800 | [diff] [blame] | 487 | scrollToNewPageWithoutMovingPages(mCurrentPage); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 490 | @Override |
Michael Jurka | 28750fb | 2010-09-24 17:43:49 -0700 | [diff] [blame] | 491 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 492 | if (DEBUG) Log.d(TAG, "PagedView.onLayout()"); |
Michael Jurka | cfc6294 | 2010-09-14 14:01:07 -0700 | [diff] [blame] | 493 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 494 | setHorizontalScrollBarEnabled(false); |
| 495 | int newX = getChildOffset(mCurrentPage) - getRelativeChildOffset(mCurrentPage); |
| 496 | scrollTo(newX, 0); |
| 497 | mScroller.setFinalX(newX); |
| 498 | setHorizontalScrollBarEnabled(true); |
| 499 | mFirstLayout = false; |
| 500 | } |
| 501 | |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 502 | final int verticalPadding = mPaddingTop + mPaddingBottom; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 503 | final int childCount = getChildCount(); |
| 504 | int childLeft = 0; |
| 505 | if (childCount > 0) { |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 506 | if (DEBUG) Log.d(TAG, "getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 507 | + getChildWidth(0)); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 508 | childLeft = getRelativeChildOffset(0); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | for (int i = 0; i < childCount; i++) { |
| 512 | final View child = getChildAt(i); |
| 513 | if (child.getVisibility() != View.GONE) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 514 | final int childWidth = getScaledMeasuredWidth(child); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 515 | final int childHeight = child.getMeasuredHeight(); |
| 516 | int childTop = mPaddingTop; |
| 517 | if (mCenterPagesVertically) { |
| 518 | childTop += ((getMeasuredHeight() - verticalPadding) - childHeight) / 2; |
| 519 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 520 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 521 | if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop); |
Adam Lesinski | 6b879f0 | 2010-11-04 16:15:23 -0700 | [diff] [blame] | 522 | child.layout(childLeft, childTop, |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 523 | childLeft + child.getMeasuredWidth(), childTop + childHeight); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 524 | childLeft += childWidth + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 527 | if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < getChildCount()) { |
| 528 | mFirstLayout = false; |
| 529 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 530 | } |
| 531 | |
Winson Chung | 0392977 | 2011-02-23 17:07:10 -0800 | [diff] [blame] | 532 | protected void forceUpdateAdjacentPagesAlpha() { |
| 533 | mDirtyPageAlpha = true; |
| 534 | updateAdjacentPagesAlpha(); |
| 535 | } |
| 536 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 537 | protected void updateAdjacentPagesAlpha() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 538 | if (mFadeInAdjacentScreens) { |
| 539 | if (mDirtyPageAlpha || (mTouchState == TOUCH_STATE_SCROLLING) || !mScroller.isFinished()) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 540 | int screenWidth = getMeasuredWidth(); |
| 541 | int halfScreenSize = screenWidth / 2; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 542 | int screenCenter = mScrollX + halfScreenSize; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 543 | final int childCount = getChildCount(); |
| 544 | for (int i = 0; i < childCount; ++i) { |
| 545 | View layout = (View) getChildAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 546 | int childWidth = getScaledMeasuredWidth(layout); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 547 | int halfChildWidth = (childWidth / 2); |
| 548 | int childCenter = getChildOffset(i) + halfChildWidth; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 549 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 550 | // On the first layout, we may not have a width nor a proper offset, so for now |
| 551 | // we should just assume full page width (and calculate the offset according to |
| 552 | // that). |
| 553 | if (childWidth <= 0) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 554 | childWidth = screenWidth; |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 555 | childCenter = (i * childWidth) + (childWidth / 2); |
| 556 | } |
| 557 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 558 | int d = halfChildWidth; |
| 559 | int distanceFromScreenCenter = childCenter - screenCenter; |
| 560 | if (distanceFromScreenCenter > 0) { |
| 561 | if (i > 0) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 562 | d += getScaledMeasuredWidth(getChildAt(i - 1)) / 2; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 563 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 564 | } else { |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 565 | if (i < childCount - 1) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 566 | d += getScaledMeasuredWidth(getChildAt(i + 1)) / 2; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 567 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 568 | } |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 569 | d += mPageSpacing; |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 570 | |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 571 | // Preventing potential divide-by-zero |
| 572 | d = Math.max(1, d); |
| 573 | |
Winson Chung | e8878e3 | 2010-09-15 20:37:09 -0700 | [diff] [blame] | 574 | float dimAlpha = (float) (Math.abs(distanceFromScreenCenter)) / d; |
| 575 | dimAlpha = Math.max(0.0f, Math.min(1.0f, (dimAlpha * dimAlpha))); |
| 576 | float alpha = 1.0f - dimAlpha; |
| 577 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 578 | if (alpha < ALPHA_QUANTIZE_LEVEL) { |
| 579 | alpha = 0.0f; |
| 580 | } else if (alpha > 1.0f - ALPHA_QUANTIZE_LEVEL) { |
| 581 | alpha = 1.0f; |
| 582 | } |
| 583 | |
Michael Jurka | a40829a | 2011-02-16 18:02:45 -0800 | [diff] [blame] | 584 | // Due to the way we're setting alpha on our children in PagedViewCellLayout, |
| 585 | // this optimization causes alpha to not be properly updated sometimes (repro |
| 586 | // case: in xlarge mode, swipe to second page in All Apps, then click on "My |
| 587 | // Apps" tab. the page will have alpha 0 until you swipe it). Removing |
| 588 | // optimization fixes the issue, but we should fix this in a better manner |
Michael Jurka | cfdb096 | 2011-02-04 01:38:00 -0800 | [diff] [blame] | 589 | //if (Float.compare(alpha, layout.getAlpha()) != 0) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 590 | layout.setAlpha(alpha); |
Michael Jurka | cfdb096 | 2011-02-04 01:38:00 -0800 | [diff] [blame] | 591 | //} |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 592 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 593 | mDirtyPageAlpha = false; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 594 | } |
| 595 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 598 | protected void screenScrolled(int screenCenter) { |
| 599 | } |
| 600 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 601 | @Override |
| 602 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 603 | int halfScreenSize = getMeasuredWidth() / 2; |
| 604 | int screenCenter = mScrollX + halfScreenSize; |
| 605 | |
| 606 | if (screenCenter != mLastScreenCenter) { |
| 607 | screenScrolled(screenCenter); |
| 608 | updateAdjacentPagesAlpha(); |
| 609 | mLastScreenCenter = screenCenter; |
| 610 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 611 | |
| 612 | // Find out which screens are visible; as an optimization we only call draw on them |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 613 | // As an optimization, this code assumes that all pages have the same width as the 0th |
| 614 | // page. |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 615 | final int pageCount = getChildCount(); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 616 | if (pageCount > 0) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 617 | final int pageWidth = getScaledMeasuredWidth(getChildAt(0)); |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 618 | final int screenWidth = getMeasuredWidth(); |
| 619 | int x = getRelativeChildOffset(0) + pageWidth; |
| 620 | int leftScreen = 0; |
| 621 | int rightScreen = 0; |
| 622 | while (x <= mScrollX) { |
| 623 | leftScreen++; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 624 | x += getScaledMeasuredWidth(getChildAt(leftScreen)) + mPageSpacing; |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 625 | } |
| 626 | rightScreen = leftScreen; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 627 | while (x < mScrollX + screenWidth && rightScreen < pageCount) { |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 628 | rightScreen++; |
Winson Chung | c3f9f4f | 2010-12-14 17:25:59 -0800 | [diff] [blame] | 629 | if (rightScreen < pageCount) { |
| 630 | x += getScaledMeasuredWidth(getChildAt(rightScreen)) + mPageSpacing; |
| 631 | } |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 632 | } |
| 633 | rightScreen = Math.min(getChildCount() - 1, rightScreen); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 634 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 635 | final long drawingTime = getDrawingTime(); |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 636 | // Clip to the bounds |
| 637 | canvas.save(); |
| 638 | canvas.clipRect(mScrollX, mScrollY, mScrollX + mRight - mLeft, |
| 639 | mScrollY + mBottom - mTop); |
| 640 | |
Michael Jurka | c4fb917 | 2010-09-02 17:19:20 -0700 | [diff] [blame] | 641 | for (int i = leftScreen; i <= rightScreen; i++) { |
| 642 | drawChild(canvas, getChildAt(i), drawingTime); |
| 643 | } |
Winson Chung | 29d6fea | 2010-12-01 15:47:31 -0800 | [diff] [blame] | 644 | canvas.restore(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 645 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 650 | int page = indexOfChild(child); |
| 651 | if (page != mCurrentPage || !mScroller.isFinished()) { |
| 652 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 653 | return true; |
| 654 | } |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | @Override |
| 659 | protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 660 | int focusablePage; |
| 661 | if (mNextPage != INVALID_PAGE) { |
| 662 | focusablePage = mNextPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 663 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 664 | focusablePage = mCurrentPage; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 665 | } |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 666 | View v = getPageAt(focusablePage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 667 | if (v != null) { |
| 668 | v.requestFocus(direction, previouslyFocusedRect); |
| 669 | } |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | @Override |
| 674 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 675 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 676 | if (getCurrentPage() > 0) { |
| 677 | snapToPage(getCurrentPage() - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 678 | return true; |
| 679 | } |
| 680 | } else if (direction == View.FOCUS_RIGHT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 681 | if (getCurrentPage() < getPageCount() - 1) { |
| 682 | snapToPage(getCurrentPage() + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 683 | return true; |
| 684 | } |
| 685 | } |
| 686 | return super.dispatchUnhandledMove(focused, direction); |
| 687 | } |
| 688 | |
| 689 | @Override |
| 690 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 691 | if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) { |
| 692 | getPageAt(mCurrentPage).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 693 | } |
| 694 | if (direction == View.FOCUS_LEFT) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 695 | if (mCurrentPage > 0) { |
| 696 | getPageAt(mCurrentPage - 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 697 | } |
| 698 | } else if (direction == View.FOCUS_RIGHT){ |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 699 | if (mCurrentPage < getPageCount() - 1) { |
| 700 | getPageAt(mCurrentPage + 1).addFocusables(views, direction); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * 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] | 707 | * pass that along if it's on the current page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 708 | * |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 709 | * This happens when live folders requery, and if they're off page, they |
| 710 | * end up calling requestFocus, which pulls it on page. |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 711 | */ |
| 712 | @Override |
| 713 | public void focusableViewAvailable(View focused) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 714 | View current = getPageAt(mCurrentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 715 | View v = focused; |
| 716 | while (true) { |
| 717 | if (v == current) { |
| 718 | super.focusableViewAvailable(focused); |
| 719 | return; |
| 720 | } |
| 721 | if (v == this) { |
| 722 | return; |
| 723 | } |
| 724 | ViewParent parent = v.getParent(); |
| 725 | if (parent instanceof View) { |
| 726 | v = (View)v.getParent(); |
| 727 | } else { |
| 728 | return; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * {@inheritDoc} |
| 735 | */ |
| 736 | @Override |
| 737 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
| 738 | if (disallowIntercept) { |
| 739 | // We need to make sure to cancel our long press if |
| 740 | // a scrollable widget takes over touch events |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 741 | final View currentPage = getChildAt(mCurrentPage); |
| 742 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 743 | } |
| 744 | super.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 745 | } |
| 746 | |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 747 | /** |
| 748 | * Return true if a tap at (x, y) should trigger a flip to the previous page. |
| 749 | */ |
| 750 | protected boolean hitsPreviousPage(float x, float y) { |
| 751 | return (x < getRelativeChildOffset(mCurrentPage) - mPageSpacing); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Return true if a tap at (x, y) should trigger a flip to the next page. |
| 756 | */ |
| 757 | protected boolean hitsNextPage(float x, float y) { |
| 758 | return (x > (getMeasuredWidth() - getRelativeChildOffset(mCurrentPage) + mPageSpacing)); |
| 759 | } |
| 760 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 761 | @Override |
| 762 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 763 | /* |
| 764 | * This method JUST determines whether we want to intercept the motion. |
| 765 | * If we return true, onTouchEvent will be called and we do the actual |
| 766 | * scrolling there. |
| 767 | */ |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 768 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 769 | |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 770 | // Skip touch handling if there are no pages to swipe |
| 771 | if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev); |
| 772 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 773 | /* |
| 774 | * Shortcut the most recurring case: the user is in the dragging |
| 775 | * state and he is moving his finger. We want to intercept this |
| 776 | * motion. |
| 777 | */ |
| 778 | final int action = ev.getAction(); |
| 779 | if ((action == MotionEvent.ACTION_MOVE) && |
| 780 | (mTouchState == TOUCH_STATE_SCROLLING)) { |
| 781 | return true; |
| 782 | } |
| 783 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 784 | switch (action & MotionEvent.ACTION_MASK) { |
| 785 | case MotionEvent.ACTION_MOVE: { |
| 786 | /* |
| 787 | * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check |
| 788 | * whether the user has moved far enough from his original down touch. |
| 789 | */ |
Michael Jurka | 1ff706b | 2010-09-14 17:35:20 -0700 | [diff] [blame] | 790 | if (mActivePointerId != INVALID_POINTER) { |
| 791 | determineScrollingStart(ev); |
| 792 | break; |
| 793 | } |
| 794 | // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN |
| 795 | // event. in that case, treat the first occurence of a move event as a ACTION_DOWN |
| 796 | // i.e. fall through to the next case (don't break) |
| 797 | // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events |
| 798 | // 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] | 799 | } |
| 800 | |
| 801 | case MotionEvent.ACTION_DOWN: { |
| 802 | final float x = ev.getX(); |
| 803 | final float y = ev.getY(); |
| 804 | // Remember location of down touch |
| 805 | mDownMotionX = x; |
| 806 | mLastMotionX = x; |
| 807 | mLastMotionY = y; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 808 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 809 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 810 | mActivePointerId = ev.getPointerId(0); |
| 811 | mAllowLongPress = true; |
| 812 | |
| 813 | /* |
| 814 | * If being flinged and user touches the screen, initiate drag; |
| 815 | * otherwise don't. mScroller.isFinished should be false when |
| 816 | * being flinged. |
| 817 | */ |
Michael Jurka | fd177c1 | 2010-10-19 15:50:43 -0700 | [diff] [blame] | 818 | final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX()); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 819 | final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop); |
| 820 | if (finishedScrolling) { |
| 821 | mTouchState = TOUCH_STATE_REST; |
| 822 | mScroller.abortAnimation(); |
| 823 | } else { |
| 824 | mTouchState = TOUCH_STATE_SCROLLING; |
| 825 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 826 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 827 | // 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] | 828 | // to scroll the current page |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 829 | if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 830 | if (getChildCount() > 0) { |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 831 | if (hitsPreviousPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 832 | mTouchState = TOUCH_STATE_PREV_PAGE; |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 833 | } else if (hitsNextPage(x, y)) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 834 | mTouchState = TOUCH_STATE_NEXT_PAGE; |
| 835 | } |
| 836 | } |
| 837 | } |
| 838 | break; |
| 839 | } |
| 840 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 841 | case MotionEvent.ACTION_UP: |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 842 | onWallpaperTap(ev); |
| 843 | case MotionEvent.ACTION_CANCEL: |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 844 | mTouchState = TOUCH_STATE_REST; |
| 845 | mAllowLongPress = false; |
| 846 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 847 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 848 | break; |
| 849 | |
| 850 | case MotionEvent.ACTION_POINTER_UP: |
| 851 | onSecondaryPointerUp(ev); |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 852 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | |
| 856 | /* |
| 857 | * The only time we want to intercept motion events is if we are in the |
| 858 | * drag mode. |
| 859 | */ |
| 860 | return mTouchState != TOUCH_STATE_REST; |
| 861 | } |
| 862 | |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 863 | protected void animateClickFeedback(View v, final Runnable r) { |
| 864 | // animate the view slightly to show click feedback running some logic after it is "pressed" |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 865 | ObjectAnimator anim = (ObjectAnimator) AnimatorInflater. |
| 866 | loadAnimator(mContext, R.anim.paged_view_click_feedback); |
| 867 | anim.setTarget(v); |
| 868 | anim.addListener(new AnimatorListenerAdapter() { |
| 869 | public void onAnimationRepeat(Animator animation) { |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 870 | r.run(); |
| 871 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 872 | }); |
Winson Chung | 228a0fa | 2011-01-26 22:14:13 -0800 | [diff] [blame] | 873 | anim.start(); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 876 | protected void determineScrollingStart(MotionEvent ev) { |
| 877 | determineScrollingStart(ev, 1.0f); |
| 878 | } |
| 879 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 880 | /* |
| 881 | * Determines if we should change the touch state to start scrolling after the |
| 882 | * user moves their touch point too far. |
| 883 | */ |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 884 | protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 885 | /* |
| 886 | * Locally do absolute value. mLastMotionX is set to the y value |
| 887 | * of the down event. |
| 888 | */ |
| 889 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 890 | final float x = ev.getX(pointerIndex); |
| 891 | final float y = ev.getY(pointerIndex); |
| 892 | final int xDiff = (int) Math.abs(x - mLastMotionX); |
| 893 | final int yDiff = (int) Math.abs(y - mLastMotionY); |
| 894 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 895 | final int touchSlop = Math.round(touchSlopScale * mTouchSlop); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 896 | boolean xPaged = xDiff > mPagingTouchSlop; |
| 897 | boolean xMoved = xDiff > touchSlop; |
| 898 | boolean yMoved = yDiff > touchSlop; |
| 899 | |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 900 | if (xMoved || xPaged || yMoved) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 901 | if (mUsePagingTouchSlop ? xPaged : xMoved) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 902 | // Scroll if the user moved far enough along the X axis |
| 903 | mTouchState = TOUCH_STATE_SCROLLING; |
Adam Cohen | 6342bba | 2011-03-10 11:33:35 -0800 | [diff] [blame] | 904 | mTotalMotionX += Math.abs(mLastMotionX - x); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 905 | mLastMotionX = x; |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 906 | mLastMotionXRemainder = 0; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 907 | mTouchX = mScrollX; |
| 908 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 909 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 910 | } |
| 911 | // Either way, cancel any pending longpress |
Adam Cohen | f8d2823 | 2011-02-01 21:47:00 -0800 | [diff] [blame] | 912 | cancelCurrentPageLongPress(); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | protected void cancelCurrentPageLongPress() { |
| 917 | if (mAllowLongPress) { |
| 918 | mAllowLongPress = false; |
| 919 | // Try canceling the long press. It could also have been scheduled |
| 920 | // by a distant descendant, so use the mAllowLongPress flag to block |
| 921 | // everything |
| 922 | final View currentPage = getPageAt(mCurrentPage); |
| 923 | if (currentPage != null) { |
| 924 | currentPage.cancelLongPress(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 929 | // This curve determines how the effect of scrolling over the limits of the page dimishes |
| 930 | // as the user pulls further and further from the bounds |
| 931 | private float overScrollInfluenceCurve(float f) { |
| 932 | f -= 1.0f; |
| 933 | return f * f * f + 1.0f; |
| 934 | } |
| 935 | |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 936 | protected void overScroll(float amount) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 937 | int screenSize = getMeasuredWidth(); |
| 938 | |
| 939 | float f = (amount / screenSize); |
| 940 | |
| 941 | if (f == 0) return; |
| 942 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 943 | |
Adam Cohen | 7bfc979 | 2011-01-28 13:52:37 -0800 | [diff] [blame] | 944 | // Clamp this factor, f, to -1 < f < 1 |
| 945 | if (Math.abs(f) >= 1) { |
| 946 | f /= Math.abs(f); |
| 947 | } |
| 948 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 949 | int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * f * screenSize); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 950 | if (amount < 0) { |
| 951 | mScrollX = overScrollAmount; |
| 952 | } else { |
| 953 | mScrollX = mMaxScrollX + overScrollAmount; |
| 954 | } |
| 955 | invalidate(); |
| 956 | } |
| 957 | |
Michael Jurka | c5b262c | 2011-01-12 20:24:50 -0800 | [diff] [blame] | 958 | protected float maxOverScroll() { |
| 959 | // Using the formula in overScroll, assuming that f = 1.0 (which it should generally not |
| 960 | // exceed). Used to find out how much extra wallpaper we need for the overscroll effect |
| 961 | float f = 1.0f; |
| 962 | f = f / (Math.abs(f)) * (overScrollInfluenceCurve(Math.abs(f))); |
| 963 | return OVERSCROLL_DAMP_FACTOR * f; |
| 964 | } |
| 965 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 966 | @Override |
| 967 | public boolean onTouchEvent(MotionEvent ev) { |
Winson Chung | 45e1d6e | 2010-11-09 17:19:49 -0800 | [diff] [blame] | 968 | // Skip touch handling if there are no pages to swipe |
| 969 | if (getChildCount() <= 0) return super.onTouchEvent(ev); |
| 970 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 971 | acquireVelocityTrackerAndAddMovement(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 972 | |
| 973 | final int action = ev.getAction(); |
| 974 | |
| 975 | switch (action & MotionEvent.ACTION_MASK) { |
| 976 | case MotionEvent.ACTION_DOWN: |
| 977 | /* |
| 978 | * If being flinged and user touches, stop the fling. isFinished |
| 979 | * will be false if being flinged. |
| 980 | */ |
| 981 | if (!mScroller.isFinished()) { |
| 982 | mScroller.abortAnimation(); |
| 983 | } |
| 984 | |
| 985 | // Remember where the motion event started |
| 986 | mDownMotionX = mLastMotionX = ev.getX(); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 987 | mLastMotionXRemainder = 0; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 988 | mTotalMotionX = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 989 | mActivePointerId = ev.getPointerId(0); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 990 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 991 | pageBeginMoving(); |
| 992 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 993 | break; |
| 994 | |
| 995 | case MotionEvent.ACTION_MOVE: |
| 996 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 997 | // Scroll to follow the motion event |
| 998 | final int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 999 | final float x = ev.getX(pointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1000 | final float deltaX = mLastMotionX + mLastMotionXRemainder - x; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1001 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1002 | mTotalMotionX += Math.abs(deltaX); |
| 1003 | |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1004 | // Only scroll and update mLastMotionX if we have moved some discrete amount. We |
| 1005 | // keep the remainder because we are actually testing if we've moved from the last |
| 1006 | // scrolled position (which is discrete). |
| 1007 | if (Math.abs(deltaX) >= 1.0f) { |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1008 | mTouchX += deltaX; |
| 1009 | mSmoothingTime = System.nanoTime() / NANOTIME_DIV; |
| 1010 | if (!mDeferScrollUpdate) { |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1011 | scrollBy((int) deltaX, 0); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1012 | if (DEBUG) Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1013 | } else { |
| 1014 | invalidate(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1015 | } |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1016 | mLastMotionX = x; |
| 1017 | mLastMotionXRemainder = deltaX - (int) deltaX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1018 | } else { |
| 1019 | awakenScrollBars(); |
| 1020 | } |
Adam Cohen | 564976a | 2010-10-13 18:52:07 -0700 | [diff] [blame] | 1021 | } else { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1022 | determineScrollingStart(ev); |
| 1023 | } |
| 1024 | break; |
| 1025 | |
| 1026 | case MotionEvent.ACTION_UP: |
| 1027 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1028 | final int activePointerId = mActivePointerId; |
| 1029 | final int pointerIndex = ev.findPointerIndex(activePointerId); |
| 1030 | final float x = ev.getX(pointerIndex); |
| 1031 | final VelocityTracker velocityTracker = mVelocityTracker; |
| 1032 | velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 1033 | int velocityX = (int) velocityTracker.getXVelocity(activePointerId); |
Winson Chung | 9cfd25f | 2010-10-24 16:09:28 -0700 | [diff] [blame] | 1034 | final int deltaX = (int) (x - mDownMotionX); |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1035 | boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1036 | final int snapVelocity = mSnapVelocity; |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1037 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1038 | mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x); |
| 1039 | |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1040 | // In the case that the page is moved far to one direction and then is flung |
| 1041 | // in the opposite direction, we use a threshold to determine whether we should |
| 1042 | // just return to the starting page, or if we should skip one further. |
| 1043 | boolean returnToOriginalPage = false; |
| 1044 | final int pageWidth = getScaledMeasuredWidth(getChildAt(mCurrentPage)); |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1045 | if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD && |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1046 | Math.signum(velocityX) != Math.signum(deltaX)) { |
| 1047 | returnToOriginalPage = true; |
| 1048 | } |
| 1049 | |
Adam Cohen | b64cb5a | 2011-02-15 13:53:42 -0800 | [diff] [blame] | 1050 | boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING && |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1051 | Math.abs(velocityX) > snapVelocity; |
| 1052 | |
| 1053 | int finalPage; |
| 1054 | // We give flings precedence over large moves, which is why we short-circuit our |
| 1055 | // test for a large move if a fling has been registered. That is, a large |
| 1056 | // move to the left and fling to the right will register as a fling to the right. |
| 1057 | if (((isSignificantMove && deltaX > 0 && !isFling) || |
| 1058 | (isFling && velocityX > 0)) && mCurrentPage > 0) { |
| 1059 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1; |
| 1060 | snapToPageWithVelocity(finalPage, velocityX); |
| 1061 | } else if (((isSignificantMove && deltaX < 0 && !isFling) || |
| 1062 | (isFling && velocityX < 0)) && |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1063 | mCurrentPage < getChildCount() - 1) { |
Adam Cohen | aefd4e1 | 2011-02-14 16:39:38 -0800 | [diff] [blame] | 1064 | finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1; |
| 1065 | snapToPageWithVelocity(finalPage, velocityX); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1066 | } else { |
| 1067 | snapToDestination(); |
| 1068 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1069 | } else if (mTouchState == TOUCH_STATE_PREV_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1070 | // at this point we have not moved beyond the touch slop |
| 1071 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1072 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1073 | int nextPage = Math.max(0, mCurrentPage - 1); |
| 1074 | if (nextPage != mCurrentPage) { |
| 1075 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1076 | } else { |
| 1077 | snapToDestination(); |
| 1078 | } |
Patrick Dubroy | d0ce1ec | 2011-01-19 18:47:27 -0800 | [diff] [blame] | 1079 | } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1080 | // at this point we have not moved beyond the touch slop |
| 1081 | // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so |
| 1082 | // we can just page |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1083 | int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1); |
| 1084 | if (nextPage != mCurrentPage) { |
| 1085 | snapToPage(nextPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1086 | } else { |
| 1087 | snapToDestination(); |
| 1088 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1089 | } else { |
| 1090 | onWallpaperTap(ev); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1091 | } |
| 1092 | mTouchState = TOUCH_STATE_REST; |
| 1093 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1094 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1095 | break; |
| 1096 | |
| 1097 | case MotionEvent.ACTION_CANCEL: |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1098 | if (mTouchState == TOUCH_STATE_SCROLLING) { |
| 1099 | snapToDestination(); |
| 1100 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1101 | mTouchState = TOUCH_STATE_REST; |
| 1102 | mActivePointerId = INVALID_POINTER; |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1103 | releaseVelocityTracker(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1104 | break; |
| 1105 | |
| 1106 | case MotionEvent.ACTION_POINTER_UP: |
| 1107 | onSecondaryPointerUp(ev); |
| 1108 | break; |
| 1109 | } |
| 1110 | |
| 1111 | return true; |
| 1112 | } |
| 1113 | |
Winson Chung | 185d716 | 2011-02-28 13:47:29 -0800 | [diff] [blame] | 1114 | @Override |
| 1115 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 1116 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 1117 | switch (event.getAction()) { |
| 1118 | case MotionEvent.ACTION_SCROLL: { |
| 1119 | // Handle mouse (or ext. device) by shifting the page depending on the scroll |
| 1120 | final float vscroll; |
| 1121 | final float hscroll; |
| 1122 | if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) { |
| 1123 | vscroll = 0; |
| 1124 | hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1125 | } else { |
| 1126 | vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 1127 | hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL); |
| 1128 | } |
| 1129 | if (hscroll != 0 || vscroll != 0) { |
| 1130 | if (hscroll > 0 || vscroll > 0) { |
| 1131 | scrollRight(); |
| 1132 | } else { |
| 1133 | scrollLeft(); |
| 1134 | } |
| 1135 | return true; |
| 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | return super.onGenericMotionEvent(event); |
| 1141 | } |
| 1142 | |
Michael Jurka | b8f0672 | 2010-10-10 15:58:46 -0700 | [diff] [blame] | 1143 | private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) { |
| 1144 | if (mVelocityTracker == null) { |
| 1145 | mVelocityTracker = VelocityTracker.obtain(); |
| 1146 | } |
| 1147 | mVelocityTracker.addMovement(ev); |
| 1148 | } |
| 1149 | |
| 1150 | private void releaseVelocityTracker() { |
| 1151 | if (mVelocityTracker != null) { |
| 1152 | mVelocityTracker.recycle(); |
| 1153 | mVelocityTracker = null; |
| 1154 | } |
| 1155 | } |
| 1156 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1157 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 1158 | final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> |
| 1159 | MotionEvent.ACTION_POINTER_INDEX_SHIFT; |
| 1160 | final int pointerId = ev.getPointerId(pointerIndex); |
| 1161 | if (pointerId == mActivePointerId) { |
| 1162 | // This was our active pointer going up. Choose a new |
| 1163 | // active pointer and adjust accordingly. |
| 1164 | // TODO: Make this decision more intelligent. |
| 1165 | final int newPointerIndex = pointerIndex == 0 ? 1 : 0; |
| 1166 | mLastMotionX = mDownMotionX = ev.getX(newPointerIndex); |
| 1167 | mLastMotionY = ev.getY(newPointerIndex); |
Winson Chung | c0844aa | 2011-02-02 15:25:58 -0800 | [diff] [blame] | 1168 | mLastMotionXRemainder = 0; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1169 | mActivePointerId = ev.getPointerId(newPointerIndex); |
| 1170 | if (mVelocityTracker != null) { |
| 1171 | mVelocityTracker.clear(); |
| 1172 | } |
| 1173 | } |
Jeff Brown | 1d0867c | 2010-12-02 18:27:39 -0800 | [diff] [blame] | 1174 | if (mTouchState == TOUCH_STATE_REST) { |
| 1175 | onWallpaperTap(ev); |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | protected void onWallpaperTap(MotionEvent ev) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | @Override |
| 1183 | public void requestChildFocus(View child, View focused) { |
| 1184 | super.requestChildFocus(child, focused); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1185 | int page = indexOfChild(child); |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 1186 | if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1187 | snapToPage(page); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1191 | protected int getChildIndexForRelativeOffset(int relativeOffset) { |
| 1192 | final int childCount = getChildCount(); |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1193 | int left; |
| 1194 | int right; |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1195 | for (int i = 0; i < childCount; ++i) { |
Adam Cohen | 9c4949e | 2010-10-05 12:27:22 -0700 | [diff] [blame] | 1196 | left = getRelativeChildOffset(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1197 | right = (left + getScaledMeasuredWidth(getChildAt(i))); |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1198 | if (left <= relativeOffset && relativeOffset <= right) { |
| 1199 | return i; |
| 1200 | } |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1201 | } |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1205 | protected void setMinimumWidthOverride(int minimumWidth) { |
| 1206 | mMinimumWidth = minimumWidth; |
| 1207 | } |
Winson Chung | 34b23d5 | 2011-03-18 11:29:34 -0700 | [diff] [blame] | 1208 | protected void resetMinimumWidthOverride() { |
| 1209 | mMinimumWidth = 0; |
| 1210 | } |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1211 | |
| 1212 | protected int getChildWidth(int index) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1213 | // This functions are called enough times that it actually makes a difference in the |
| 1214 | // profiler -- so just inline the max() here |
| 1215 | final int measuredWidth = getChildAt(index).getMeasuredWidth(); |
| 1216 | final int minWidth = mMinimumWidth; |
| 1217 | return (minWidth > measuredWidth) ? minWidth : measuredWidth; |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1220 | protected int getRelativeChildOffset(int index) { |
Winson Chung | 1908d07 | 2011-02-24 18:09:44 -0800 | [diff] [blame] | 1221 | return (getMeasuredWidth() - getChildWidth(index)) / 2; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | protected int getChildOffset(int index) { |
| 1225 | if (getChildCount() == 0) |
| 1226 | return 0; |
| 1227 | |
| 1228 | int offset = getRelativeChildOffset(0); |
| 1229 | for (int i = 0; i < index; ++i) { |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1230 | offset += getScaledMeasuredWidth(getChildAt(i)) + mPageSpacing; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1231 | } |
| 1232 | return offset; |
| 1233 | } |
| 1234 | |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1235 | protected int getScaledMeasuredWidth(View child) { |
Winson Chung | 63257c1 | 2011-05-05 17:06:13 -0700 | [diff] [blame] | 1236 | // This functions are called enough times that it actually makes a difference in the |
| 1237 | // profiler -- so just inline the max() here |
| 1238 | final int measuredWidth = child.getMeasuredWidth(); |
| 1239 | final int minWidth = mMinimumWidth; |
| 1240 | final int maxWidth = (minWidth > measuredWidth) ? minWidth : measuredWidth; |
| 1241 | return (int) (maxWidth * mLayoutScale + 0.5f); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1242 | } |
| 1243 | |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1244 | int getPageNearestToCenterOfScreen() { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1245 | int minDistanceFromScreenCenter = getMeasuredWidth(); |
| 1246 | int minDistanceFromScreenCenterIndex = -1; |
| 1247 | int screenCenter = mScrollX + (getMeasuredWidth() / 2); |
| 1248 | final int childCount = getChildCount(); |
| 1249 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1250 | View layout = (View) getChildAt(i); |
Michael Jurka | d3ef306 | 2010-11-23 16:23:58 -0800 | [diff] [blame] | 1251 | int childWidth = getScaledMeasuredWidth(layout); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1252 | int halfChildWidth = (childWidth / 2); |
| 1253 | int childCenter = getChildOffset(i) + halfChildWidth; |
| 1254 | int distanceFromScreenCenter = Math.abs(childCenter - screenCenter); |
| 1255 | if (distanceFromScreenCenter < minDistanceFromScreenCenter) { |
| 1256 | minDistanceFromScreenCenter = distanceFromScreenCenter; |
| 1257 | minDistanceFromScreenCenterIndex = i; |
| 1258 | } |
| 1259 | } |
Adam Cohen | d19d3ca | 2010-09-15 14:43:42 -0700 | [diff] [blame] | 1260 | return minDistanceFromScreenCenterIndex; |
| 1261 | } |
| 1262 | |
| 1263 | protected void snapToDestination() { |
| 1264 | snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1267 | private static class ScrollInterpolator implements Interpolator { |
| 1268 | public ScrollInterpolator() { |
| 1269 | } |
| 1270 | |
| 1271 | public float getInterpolation(float t) { |
| 1272 | t -= 1.0f; |
| 1273 | return t*t*t*t*t + 1; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | // We want the duration of the page snap animation to be influenced by the distance that |
| 1278 | // the screen has to travel, however, we don't want this duration to be effected in a |
| 1279 | // purely linear fashion. Instead, we use this method to moderate the effect that the distance |
| 1280 | // of travel has on the overall snap duration. |
| 1281 | float distanceInfluenceForSnapDuration(float f) { |
| 1282 | f -= 0.5f; // center the values about 0. |
| 1283 | f *= 0.3f * Math.PI / 2.0f; |
| 1284 | return (float) Math.sin(f); |
| 1285 | } |
| 1286 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1287 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1288 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 1289 | int halfScreenSize = getMeasuredWidth() / 2; |
| 1290 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1291 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1292 | if (DEBUG) Log.d(TAG, "snapToPageWithVelocity.getRelativeChildOffset(): " |
| 1293 | + getMeasuredWidth() + ", " + getChildWidth(whichPage)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1294 | final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
| 1295 | int delta = newX - mUnboundedScrollX; |
| 1296 | int duration = 0; |
| 1297 | |
| 1298 | if (Math.abs(velocity) < MIN_FLING_VELOCITY) { |
| 1299 | // If the velocity is low enough, then treat this more as an automatic page advance |
| 1300 | // as opposed to an apparent physical response to flinging |
| 1301 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | // Here we compute a "distance" that will be used in the computation of the overall |
| 1306 | // snap duration. This is a function of the actual distance that needs to be traveled; |
| 1307 | // we keep this value close to half screen size in order to reduce the variance in snap |
| 1308 | // duration as a function of the distance the page needs to travel. |
Michael Jurka | 20b7ca9 | 2011-06-07 20:09:16 -0700 | [diff] [blame] | 1309 | float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1310 | float distance = halfScreenSize + halfScreenSize * |
| 1311 | distanceInfluenceForSnapDuration(distanceRatio); |
| 1312 | |
| 1313 | velocity = Math.abs(velocity); |
| 1314 | velocity = Math.max(MINIMUM_SNAP_VELOCITY, velocity); |
| 1315 | |
| 1316 | // we want the page's snap velocity to approximately match the velocity at which the |
| 1317 | // 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] | 1318 | // interpolator at zero, ie. 5. We use 4 to make it a little slower. |
| 1319 | duration = 4 * Math.round(1000 * Math.abs(distance / velocity)); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 1320 | |
| 1321 | snapToPage(whichPage, delta, duration); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | protected void snapToPage(int whichPage) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1325 | snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1326 | } |
| 1327 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1328 | protected void snapToPage(int whichPage, int duration) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1329 | whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1)); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1330 | |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1331 | if (DEBUG) Log.d(TAG, "snapToPage.getChildOffset(): " + getChildOffset(whichPage)); |
| 1332 | if (DEBUG) Log.d(TAG, "snapToPage.getRelativeChildOffset(): " + getMeasuredWidth() + ", " |
| 1333 | + getChildWidth(whichPage)); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1334 | int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1335 | final int sX = mUnboundedScrollX; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1336 | final int delta = newX - sX; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1337 | snapToPage(whichPage, delta, duration); |
| 1338 | } |
| 1339 | |
| 1340 | protected void snapToPage(int whichPage, int delta, int duration) { |
| 1341 | mNextPage = whichPage; |
| 1342 | |
| 1343 | View focusedChild = getFocusedChild(); |
| 1344 | if (focusedChild != null && whichPage != mCurrentPage && |
| 1345 | focusedChild == getChildAt(mCurrentPage)) { |
| 1346 | focusedChild.clearFocus(); |
| 1347 | } |
| 1348 | |
| 1349 | pageBeginMoving(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1350 | awakenScrollBars(duration); |
| 1351 | if (duration == 0) { |
| 1352 | duration = Math.abs(delta); |
| 1353 | } |
| 1354 | |
| 1355 | if (!mScroller.isFinished()) mScroller.abortAnimation(); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 1356 | mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration); |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1357 | |
| 1358 | // only load some associated pages |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1359 | loadAssociatedPages(mNextPage); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1360 | notifyPageSwitchListener(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1361 | invalidate(); |
| 1362 | } |
| 1363 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1364 | public void scrollLeft() { |
| 1365 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1366 | if (mCurrentPage > 0) snapToPage(mCurrentPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1367 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1368 | if (mNextPage > 0) snapToPage(mNextPage - 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | public void scrollRight() { |
| 1373 | if (mScroller.isFinished()) { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1374 | if (mCurrentPage < getChildCount() -1) snapToPage(mCurrentPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1375 | } else { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1376 | if (mNextPage < getChildCount() -1) snapToPage(mNextPage + 1); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1380 | public int getPageForView(View v) { |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1381 | int result = -1; |
| 1382 | if (v != null) { |
| 1383 | ViewParent vp = v.getParent(); |
| 1384 | int count = getChildCount(); |
| 1385 | for (int i = 0; i < count; i++) { |
| 1386 | if (vp == getChildAt(i)) { |
| 1387 | return i; |
| 1388 | } |
| 1389 | } |
| 1390 | } |
| 1391 | return result; |
| 1392 | } |
| 1393 | |
| 1394 | /** |
| 1395 | * @return True is long presses are still allowed for the current touch |
| 1396 | */ |
| 1397 | public boolean allowLongPress() { |
| 1398 | return mAllowLongPress; |
| 1399 | } |
| 1400 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1401 | /** |
| 1402 | * Set true to allow long-press events to be triggered, usually checked by |
| 1403 | * {@link Launcher} to accept or block dpad-initiated long-presses. |
| 1404 | */ |
| 1405 | public void setAllowLongPress(boolean allowLongPress) { |
| 1406 | mAllowLongPress = allowLongPress; |
| 1407 | } |
| 1408 | |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1409 | public static class SavedState extends BaseSavedState { |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1410 | int currentPage = -1; |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1411 | |
| 1412 | SavedState(Parcelable superState) { |
| 1413 | super(superState); |
| 1414 | } |
| 1415 | |
| 1416 | private SavedState(Parcel in) { |
| 1417 | super(in); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1418 | currentPage = in.readInt(); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | @Override |
| 1422 | public void writeToParcel(Parcel out, int flags) { |
| 1423 | super.writeToParcel(out, flags); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1424 | out.writeInt(currentPage); |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | public static final Parcelable.Creator<SavedState> CREATOR = |
| 1428 | new Parcelable.Creator<SavedState>() { |
| 1429 | public SavedState createFromParcel(Parcel in) { |
| 1430 | return new SavedState(in); |
| 1431 | } |
| 1432 | |
| 1433 | public SavedState[] newArray(int size) { |
| 1434 | return new SavedState[size]; |
| 1435 | } |
| 1436 | }; |
| 1437 | } |
| 1438 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1439 | public void loadAssociatedPages(int page) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1440 | if (mContentIsRefreshable) { |
| 1441 | final int count = getChildCount(); |
| 1442 | if (page < count) { |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1443 | int lowerPageBound = getAssociatedLowerPageBound(page); |
| 1444 | int upperPageBound = getAssociatedUpperPageBound(page); |
Winson Chung | 785d2eb | 2011-04-14 16:08:02 -0700 | [diff] [blame] | 1445 | if (DEBUG) Log.d(TAG, "loadAssociatedPages: " + lowerPageBound + "/" |
| 1446 | + upperPageBound); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1447 | for (int i = 0; i < count; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1448 | Page layout = (Page) getChildAt(i); |
| 1449 | final int childCount = layout.getPageChildCount(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1450 | if (lowerPageBound <= i && i <= upperPageBound) { |
| 1451 | if (mDirtyPageContent.get(i)) { |
| 1452 | syncPageItems(i); |
| 1453 | mDirtyPageContent.set(i, false); |
| 1454 | } |
| 1455 | } else { |
| 1456 | if (childCount > 0) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1457 | layout.removeAllViewsOnPage(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1458 | } |
| 1459 | mDirtyPageContent.set(i, true); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1460 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1461 | } |
| 1462 | } |
Winson Chung | 80baf5a | 2010-08-09 16:03:15 -0700 | [diff] [blame] | 1463 | } |
| 1464 | } |
| 1465 | |
Winson Chung | e3193b9 | 2010-09-10 11:44:42 -0700 | [diff] [blame] | 1466 | protected int getAssociatedLowerPageBound(int page) { |
| 1467 | return Math.max(0, page - 1); |
| 1468 | } |
| 1469 | protected int getAssociatedUpperPageBound(int page) { |
| 1470 | final int count = getChildCount(); |
| 1471 | return Math.min(page + 1, count - 1); |
| 1472 | } |
| 1473 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1474 | protected void startChoiceMode(int mode, ActionMode.Callback callback) { |
Patrick Dubroy | 430c53b | 2010-09-08 16:01:19 -0700 | [diff] [blame] | 1475 | if (isChoiceMode(CHOICE_MODE_NONE)) { |
| 1476 | mChoiceMode = mode; |
| 1477 | mActionMode = startActionMode(callback); |
| 1478 | } |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1479 | } |
| 1480 | |
Patrick Dubroy | 2b9ff37 | 2010-09-07 17:49:27 -0700 | [diff] [blame] | 1481 | public void endChoiceMode() { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1482 | if (!isChoiceMode(CHOICE_MODE_NONE)) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1483 | mChoiceMode = CHOICE_MODE_NONE; |
| 1484 | resetCheckedGrandchildren(); |
Michael Jurka | e17e19c | 2010-09-28 11:01:39 -0700 | [diff] [blame] | 1485 | if (mActionMode != null) mActionMode.finish(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1486 | mActionMode = null; |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | protected boolean isChoiceMode(int mode) { |
| 1491 | return mChoiceMode == mode; |
| 1492 | } |
| 1493 | |
| 1494 | protected ArrayList<Checkable> getCheckedGrandchildren() { |
| 1495 | ArrayList<Checkable> checked = new ArrayList<Checkable>(); |
| 1496 | final int childCount = getChildCount(); |
| 1497 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1498 | Page layout = (Page) getChildAt(i); |
| 1499 | final int grandChildCount = layout.getPageChildCount(); |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1500 | for (int j = 0; j < grandChildCount; ++j) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1501 | final View v = layout.getChildOnPageAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1502 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1503 | checked.add((Checkable) v); |
| 1504 | } |
| 1505 | } |
| 1506 | } |
| 1507 | return checked; |
| 1508 | } |
| 1509 | |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1510 | /** |
| 1511 | * If in CHOICE_MODE_SINGLE and an item is checked, returns that item. |
| 1512 | * Otherwise, returns null. |
| 1513 | */ |
| 1514 | protected Checkable getSingleCheckedGrandchild() { |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 1515 | if (mChoiceMode != CHOICE_MODE_MULTIPLE) { |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1516 | final int childCount = getChildCount(); |
| 1517 | for (int i = 0; i < childCount; ++i) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1518 | Page layout = (Page) getChildAt(i); |
| 1519 | final int grandChildCount = layout.getPageChildCount(); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1520 | for (int j = 0; j < grandChildCount; ++j) { |
Michael Jurka | 8245a86 | 2011-02-01 17:53:59 -0800 | [diff] [blame] | 1521 | final View v = layout.getChildOnPageAt(j); |
Patrick Dubroy | 9f7aec8 | 2010-09-06 11:03:37 -0700 | [diff] [blame] | 1522 | if (v instanceof Checkable && ((Checkable) v).isChecked()) { |
| 1523 | return (Checkable) v; |
| 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | return null; |
| 1529 | } |
| 1530 | |
Winson Chung | 5f2aa4e | 2010-08-20 14:49:25 -0700 | [diff] [blame] | 1531 | protected void resetCheckedGrandchildren() { |
| 1532 | // loop through children, and set all of their children to _not_ be checked |
| 1533 | final ArrayList<Checkable> checked = getCheckedGrandchildren(); |
| 1534 | for (int i = 0; i < checked.size(); ++i) { |
| 1535 | final Checkable c = checked.get(i); |
| 1536 | c.setChecked(false); |
| 1537 | } |
| 1538 | } |
| 1539 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 1540 | public void setRestorePage(int restorePage) { |
| 1541 | mRestorePage = restorePage; |
| 1542 | } |
| 1543 | |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1544 | /** |
| 1545 | * This method is called ONLY to synchronize the number of pages that the paged view has. |
| 1546 | * To actually fill the pages with information, implement syncPageItems() below. It is |
| 1547 | * guaranteed that syncPageItems() will be called for a particular page before it is shown, |
| 1548 | * and therefore, individual page items do not need to be updated in this method. |
| 1549 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1550 | public abstract void syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1551 | |
| 1552 | /** |
| 1553 | * This method is called to synchronize the items that are on a particular page. If views on |
| 1554 | * the page can be reused, then they should be updated within this method. |
| 1555 | */ |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1556 | public abstract void syncPageItems(int page); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1557 | |
Michael Jurka | 983e3fd | 2011-05-26 17:10:29 -0700 | [diff] [blame] | 1558 | protected void postInvalidatePageData(final boolean clearViews) { |
| 1559 | post(new Runnable() { |
| 1560 | // post the call to avoid a call to requestLayout from a layout pass |
| 1561 | public void run() { |
| 1562 | if (clearViews) { |
| 1563 | removeAllViews(); |
| 1564 | } |
| 1565 | invalidatePageData(); |
| 1566 | } |
| 1567 | }); |
| 1568 | } |
| 1569 | |
Patrick Dubroy | 244d74c | 2011-05-19 16:48:48 -0700 | [diff] [blame] | 1570 | protected void invalidatePageData() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1571 | if (mContentIsRefreshable) { |
| 1572 | // Update all the pages |
| 1573 | syncPages(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1574 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1575 | // Mark each of the pages as dirty |
| 1576 | final int count = getChildCount(); |
| 1577 | mDirtyPageContent.clear(); |
| 1578 | for (int i = 0; i < count; ++i) { |
| 1579 | mDirtyPageContent.add(true); |
| 1580 | } |
| 1581 | |
Winson Chung | a12a250 | 2010-12-20 14:41:35 -0800 | [diff] [blame] | 1582 | // Use the restore page if necessary |
| 1583 | if (mRestorePage > -1) { |
| 1584 | mCurrentPage = mRestorePage; |
| 1585 | mRestorePage = -1; |
| 1586 | } |
| 1587 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1588 | // Load any pages that are necessary for the current window of views |
| 1589 | loadAssociatedPages(mCurrentPage); |
| 1590 | mDirtyPageAlpha = true; |
Winson Chung | b0b2e6f | 2010-10-12 17:49:56 -0700 | [diff] [blame] | 1591 | updateAdjacentPagesAlpha(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1592 | requestLayout(); |
Winson Chung | 86f7753 | 2010-08-24 11:08:22 -0700 | [diff] [blame] | 1593 | } |
Winson Chung | 321e9ee | 2010-08-09 13:37:56 -0700 | [diff] [blame] | 1594 | } |
| 1595 | } |