blob: 5606a244d55c17746fe0baf690f3713ff0f7dc7e [file] [log] [blame]
Winson Chung321e9ee2010-08-09 13:37:56 -07001/*
Adam Cohen7d30a372013-07-01 17:03:59 -07002 * Copyright (C) 2012 The Android Open Source Project
Winson Chung321e9ee2010-08-09 13:37:56 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung321e9ee2010-08-09 13:37:56 -070018
Winson Chung228a0fa2011-01-26 22:14:13 -080019import android.animation.Animator;
Winson Chung228a0fa2011-01-26 22:14:13 -080020import android.animation.AnimatorListenerAdapter;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -070021import android.animation.LayoutTransition;
Adam Cohen7d30a372013-07-01 17:03:59 -070022import android.animation.ObjectAnimator;
23import android.animation.TimeInterpolator;
Sunny Goyalcf25b522015-07-09 00:01:18 -070024import android.annotation.SuppressLint;
Sunny Goyal316490e2015-06-02 09:38:28 -070025import android.annotation.TargetApi;
Winson Chung321e9ee2010-08-09 13:37:56 -070026import android.content.Context;
Adam Cohen9c4949e2010-10-05 12:27:22 -070027import android.content.res.TypedArray;
Winson Chung321e9ee2010-08-09 13:37:56 -070028import android.graphics.Canvas;
Adam Cohen7d30a372013-07-01 17:03:59 -070029import android.graphics.Matrix;
Winson Chung321e9ee2010-08-09 13:37:56 -070030import android.graphics.Rect;
Sunny Goyal8bf6f312016-01-23 14:40:35 -080031import android.graphics.RectF;
Sunny Goyal316490e2015-06-02 09:38:28 -070032import android.os.Build;
Svetoslav Ganov08055f62012-05-15 11:06:36 -070033import android.os.Bundle;
Winson Chung321e9ee2010-08-09 13:37:56 -070034import android.os.Parcel;
35import android.os.Parcelable;
36import android.util.AttributeSet;
Adam Cohen7d30a372013-07-01 17:03:59 -070037import android.util.DisplayMetrics;
Winson Chung785d2eb2011-04-14 16:08:02 -070038import android.util.Log;
Winson Chung185d7162011-02-28 13:47:29 -080039import android.view.InputDevice;
40import android.view.KeyEvent;
Winson Chung321e9ee2010-08-09 13:37:56 -070041import android.view.MotionEvent;
42import android.view.VelocityTracker;
43import android.view.View;
44import android.view.ViewConfiguration;
Sunny Goyal4ffec482016-02-09 11:28:52 -080045import android.view.ViewDebug;
Winson Chung321e9ee2010-08-09 13:37:56 -070046import android.view.ViewGroup;
47import android.view.ViewParent;
Winson Chung6a0f57d2011-06-29 20:10:49 -070048import android.view.accessibility.AccessibilityEvent;
Winson Chungc27d1bb2011-09-29 12:07:42 -070049import android.view.accessibility.AccessibilityManager;
Winson Chung6a0f57d2011-06-29 20:10:49 -070050import android.view.accessibility.AccessibilityNodeInfo;
Adam Cohene0f66b52010-11-23 15:06:07 -080051import android.view.animation.Interpolator;
Sunny Goyal4d113a52015-05-27 10:05:28 -070052import com.android.launcher3.util.LauncherEdgeEffect;
Adam Cohen091440a2015-03-18 14:16:05 -070053import com.android.launcher3.util.Thunk;
Winson Chung6a0f57d2011-06-29 20:10:49 -070054import java.util.ArrayList;
55
Winson Chung321e9ee2010-08-09 13:37:56 -070056/**
57 * An abstraction of the original Workspace which supports browsing through a
Michael Jurka0142d492010-08-25 17:46:15 -070058 * sequential list of "pages"
Winson Chung321e9ee2010-08-09 13:37:56 -070059 */
Michael Jurka8b805b12012-04-18 14:23:14 -070060public abstract class PagedView extends ViewGroup implements ViewGroup.OnHierarchyChangeListener {
Winson Chung321e9ee2010-08-09 13:37:56 -070061 private static final String TAG = "PagedView";
Winson Chung785d2eb2011-04-14 16:08:02 -070062 private static final boolean DEBUG = false;
Michael Jurka0142d492010-08-25 17:46:15 -070063 protected static final int INVALID_PAGE = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -070064
Winson Chung86f77532010-08-24 11:08:22 -070065 // the min drag distance for a fling to register, to prevent random page shifts
Winson Chung9cfd25f2010-10-24 16:09:28 -070066 private static final int MIN_LENGTH_FOR_FLING = 25;
Winson Chung321e9ee2010-08-09 13:37:56 -070067
Vadim Tryshevfedca432015-08-19 17:55:02 -070068 public static final int PAGE_SNAP_ANIMATION_DURATION = 750;
Winson Chungf0c6ae02012-03-21 16:10:31 -070069 protected static final int SLOW_PAGE_SNAP_ANIMATION_DURATION = 950;
Winson Chung321e9ee2010-08-09 13:37:56 -070070
Adam Cohenb64cb5a2011-02-15 13:53:42 -080071 private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
Adam Cohen00481b32011-11-18 12:03:48 -080072 // The page is moved more than halfway, automatically move to the next page on touch up.
73 private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
Adam Cohen68d73932010-11-15 10:50:58 -080074
Sunny Goyal8e2133b2015-05-06 13:39:07 -070075 private static final float MAX_SCROLL_PROGRESS = 1.0f;
76
Adam Cohen265b9a62011-12-07 14:37:18 -080077 // The following constants need to be scaled based on density. The scaled versions will be
78 // assigned to the corresponding member variables below.
79 private static final int FLING_THRESHOLD_VELOCITY = 500;
80 private static final int MIN_SNAP_VELOCITY = 1500;
81 private static final int MIN_FLING_VELOCITY = 250;
82
Adam Cohen21cd0022013-10-09 18:57:02 -070083 public static final int INVALID_RESTORE_PAGE = -1001;
84
Adam Cohenf358a4b2013-07-23 16:47:31 -070085 private boolean mFreeScroll = false;
86 private int mFreeScrollMinScrollX = -1;
87 private int mFreeScrollMaxScrollX = -1;
88
Adam Cohen265b9a62011-12-07 14:37:18 -080089 protected int mFlingThresholdVelocity;
90 protected int mMinFlingVelocity;
91 protected int mMinSnapVelocity;
Michael Jurka0142d492010-08-25 17:46:15 -070092
Michael Jurka0142d492010-08-25 17:46:15 -070093 protected boolean mFirstLayout = true;
Adam Cohen410f3cd2013-09-22 12:09:32 -070094 private int mNormalChildHeight;
Michael Jurka0142d492010-08-25 17:46:15 -070095
Sunny Goyal4ffec482016-02-09 11:28:52 -080096 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -070097 protected int mCurrentPage;
Adam Cohen21cd0022013-10-09 18:57:02 -070098 protected int mRestorePage = INVALID_RESTORE_PAGE;
Sunny Goyalcf25b522015-07-09 00:01:18 -070099 private int mChildCountOnLastLayout;
Adam Cohene61a9a22013-06-11 15:45:31 -0700100
Sunny Goyal4ffec482016-02-09 11:28:52 -0800101 @ViewDebug.ExportedProperty(category = "launcher")
Michael Jurka0142d492010-08-25 17:46:15 -0700102 protected int mNextPage = INVALID_PAGE;
Sunny Goyalc86df472016-02-25 09:19:38 -0800103 protected int mMaxScrollX;
Adam Cohenf9618852013-11-08 06:45:03 -0800104 protected LauncherScroller mScroller;
105 private Interpolator mDefaultInterpolator;
Winson Chung321e9ee2010-08-09 13:37:56 -0700106 private VelocityTracker mVelocityTracker;
Adam Cohen091440a2015-03-18 14:16:05 -0700107 @Thunk int mPageSpacing = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700108
Adam Cohen7d30a372013-07-01 17:03:59 -0700109 private float mParentDownMotionX;
110 private float mParentDownMotionY;
Winson Chung321e9ee2010-08-09 13:37:56 -0700111 private float mDownMotionX;
Adam Cohen7d30a372013-07-01 17:03:59 -0700112 private float mDownMotionY;
113 private float mDownScrollX;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700114 private float mDragViewBaselineLeft;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700115 private float mLastMotionX;
116 private float mLastMotionXRemainder;
117 private float mLastMotionY;
118 private float mTotalMotionX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700119 private int mLastScreenCenter = -1;
Adam Cohenedb40762013-07-18 16:45:45 -0700120
Adam Cohendbdff6b2013-09-18 19:09:15 -0700121 private boolean mCancelTap;
122
Adam Cohenedb40762013-07-18 16:45:45 -0700123 private int[] mPageScrolls;
Winson Chung321e9ee2010-08-09 13:37:56 -0700124
Michael Jurka0142d492010-08-25 17:46:15 -0700125 protected final static int TOUCH_STATE_REST = 0;
126 protected final static int TOUCH_STATE_SCROLLING = 1;
127 protected final static int TOUCH_STATE_PREV_PAGE = 2;
128 protected final static int TOUCH_STATE_NEXT_PAGE = 3;
Adam Cohen7d30a372013-07-01 17:03:59 -0700129 protected final static int TOUCH_STATE_REORDERING = 4;
130
Michael Jurka0142d492010-08-25 17:46:15 -0700131 protected int mTouchState = TOUCH_STATE_REST;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700132 private boolean mForceScreenScrolled = false;
Adam Cohencae7f572013-11-04 14:42:52 -0800133
Michael Jurka0142d492010-08-25 17:46:15 -0700134 protected OnLongClickListener mLongClickListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700135
Michael Jurka7426c422010-11-11 15:23:47 -0800136 protected int mTouchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -0700137 private int mMaximumVelocity;
Adam Cohen68d73932010-11-15 10:50:58 -0800138 protected boolean mAllowOverScroll = true;
Michael Jurkadde558b2011-11-09 22:09:06 -0800139 protected int[] mTempVisiblePagesRange = new int[2];
Winson Chung321e9ee2010-08-09 13:37:56 -0700140
Michael Jurka5f1c5092010-09-03 14:15:02 -0700141 protected static final int INVALID_POINTER = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -0700142
Michael Jurka5f1c5092010-09-03 14:15:02 -0700143 protected int mActivePointerId = INVALID_POINTER;
Winson Chung321e9ee2010-08-09 13:37:56 -0700144
Winson Chung86f77532010-08-24 11:08:22 -0700145 private PageSwitchListener mPageSwitchListener;
Winson Chung321e9ee2010-08-09 13:37:56 -0700146
Michael Jurka0142d492010-08-25 17:46:15 -0700147 // If true, modify alpha of neighboring pages as user scrolls left/right
Adam Cohen7d30a372013-07-01 17:03:59 -0700148 protected boolean mFadeInAdjacentScreens = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700149
Patrick Dubroy1262e362010-10-06 15:49:50 -0700150 protected boolean mIsPageMoving = false;
151
Sunny Goyal061380a2016-02-29 15:15:03 -0800152 protected boolean mWasInOverscroll = false;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700153
Winson Chungd2be3812013-07-16 11:11:32 -0700154 // Page Indicator
Adam Cohen091440a2015-03-18 14:16:05 -0700155 @Thunk int mPageIndicatorViewId;
156 @Thunk PageIndicator mPageIndicator;
Adam Cohen7d30a372013-07-01 17:03:59 -0700157 // The viewport whether the pages are to be contained (the actual view may be larger than the
158 // viewport)
Sunny Goyal4ffec482016-02-09 11:28:52 -0800159 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohen7d30a372013-07-01 17:03:59 -0700160 private Rect mViewport = new Rect();
161
162 // Reordering
163 // We use the min scale to determine how much to expand the actually PagedView measured
164 // dimensions such that when we are zoomed out, the view is not clipped
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700165 private static int REORDERING_DROP_REPOSITION_DURATION = 200;
Sunny Goyal316490e2015-06-02 09:38:28 -0700166 @Thunk static int REORDERING_REORDER_REPOSITION_DURATION = 300;
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700167 private static int REORDERING_SIDE_PAGE_HOVER_TIMEOUT = 80;
168
Adam Cohen7d30a372013-07-01 17:03:59 -0700169 private float mMinScale = 1f;
Winson Chungc58497e2013-09-03 17:48:37 -0700170 private boolean mUseMinScale = false;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700171 @Thunk View mDragView;
Adam Cohen7d30a372013-07-01 17:03:59 -0700172 private Runnable mSidePageHoverRunnable;
Adam Cohen091440a2015-03-18 14:16:05 -0700173 @Thunk int mSidePageHoverIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -0700174 // This variable's scope is only for the duration of startReordering() and endReordering()
175 private boolean mReorderingStarted = false;
176 // This variable's scope is for the duration of startReordering() and after the zoomIn()
177 // animation after endReordering()
178 private boolean mIsReordering;
179 // The runnable that settles the page after snapToPage and animateDragViewToOriginalPosition
Sunny Goyalcf25b522015-07-09 00:01:18 -0700180 private static final int NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT = 2;
Adam Cohen7d30a372013-07-01 17:03:59 -0700181 private int mPostReorderingPreZoomInRemainingAnimationCount;
182 private Runnable mPostReorderingPreZoomInRunnable;
183
Adam Cohen7d30a372013-07-01 17:03:59 -0700184 // Convenience/caching
Sunny Goyal106bf642015-07-16 12:18:06 -0700185 private static final Matrix sTmpInvMatrix = new Matrix();
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700186 private static final float[] sTmpPoint = new float[2];
187 private static final int[] sTmpIntPoint = new int[2];
188 private static final Rect sTmpRect = new Rect();
Sunny Goyal8bf6f312016-01-23 14:40:35 -0800189 private static final RectF sTmpRectF = new RectF();
Winson Chungb44b5242011-06-13 11:32:14 -0700190
John Spurlock77e1f472013-09-11 10:09:51 -0400191 protected final Rect mInsets = new Rect();
Sunny Goyal70660032015-05-14 00:07:08 -0700192 protected final boolean mIsRtl;
John Spurlock77e1f472013-09-11 10:09:51 -0400193
Sunny Goyal4d113a52015-05-27 10:05:28 -0700194 // Edge effect
195 private final LauncherEdgeEffect mEdgeGlowLeft = new LauncherEdgeEffect();
196 private final LauncherEdgeEffect mEdgeGlowRight = new LauncherEdgeEffect();
197
Winson Chung86f77532010-08-24 11:08:22 -0700198 public interface PageSwitchListener {
199 void onPageSwitch(View newPage, int newPageIndex);
Winson Chung321e9ee2010-08-09 13:37:56 -0700200 }
201
Winson Chung321e9ee2010-08-09 13:37:56 -0700202 public PagedView(Context context) {
203 this(context, null);
204 }
205
206 public PagedView(Context context, AttributeSet attrs) {
207 this(context, attrs, 0);
208 }
209
210 public PagedView(Context context, AttributeSet attrs, int defStyle) {
211 super(context, attrs, defStyle);
Winson Chungc9ca2982013-07-19 12:07:38 -0700212
Adam Cohen9c4949e2010-10-05 12:27:22 -0700213 TypedArray a = context.obtainStyledAttributes(attrs,
214 R.styleable.PagedView, defStyle, 0);
Winson Chungd2be3812013-07-16 11:11:32 -0700215 mPageIndicatorViewId = a.getResourceId(R.styleable.PagedView_pageIndicator, -1);
Adam Cohen9c4949e2010-10-05 12:27:22 -0700216 a.recycle();
217
Winson Chung321e9ee2010-08-09 13:37:56 -0700218 setHapticFeedbackEnabled(false);
Sunny Goyal70660032015-05-14 00:07:08 -0700219 mIsRtl = Utilities.isRtl(getResources());
Michael Jurka0142d492010-08-25 17:46:15 -0700220 init();
Winson Chung321e9ee2010-08-09 13:37:56 -0700221 }
222
223 /**
224 * Initializes various states for this workspace.
225 */
Michael Jurka0142d492010-08-25 17:46:15 -0700226 protected void init() {
Adam Cohenf9618852013-11-08 06:45:03 -0800227 mScroller = new LauncherScroller(getContext());
228 setDefaultInterpolator(new ScrollInterpolator());
Winson Chung86f77532010-08-24 11:08:22 -0700229 mCurrentPage = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700230
231 final ViewConfiguration configuration = ViewConfiguration.get(getContext());
Adam Cohen7d30a372013-07-01 17:03:59 -0700232 mTouchSlop = configuration.getScaledPagingTouchSlop();
Winson Chung321e9ee2010-08-09 13:37:56 -0700233 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
Adam Cohen265b9a62011-12-07 14:37:18 -0800234
Sunny Goyalcf25b522015-07-09 00:01:18 -0700235 float density = getResources().getDisplayMetrics().density;
236 mFlingThresholdVelocity = (int) (FLING_THRESHOLD_VELOCITY * density);
237 mMinFlingVelocity = (int) (MIN_FLING_VELOCITY * density);
238 mMinSnapVelocity = (int) (MIN_SNAP_VELOCITY * density);
Michael Jurka8b805b12012-04-18 14:23:14 -0700239 setOnHierarchyChangeListener(this);
Sunny Goyal4d113a52015-05-27 10:05:28 -0700240 setWillNotDraw(false);
241 }
242
243 protected void setEdgeGlowColor(int color) {
244 mEdgeGlowLeft.setColor(color);
245 mEdgeGlowRight.setColor(color);
Winson Chung321e9ee2010-08-09 13:37:56 -0700246 }
247
Adam Cohenf9618852013-11-08 06:45:03 -0800248 protected void setDefaultInterpolator(Interpolator interpolator) {
249 mDefaultInterpolator = interpolator;
250 mScroller.setInterpolator(mDefaultInterpolator);
251 }
252
Winson Chungd2be3812013-07-16 11:11:32 -0700253 protected void onAttachedToWindow() {
Adam Cohen53805212013-10-01 10:39:23 -0700254 super.onAttachedToWindow();
255
Winson Chungd2be3812013-07-16 11:11:32 -0700256 // Hook up the page indicator
257 ViewGroup parent = (ViewGroup) getParent();
Adam Cohen9bfdb762014-07-21 17:44:06 -0700258 ViewGroup grandParent = (ViewGroup) parent.getParent();
Winson Chungd2be3812013-07-16 11:11:32 -0700259 if (mPageIndicator == null && mPageIndicatorViewId > -1) {
Adam Cohen9bfdb762014-07-21 17:44:06 -0700260 mPageIndicator = (PageIndicator) grandParent.findViewById(mPageIndicatorViewId);
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700261 mPageIndicator.removeAllMarkers(true);
Winson Chung82dfe582013-07-26 15:07:49 -0700262
Winson Chung7819a562013-09-19 15:55:45 -0700263 ArrayList<PageIndicator.PageMarkerResources> markers =
264 new ArrayList<PageIndicator.PageMarkerResources>();
Winson Chung82dfe582013-07-26 15:07:49 -0700265 for (int i = 0; i < getChildCount(); ++i) {
266 markers.add(getPageIndicatorMarker(i));
267 }
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700269 mPageIndicator.addMarkers(markers, true);
Adam Cohenfbdf4272013-10-09 13:02:21 -0700270
271 OnClickListener listener = getPageIndicatorClickListener();
272 if (listener != null) {
273 mPageIndicator.setOnClickListener(listener);
274 }
Adam Cohen53805212013-10-01 10:39:23 -0700275 mPageIndicator.setContentDescription(getPageIndicatorDescription());
Winson Chungd2be3812013-07-16 11:11:32 -0700276 }
277 }
278
Adam Cohen53805212013-10-01 10:39:23 -0700279 protected String getPageIndicatorDescription() {
280 return getCurrentPageDescription();
281 }
282
283 protected OnClickListener getPageIndicatorClickListener() {
284 return null;
285 }
286
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700287 @Override
Winson Chungd2be3812013-07-16 11:11:32 -0700288 protected void onDetachedFromWindow() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700289 super.onDetachedFromWindow();
Winson Chungd2be3812013-07-16 11:11:32 -0700290 // Unhook the page indicator
291 mPageIndicator = null;
292 }
293
Adam Cohen7d30a372013-07-01 17:03:59 -0700294 // Convenience methods to map points from self to parent and vice versa
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700295 private float[] mapPointFromViewToParent(View v, float x, float y) {
296 sTmpPoint[0] = x;
297 sTmpPoint[1] = y;
298 v.getMatrix().mapPoints(sTmpPoint);
299 sTmpPoint[0] += v.getLeft();
300 sTmpPoint[1] += v.getTop();
301 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700302 }
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700303 private float[] mapPointFromParentToView(View v, float x, float y) {
304 sTmpPoint[0] = x - v.getLeft();
305 sTmpPoint[1] = y - v.getTop();
306 v.getMatrix().invert(sTmpInvMatrix);
307 sTmpInvMatrix.mapPoints(sTmpPoint);
308 return sTmpPoint;
Adam Cohen7d30a372013-07-01 17:03:59 -0700309 }
310
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700311 private void updateDragViewTranslationDuringDrag() {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700312 if (mDragView != null) {
313 float x = (mLastMotionX - mDownMotionX) + (getScrollX() - mDownScrollX) +
314 (mDragViewBaselineLeft - mDragView.getLeft());
315 float y = mLastMotionY - mDownMotionY;
316 mDragView.setTranslationX(x);
317 mDragView.setTranslationY(y);
Adam Cohen7d30a372013-07-01 17:03:59 -0700318
Adam Cohenf358a4b2013-07-23 16:47:31 -0700319 if (DEBUG) Log.d(TAG, "PagedView.updateDragViewTranslationDuringDrag(): "
320 + x + ", " + y);
321 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700322 }
323
324 public void setMinScale(float f) {
325 mMinScale = f;
Winson Chungc58497e2013-09-03 17:48:37 -0700326 mUseMinScale = true;
Adam Cohen7d30a372013-07-01 17:03:59 -0700327 requestLayout();
328 }
329
330 @Override
331 public void setScaleX(float scaleX) {
332 super.setScaleX(scaleX);
333 if (isReordering(true)) {
334 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
335 mLastMotionX = p[0];
336 mLastMotionY = p[1];
337 updateDragViewTranslationDuringDrag();
338 }
339 }
340
341 // Convenience methods to get the actual width/height of the PagedView (since it is measured
342 // to be larger to account for the minimum possible scale)
343 int getViewportWidth() {
344 return mViewport.width();
345 }
Adam Cohenf9c184a2016-01-15 16:47:43 -0800346 public int getViewportHeight() {
Adam Cohen7d30a372013-07-01 17:03:59 -0700347 return mViewport.height();
348 }
349
350 // Convenience methods to get the offset ASSUMING that we are centering the pages in the
351 // PagedView both horizontally and vertically
352 int getViewportOffsetX() {
353 return (getMeasuredWidth() - getViewportWidth()) / 2;
354 }
355
356 int getViewportOffsetY() {
357 return (getMeasuredHeight() - getViewportHeight()) / 2;
358 }
359
Adam Cohenedb40762013-07-18 16:45:45 -0700360 PageIndicator getPageIndicator() {
361 return mPageIndicator;
362 }
Winson Chung7819a562013-09-19 15:55:45 -0700363 protected PageIndicator.PageMarkerResources getPageIndicatorMarker(int pageIndex) {
364 return new PageIndicator.PageMarkerResources();
Winson Chung82dfe582013-07-26 15:07:49 -0700365 }
Adam Cohenedb40762013-07-18 16:45:45 -0700366
Adam Cohen674531f2013-12-13 15:07:14 -0800367 /**
368 * Add a page change listener which will be called when a page is _finished_ listening.
369 *
370 */
Winson Chung86f77532010-08-24 11:08:22 -0700371 public void setPageSwitchListener(PageSwitchListener pageSwitchListener) {
372 mPageSwitchListener = pageSwitchListener;
373 if (mPageSwitchListener != null) {
374 mPageSwitchListener.onPageSwitch(getPageAt(mCurrentPage), mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -0700375 }
376 }
377
378 /**
Tony Wickham29d853c2015-09-08 10:35:56 -0700379 * Returns the index of the currently displayed page. When in free scroll mode, this is the page
380 * that the user was on before entering free scroll mode (e.g. the home screen page they
381 * long-pressed on to enter the overview). Try using {@link #getPageNearestToCenterOfScreen()}
382 * to get the page the user is currently scrolling over.
Winson Chung321e9ee2010-08-09 13:37:56 -0700383 */
Sunny Goyal83a8f042015-05-19 12:52:12 -0700384 public int getCurrentPage() {
Winson Chung86f77532010-08-24 11:08:22 -0700385 return mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -0700386 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700387
Hyunyoung Songb76cd622015-04-16 14:34:09 -0700388 /**
389 * Returns the index of page to be shown immediately afterwards.
390 */
Vadim Tryshevfedca432015-08-19 17:55:02 -0700391 public int getNextPage() {
Winson Chung360e63f2012-04-27 13:48:05 -0700392 return (mNextPage != INVALID_PAGE) ? mNextPage : mCurrentPage;
393 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700394
Adam Cohenf9c184a2016-01-15 16:47:43 -0800395 public int getPageCount() {
Winson Chung321e9ee2010-08-09 13:37:56 -0700396 return getChildCount();
397 }
398
Sunny Goyal83a8f042015-05-19 12:52:12 -0700399 public View getPageAt(int index) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700400 return getChildAt(index);
401 }
402
Adam Cohenae4f1552011-10-20 00:15:42 -0700403 protected int indexToPage(int index) {
404 return index;
405 }
406
Winson Chung321e9ee2010-08-09 13:37:56 -0700407 /**
Winson Chungbbc60d82010-11-11 16:34:41 -0800408 * Updates the scroll of the current page immediately to its final scroll position. We use this
409 * in CustomizePagedView to allow tabs to share the same PagedView while resetting the scroll of
410 * the previous tab page.
411 */
412 protected void updateCurrentPageScroll() {
Adam Cohen0ffac432013-07-10 11:19:26 -0700413 // If the current page is invalid, just reset the scroll position to zero
414 int newX = 0;
415 if (0 <= mCurrentPage && mCurrentPage < getPageCount()) {
Adam Cohenedb40762013-07-18 16:45:45 -0700416 newX = getScrollForPage(mCurrentPage);
Adam Cohen0ffac432013-07-10 11:19:26 -0700417 }
Winson Chungbbc60d82010-11-11 16:34:41 -0800418 scrollTo(newX, 0);
419 mScroller.setFinalX(newX);
Adam Cohen4fe4c932013-10-28 16:02:34 -0700420 forceFinishScroller();
Winson Chungbbc60d82010-11-11 16:34:41 -0800421 }
422
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700423 private void abortScrollerAnimation(boolean resetNextPage) {
Adam Cohen4fe4c932013-10-28 16:02:34 -0700424 mScroller.abortAnimation();
425 // We need to clean up the next page here to avoid computeScrollHelper from
426 // updating current page on the pass.
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700427 if (resetNextPage) {
428 mNextPage = INVALID_PAGE;
429 }
Chet Haasebc2f0822012-10-26 17:59:53 -0700430 }
Adam Cohen4fe4c932013-10-28 16:02:34 -0700431
432 private void forceFinishScroller() {
433 mScroller.forceFinished(true);
434 // We need to clean up the next page here to avoid computeScrollHelper from
435 // updating current page on the pass.
436 mNextPage = INVALID_PAGE;
437 }
438
Adam Cohen6f127a62014-06-12 14:54:41 -0700439 private int validateNewPage(int newPage) {
440 int validatedPage = newPage;
441 // When in free scroll mode, we need to clamp to the free scroll page range.
442 if (mFreeScroll) {
443 getFreeScrollPageRange(mTempVisiblePagesRange);
444 validatedPage = Math.max(mTempVisiblePagesRange[0],
445 Math.min(newPage, mTempVisiblePagesRange[1]));
446 }
447 // Ensure that it is clamped by the actual set of children in all cases
Tony Wickham86930612015-09-09 13:50:40 -0700448 validatedPage = Utilities.boundInRange(validatedPage, 0, getPageCount() - 1);
Adam Cohen6f127a62014-06-12 14:54:41 -0700449 return validatedPage;
450 }
451
Chet Haasebc2f0822012-10-26 17:59:53 -0700452 /**
Winson Chung86f77532010-08-24 11:08:22 -0700453 * Sets the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -0700454 */
Sunny Goyal1d08f702015-05-04 15:50:25 -0700455 public void setCurrentPage(int currentPage) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800456 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -0700457 abortScrollerAnimation(true);
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800458 }
Michael Jurkad3ef3062010-11-23 16:23:58 -0800459 // don't introduce any checks like mCurrentPage == currentPage here-- if we change the
460 // the default
461 if (getChildCount() == 0) {
Patrick Dubroy72e0d342010-11-09 15:23:28 -0800462 return;
463 }
Adam Cohene61a9a22013-06-11 15:45:31 -0700464 mForceScreenScrolled = true;
Adam Cohen6f127a62014-06-12 14:54:41 -0700465 mCurrentPage = validateNewPage(currentPage);
Winson Chung181c3dc2013-07-17 15:36:20 -0700466 updateCurrentPageScroll();
Winson Chung86f77532010-08-24 11:08:22 -0700467 notifyPageSwitchListener();
Winson Chunga12a2502010-12-20 14:41:35 -0800468 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -0700469 }
470
Winson Chung8c87cd82013-07-23 16:20:10 -0700471 /**
472 * The restore page will be set in place of the current page at the next (likely first)
473 * layout.
474 */
475 void setRestorePage(int restorePage) {
476 mRestorePage = restorePage;
477 }
Winson Chung9b9fb962013-11-15 15:39:34 -0800478 int getRestorePage() {
479 return mRestorePage;
480 }
Winson Chung8c87cd82013-07-23 16:20:10 -0700481
Adam Cohen674531f2013-12-13 15:07:14 -0800482 /**
483 * Should be called whenever the page changes. In the case of a scroll, we wait until the page
484 * has settled.
485 */
Michael Jurka0142d492010-08-25 17:46:15 -0700486 protected void notifyPageSwitchListener() {
Winson Chung86f77532010-08-24 11:08:22 -0700487 if (mPageSwitchListener != null) {
Adam Cohen674531f2013-12-13 15:07:14 -0800488 mPageSwitchListener.onPageSwitch(getPageAt(getNextPage()), getNextPage());
Winson Chung321e9ee2010-08-09 13:37:56 -0700489 }
Winson Chungd2be3812013-07-16 11:11:32 -0700490
Adam Cohen674531f2013-12-13 15:07:14 -0800491 updatePageIndicator();
492 }
493
494 private void updatePageIndicator() {
Winson Chungd2be3812013-07-16 11:11:32 -0700495 // Update the page indicator (when we aren't reordering)
Sunny Goyal4cbf0462014-08-28 16:19:39 -0700496 if (mPageIndicator != null) {
497 mPageIndicator.setContentDescription(getPageIndicatorDescription());
498 if (!isReordering(false)) {
499 mPageIndicator.setActiveMarker(getNextPage());
500 }
Winson Chungd2be3812013-07-16 11:11:32 -0700501 }
Winson Chung321e9ee2010-08-09 13:37:56 -0700502 }
Michael Jurkace7e05f2011-02-01 22:02:35 -0800503 protected void pageBeginMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700504 if (!mIsPageMoving) {
505 mIsPageMoving = true;
506 onPageBeginMoving();
507 }
Patrick Dubroy1262e362010-10-06 15:49:50 -0700508 }
509
Michael Jurkace7e05f2011-02-01 22:02:35 -0800510 protected void pageEndMoving() {
Michael Jurkad74c9842011-07-10 12:44:21 -0700511 if (mIsPageMoving) {
512 mIsPageMoving = false;
513 onPageEndMoving();
514 }
Michael Jurka0142d492010-08-25 17:46:15 -0700515 }
516
Adam Cohen26976d92011-03-22 15:33:33 -0700517 protected boolean isPageMoving() {
518 return mIsPageMoving;
519 }
520
Michael Jurka0142d492010-08-25 17:46:15 -0700521 // a method that subclasses can override to add behavior
Patrick Dubroy1262e362010-10-06 15:49:50 -0700522 protected void onPageBeginMoving() {
523 }
524
525 // a method that subclasses can override to add behavior
526 protected void onPageEndMoving() {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700527 mWasInOverscroll = false;
Michael Jurka0142d492010-08-25 17:46:15 -0700528 }
529
Winson Chung321e9ee2010-08-09 13:37:56 -0700530 /**
Winson Chung86f77532010-08-24 11:08:22 -0700531 * Registers the specified listener on each page contained in this workspace.
Winson Chung321e9ee2010-08-09 13:37:56 -0700532 *
533 * @param l The listener used to respond to long clicks.
534 */
535 @Override
536 public void setOnLongClickListener(OnLongClickListener l) {
537 mLongClickListener = l;
Winson Chung86f77532010-08-24 11:08:22 -0700538 final int count = getPageCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700539 for (int i = 0; i < count; i++) {
Winson Chung86f77532010-08-24 11:08:22 -0700540 getPageAt(i).setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700541 }
Adam Cohen1697b792013-09-17 19:08:21 -0700542 super.setOnLongClickListener(l);
Winson Chung321e9ee2010-08-09 13:37:56 -0700543 }
544
Sunny Goyalc86df472016-02-25 09:19:38 -0800545 protected int getUnboundedScrollX() {
546 return getScrollX();
547 }
548
Winson Chung321e9ee2010-08-09 13:37:56 -0700549 @Override
Adam Cohen68d73932010-11-15 10:50:58 -0800550 public void scrollBy(int x, int y) {
Sunny Goyalc86df472016-02-25 09:19:38 -0800551 scrollTo(getUnboundedScrollX() + x, getScrollY() + y);
Adam Cohen68d73932010-11-15 10:50:58 -0800552 }
553
554 @Override
Michael Jurka0142d492010-08-25 17:46:15 -0700555 public void scrollTo(int x, int y) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700556 // In free scroll mode, we clamp the scrollX
557 if (mFreeScroll) {
Adam Cohenb2b02b92015-06-10 18:40:05 -0700558 // If the scroller is trying to move to a location beyond the maximum allowed
559 // in the free scroll mode, we make sure to end the scroll operation.
560 if (!mScroller.isFinished() &&
561 (x > mFreeScrollMaxScrollX || x < mFreeScrollMinScrollX)) {
562 forceFinishScroller();
563 }
564
Adam Cohenf358a4b2013-07-23 16:47:31 -0700565 x = Math.min(x, mFreeScrollMaxScrollX);
566 x = Math.max(x, mFreeScrollMinScrollX);
567 }
568
Sunny Goyal70660032015-05-14 00:07:08 -0700569 boolean isXBeforeFirstPage = mIsRtl ? (x > mMaxScrollX) : (x < 0);
570 boolean isXAfterLastPage = mIsRtl ? (x < 0) : (x > mMaxScrollX);
Adam Cohen0ffac432013-07-10 11:19:26 -0700571 if (isXBeforeFirstPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700572 super.scrollTo(mIsRtl ? mMaxScrollX : 0, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800573 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700574 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700575 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700576 overScroll(x - mMaxScrollX);
577 } else {
578 overScroll(x);
579 }
Adam Cohen68d73932010-11-15 10:50:58 -0800580 }
Adam Cohen0ffac432013-07-10 11:19:26 -0700581 } else if (isXAfterLastPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -0700582 super.scrollTo(mIsRtl ? 0 : mMaxScrollX, y);
Adam Cohen68d73932010-11-15 10:50:58 -0800583 if (mAllowOverScroll) {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700584 mWasInOverscroll = true;
Sunny Goyal70660032015-05-14 00:07:08 -0700585 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -0700586 overScroll(x);
587 } else {
588 overScroll(x - mMaxScrollX);
589 }
Adam Cohen68d73932010-11-15 10:50:58 -0800590 }
591 } else {
Adam Cohenc2d6e892014-10-16 09:49:24 -0700592 if (mWasInOverscroll) {
593 overScroll(0);
594 mWasInOverscroll = false;
595 }
Adam Cohen68d73932010-11-15 10:50:58 -0800596 super.scrollTo(x, y);
597 }
598
Adam Cohen7d30a372013-07-01 17:03:59 -0700599 // Update the last motion events when scrolling
600 if (isReordering(true)) {
601 float[] p = mapPointFromParentToView(this, mParentDownMotionX, mParentDownMotionY);
602 mLastMotionX = p[0];
603 mLastMotionY = p[1];
604 updateDragViewTranslationDuringDrag();
605 }
Michael Jurka0142d492010-08-25 17:46:15 -0700606 }
607
Adam Cohen53805212013-10-01 10:39:23 -0700608 private void sendScrollAccessibilityEvent() {
609 AccessibilityManager am =
610 (AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
611 if (am.isEnabled()) {
Vadim Tryshevf4715972015-05-08 17:21:03 -0700612 if (mCurrentPage != getNextPage()) {
613 AccessibilityEvent ev =
614 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_VIEW_SCROLLED);
Vadim Tryshev7066c122015-05-21 14:06:35 -0700615 ev.setScrollable(true);
616 ev.setScrollX(getScrollX());
617 ev.setScrollY(getScrollY());
618 ev.setMaxScrollX(mMaxScrollX);
619 ev.setMaxScrollY(0);
Adam Cohen53805212013-10-01 10:39:23 -0700620
Vadim Tryshevf4715972015-05-08 17:21:03 -0700621 sendAccessibilityEventUnchecked(ev);
Adam Cohen53805212013-10-01 10:39:23 -0700622 }
Adam Cohen53805212013-10-01 10:39:23 -0700623 }
624 }
625
Michael Jurka0142d492010-08-25 17:46:15 -0700626 // we moved this functionality to a helper function so SmoothPagedView can reuse it
627 protected boolean computeScrollHelper() {
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800628 return computeScrollHelper(true);
629 }
630
631 protected boolean computeScrollHelper(boolean shouldInvalidate) {
Winson Chung321e9ee2010-08-09 13:37:56 -0700632 if (mScroller.computeScrollOffset()) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700633 // Don't bother scrolling if the page does not need to be moved
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 if (getScrollX() != mScroller.getCurrX()
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800635 || getScrollY() != mScroller.getCurrY()) {
Adam Cohenf358a4b2013-07-23 16:47:31 -0700636 float scaleX = mFreeScroll ? getScaleX() : 1f;
637 int scrollX = (int) (mScroller.getCurrX() * (1 / scaleX));
638 scrollTo(scrollX, mScroller.getCurrY());
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 }
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800640 if (shouldInvalidate) {
641 invalidate();
642 }
Michael Jurka0142d492010-08-25 17:46:15 -0700643 return true;
Tony Wickham95cdb3a2016-02-18 14:37:07 -0800644 } else if (mNextPage != INVALID_PAGE && shouldInvalidate) {
Adam Cohen53805212013-10-01 10:39:23 -0700645 sendScrollAccessibilityEvent();
646
Adam Cohen6f127a62014-06-12 14:54:41 -0700647 mCurrentPage = validateNewPage(mNextPage);
Winson Chung86f77532010-08-24 11:08:22 -0700648 mNextPage = INVALID_PAGE;
Michael Jurka0142d492010-08-25 17:46:15 -0700649 notifyPageSwitchListener();
Winson Chungb44b5242011-06-13 11:32:14 -0700650
Adam Cohen73aa9752010-11-24 16:26:58 -0800651 // We don't want to trigger a page end moving unless the page has settled
652 // and the user has stopped scrolling
653 if (mTouchState == TOUCH_STATE_REST) {
654 pageEndMoving();
655 }
Winson Chungc27d1bb2011-09-29 12:07:42 -0700656
Adam Cohen7d30a372013-07-01 17:03:59 -0700657 onPostReorderingAnimationCompleted();
Adam Cohen53805212013-10-01 10:39:23 -0700658 AccessibilityManager am = (AccessibilityManager)
Adam Cohen0ffac432013-07-10 11:19:26 -0700659 getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
Adam Cohen53805212013-10-01 10:39:23 -0700660 if (am.isEnabled()) {
661 // Notify the user when the page changes
662 announceForAccessibility(getCurrentPageDescription());
Adam Cohen0ffac432013-07-10 11:19:26 -0700663 }
Michael Jurka0142d492010-08-25 17:46:15 -0700664 return true;
Winson Chung321e9ee2010-08-09 13:37:56 -0700665 }
Michael Jurka0142d492010-08-25 17:46:15 -0700666 return false;
667 }
668
669 @Override
670 public void computeScroll() {
671 computeScrollHelper();
Winson Chung321e9ee2010-08-09 13:37:56 -0700672 }
673
Adam Cohen96d30a12013-07-16 18:13:21 -0700674 public static class LayoutParams extends ViewGroup.LayoutParams {
675 public boolean isFullScreenPage = false;
676
677 /**
678 * {@inheritDoc}
679 */
680 public LayoutParams(int width, int height) {
681 super(width, height);
682 }
683
Sunny Goyal41b22c02015-05-14 15:20:48 -0700684 public LayoutParams(Context context, AttributeSet attrs) {
685 super(context, attrs);
686 }
687
Adam Cohen96d30a12013-07-16 18:13:21 -0700688 public LayoutParams(ViewGroup.LayoutParams source) {
689 super(source);
690 }
691 }
692
Sunny Goyal41b22c02015-05-14 15:20:48 -0700693 @Override
694 public LayoutParams generateLayoutParams(AttributeSet attrs) {
695 return new LayoutParams(getContext(), attrs);
696 }
697
698 @Override
Adam Cohen96d30a12013-07-16 18:13:21 -0700699 protected LayoutParams generateDefaultLayoutParams() {
700 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
701 }
702
Sunny Goyal41b22c02015-05-14 15:20:48 -0700703 @Override
704 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
705 return new LayoutParams(p);
706 }
707
708 @Override
709 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
710 return p instanceof LayoutParams;
711 }
712
Adam Cohenedb40762013-07-18 16:45:45 -0700713 public void addFullScreenPage(View page) {
Adam Cohen96d30a12013-07-16 18:13:21 -0700714 LayoutParams lp = generateDefaultLayoutParams();
715 lp.isFullScreenPage = true;
716 super.addView(page, 0, lp);
717 }
718
Adam Cohen410f3cd2013-09-22 12:09:32 -0700719 public int getNormalChildHeight() {
720 return mNormalChildHeight;
721 }
722
Winson Chung321e9ee2010-08-09 13:37:56 -0700723 @Override
724 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700725 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700726 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
727 return;
728 }
729
Adam Cohen7d30a372013-07-01 17:03:59 -0700730 // We measure the dimensions of the PagedView to be larger than the pages so that when we
731 // zoom out (and scale down), the view is still contained in the parent
Adam Cohen7d30a372013-07-01 17:03:59 -0700732 int widthMode = MeasureSpec.getMode(widthMeasureSpec);
733 int widthSize = MeasureSpec.getSize(widthMeasureSpec);
734 int heightMode = MeasureSpec.getMode(heightMeasureSpec);
Winson Chung8aad6102012-05-11 16:27:49 -0700735 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
Winson Chungc82d2622013-11-06 13:23:29 -0800736 // NOTE: We multiply by 2f to account for the fact that depending on the offset of the
Adam Cohen7d30a372013-07-01 17:03:59 -0700737 // viewport, we can be at most one and a half screens offset once we scale down
738 DisplayMetrics dm = getResources().getDisplayMetrics();
Adam Cohen3b185e22013-10-29 14:45:58 -0700739 int maxSize = Math.max(dm.widthPixels + mInsets.left + mInsets.right,
740 dm.heightPixels + mInsets.top + mInsets.bottom);
Adam Cohen410f3cd2013-09-22 12:09:32 -0700741
Winson Chungc82d2622013-11-06 13:23:29 -0800742 int parentWidthSize = (int) (2f * maxSize);
743 int parentHeightSize = (int) (2f * maxSize);
Winson Chungc58497e2013-09-03 17:48:37 -0700744 int scaledWidthSize, scaledHeightSize;
745 if (mUseMinScale) {
Winson Chungc58497e2013-09-03 17:48:37 -0700746 scaledWidthSize = (int) (parentWidthSize / mMinScale);
747 scaledHeightSize = (int) (parentHeightSize / mMinScale);
748 } else {
749 scaledWidthSize = widthSize;
750 scaledHeightSize = heightSize;
751 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700752 mViewport.set(0, 0, widthSize, heightSize);
753
754 if (widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
755 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
756 return;
Winson Chung321e9ee2010-08-09 13:37:56 -0700757 }
758
Winson Chung8aad6102012-05-11 16:27:49 -0700759 // Return early if we aren't given a proper dimension
760 if (widthSize <= 0 || heightSize <= 0) {
761 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
762 return;
763 }
764
Adam Lesinski6b879f02010-11-04 16:15:23 -0700765 /* Allow the height to be set as WRAP_CONTENT. This allows the particular case
766 * of the All apps view on XLarge displays to not take up more space then it needs. Width
767 * is still not allowed to be set as WRAP_CONTENT since many parts of the code expect
768 * each page to have the same width.
769 */
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 final int verticalPadding = getPaddingTop() + getPaddingBottom();
771 final int horizontalPadding = getPaddingLeft() + getPaddingRight();
Winson Chung321e9ee2010-08-09 13:37:56 -0700772
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700773 int referenceChildWidth = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -0700774 // The children are given the same width and height as the workspace
Michael Jurka5f1c5092010-09-03 14:15:02 -0700775 // unless they were set to WRAP_CONTENT
Winson Chung785d2eb2011-04-14 16:08:02 -0700776 if (DEBUG) Log.d(TAG, "PagedView.onMeasure(): " + widthSize + ", " + heightSize);
Adam Cohen7d30a372013-07-01 17:03:59 -0700777 if (DEBUG) Log.d(TAG, "PagedView.scaledSize: " + scaledWidthSize + ", " + scaledHeightSize);
778 if (DEBUG) Log.d(TAG, "PagedView.parentSize: " + parentWidthSize + ", " + parentHeightSize);
779 if (DEBUG) Log.d(TAG, "PagedView.horizontalPadding: " + horizontalPadding);
780 if (DEBUG) Log.d(TAG, "PagedView.verticalPadding: " + verticalPadding);
Winson Chung321e9ee2010-08-09 13:37:56 -0700781 final int childCount = getChildCount();
782 for (int i = 0; i < childCount; i++) {
Michael Jurka5f1c5092010-09-03 14:15:02 -0700783 // disallowing padding in paged view (just pass 0)
Adam Cohen22f823d2011-09-01 17:22:18 -0700784 final View child = getPageAt(i);
Vladimir Marko2824b072013-10-04 16:42:17 +0100785 if (child.getVisibility() != GONE) {
786 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
Michael Jurka5f1c5092010-09-03 14:15:02 -0700787
Vladimir Marko2824b072013-10-04 16:42:17 +0100788 int childWidthMode;
789 int childHeightMode;
790 int childWidth;
791 int childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700792
Vladimir Marko2824b072013-10-04 16:42:17 +0100793 if (!lp.isFullScreenPage) {
794 if (lp.width == LayoutParams.WRAP_CONTENT) {
795 childWidthMode = MeasureSpec.AT_MOST;
796 } else {
797 childWidthMode = MeasureSpec.EXACTLY;
798 }
799
800 if (lp.height == LayoutParams.WRAP_CONTENT) {
801 childHeightMode = MeasureSpec.AT_MOST;
802 } else {
803 childHeightMode = MeasureSpec.EXACTLY;
804 }
805
Adam Cohen3b185e22013-10-29 14:45:58 -0700806 childWidth = getViewportWidth() - horizontalPadding
807 - mInsets.left - mInsets.right;
808 childHeight = getViewportHeight() - verticalPadding
809 - mInsets.top - mInsets.bottom;
Vladimir Marko2824b072013-10-04 16:42:17 +0100810 mNormalChildHeight = childHeight;
Adam Cohen96d30a12013-07-16 18:13:21 -0700811 } else {
812 childWidthMode = MeasureSpec.EXACTLY;
Adam Cohen96d30a12013-07-16 18:13:21 -0700813 childHeightMode = MeasureSpec.EXACTLY;
Vladimir Marko2824b072013-10-04 16:42:17 +0100814
Sunny Goyalecdc24f2016-01-12 10:35:32 -0800815 childWidth = getViewportWidth();
Adam Cohen3b185e22013-10-29 14:45:58 -0700816 childHeight = getViewportHeight();
Adam Cohen96d30a12013-07-16 18:13:21 -0700817 }
Adam Cohen1d3d4f12014-08-14 19:14:52 -0700818 if (referenceChildWidth == 0) {
819 referenceChildWidth = childWidth;
820 }
Adam Cohen96d30a12013-07-16 18:13:21 -0700821
Vladimir Marko2824b072013-10-04 16:42:17 +0100822 final int childWidthMeasureSpec =
823 MeasureSpec.makeMeasureSpec(childWidth, childWidthMode);
824 final int childHeightMeasureSpec =
825 MeasureSpec.makeMeasureSpec(childHeight, childHeightMode);
826 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
Michael Jurka5f1c5092010-09-03 14:15:02 -0700827 }
Adam Lesinski6b879f02010-11-04 16:15:23 -0700828 }
Adam Cohen7d30a372013-07-01 17:03:59 -0700829 setMeasuredDimension(scaledWidthSize, scaledHeightSize);
Adam Cohen60b07122011-11-14 17:26:06 -0800830 }
831
Sunny Goyalcf25b522015-07-09 00:01:18 -0700832 @SuppressLint("DrawAllocation")
Winson Chung321e9ee2010-08-09 13:37:56 -0700833 @Override
Michael Jurka28750fb2010-09-24 17:43:49 -0700834 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700835 if (getChildCount() == 0) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700836 return;
837 }
838
Winson Chung785d2eb2011-04-14 16:08:02 -0700839 if (DEBUG) Log.d(TAG, "PagedView.onLayout()");
Winson Chung321e9ee2010-08-09 13:37:56 -0700840 final int childCount = getChildCount();
Winson Chung321e9ee2010-08-09 13:37:56 -0700841
Adam Cohen7d30a372013-07-01 17:03:59 -0700842 int offsetX = getViewportOffsetX();
843 int offsetY = getViewportOffsetY();
844
845 // Update the viewport offsets
Vadim Tryshev7af0d442015-05-15 08:48:11 -0700846 mViewport.offset(offsetX, offsetY);
Adam Cohen7d30a372013-07-01 17:03:59 -0700847
Sunny Goyal70660032015-05-14 00:07:08 -0700848 final int startIndex = mIsRtl ? childCount - 1 : 0;
849 final int endIndex = mIsRtl ? -1 : childCount;
850 final int delta = mIsRtl ? -1 : 1;
Adam Cohen0ffac432013-07-10 11:19:26 -0700851
Adam Cohen7d30a372013-07-01 17:03:59 -0700852 int verticalPadding = getPaddingTop() + getPaddingBottom();
Adam Cohenedb40762013-07-18 16:45:45 -0700853
Adam Cohen3b185e22013-10-29 14:45:58 -0700854 LayoutParams lp = (LayoutParams) getChildAt(startIndex).getLayoutParams();
Adam Cohen84a465a2013-11-11 18:49:56 +0000855 LayoutParams nextLp;
Adam Cohen3b185e22013-10-29 14:45:58 -0700856
857 int childLeft = offsetX + (lp.isFullScreenPage ? 0 : getPaddingLeft());
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700858 if (mPageScrolls == null || childCount != mChildCountOnLastLayout) {
859 mPageScrolls = new int[childCount];
Adam Cohenedb40762013-07-18 16:45:45 -0700860 }
861
Adam Cohen0ffac432013-07-10 11:19:26 -0700862 for (int i = startIndex; i != endIndex; i += delta) {
Adam Cohen22f823d2011-09-01 17:22:18 -0700863 final View child = getPageAt(i);
Winson Chung321e9ee2010-08-09 13:37:56 -0700864 if (child.getVisibility() != View.GONE) {
Adam Cohen3b185e22013-10-29 14:45:58 -0700865 lp = (LayoutParams) child.getLayoutParams();
Vladimir Marko2824b072013-10-04 16:42:17 +0100866 int childTop;
867 if (lp.isFullScreenPage) {
868 childTop = offsetY;
869 } else {
870 childTop = offsetY + getPaddingTop() + mInsets.top;
Sunny Goyalcf25b522015-07-09 00:01:18 -0700871 childTop += (getViewportHeight() - mInsets.top - mInsets.bottom - verticalPadding - child.getMeasuredHeight()) / 2;
Vladimir Marko2824b072013-10-04 16:42:17 +0100872 }
873
Adam Cohen96d30a12013-07-16 18:13:21 -0700874 final int childWidth = child.getMeasuredWidth();
Adam Lesinski6b879f02010-11-04 16:15:23 -0700875 final int childHeight = child.getMeasuredHeight();
Michael Jurkad3ef3062010-11-23 16:23:58 -0800876
Winson Chung785d2eb2011-04-14 16:08:02 -0700877 if (DEBUG) Log.d(TAG, "\tlayout-child" + i + ": " + childLeft + ", " + childTop);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700878 child.layout(childLeft, childTop,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800879 childLeft + child.getMeasuredWidth(), childTop + childHeight);
Adam Cohenedb40762013-07-18 16:45:45 -0700880
Adam Cohen3b185e22013-10-29 14:45:58 -0700881 int scrollOffsetLeft = lp.isFullScreenPage ? 0 : getPaddingLeft();
882 mPageScrolls[i] = childLeft - scrollOffsetLeft - offsetX;
Adam Cohen84a465a2013-11-11 18:49:56 +0000883
884 int pageGap = mPageSpacing;
885 int next = i + delta;
886 if (next != endIndex) {
887 nextLp = (LayoutParams) getPageAt(next).getLayoutParams();
888 } else {
889 nextLp = null;
890 }
891
892 // Prevent full screen pages from showing in the viewport
893 // when they are not the current page.
894 if (lp.isFullScreenPage) {
895 pageGap = getPaddingLeft();
896 } else if (nextLp != null && nextLp.isFullScreenPage) {
897 pageGap = getPaddingRight();
898 }
899
Sunny Goyala1fbd842015-05-20 13:40:27 -0700900 childLeft += childWidth + pageGap + getChildGap();
Winson Chung321e9ee2010-08-09 13:37:56 -0700901 }
902 }
Winson Chungc3665fa2011-09-14 17:56:27 -0700903
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700904 final LayoutTransition transition = getLayoutTransition();
905 // If the transition is running defer updating max scroll, as some empty pages could
906 // still be present, and a max scroll change could cause sudden jumps in scroll.
907 if (transition != null && transition.isRunning()) {
908 transition.addTransitionListener(new LayoutTransition.TransitionListener() {
909
910 @Override
911 public void startTransition(LayoutTransition transition, ViewGroup container,
912 View view, int transitionType) { }
913
914 @Override
915 public void endTransition(LayoutTransition transition, ViewGroup container,
916 View view, int transitionType) {
917 // Wait until all transitions are complete.
918 if (!transition.isRunning()) {
919 transition.removeTransitionListener(this);
920 updateMaxScrollX();
921 }
922 }
923 });
Adam Cohenf698c6e2013-07-17 18:44:25 -0700924 } else {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700925 updateMaxScrollX();
Adam Cohenf698c6e2013-07-17 18:44:25 -0700926 }
927
Sunny Goyalcb037ee2015-07-08 16:41:21 -0700928 if (mFirstLayout && mCurrentPage >= 0 && mCurrentPage < childCount) {
929 updateCurrentPageScroll();
930 mFirstLayout = false;
931 }
932
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700933 if (mScroller.isFinished() && mChildCountOnLastLayout != childCount) {
Adam Cohen21cd0022013-10-09 18:57:02 -0700934 if (mRestorePage != INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -0700935 setCurrentPage(mRestorePage);
Adam Cohen21cd0022013-10-09 18:57:02 -0700936 mRestorePage = INVALID_RESTORE_PAGE;
Winson Chung8c87cd82013-07-23 16:20:10 -0700937 } else {
938 setCurrentPage(getNextPage());
939 }
Adam Cohenf698c6e2013-07-17 18:44:25 -0700940 }
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700941 mChildCountOnLastLayout = childCount;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700942
943 if (isReordering(true)) {
944 updateDragViewTranslationDuringDrag();
945 }
Winson Chunge3193b92010-09-10 11:44:42 -0700946 }
947
Sunny Goyala1fbd842015-05-20 13:40:27 -0700948 protected int getChildGap() {
949 return 0;
950 }
951
Sunny Goyal316490e2015-06-02 09:38:28 -0700952 @Thunk void updateMaxScrollX() {
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700953 int childCount = getChildCount();
954 if (childCount > 0) {
Sunny Goyal70660032015-05-14 00:07:08 -0700955 final int index = mIsRtl ? 0 : childCount - 1;
Sunny Goyal3e2ff8a2015-04-15 16:31:22 -0700956 mMaxScrollX = getScrollForPage(index);
957 } else {
958 mMaxScrollX = 0;
959 }
960 }
961
Adam Cohen3b185e22013-10-29 14:45:58 -0700962 public void setPageSpacing(int pageSpacing) {
963 mPageSpacing = pageSpacing;
964 requestLayout();
965 }
966
Sunny Goyal4d113a52015-05-27 10:05:28 -0700967 /**
968 * Called when the center screen changes during scrolling.
969 */
970 protected void screenScrolled(int screenCenter) { }
Adam Cohenf34bab52010-09-30 14:11:56 -0700971
Winson Chunge3193b92010-09-10 11:44:42 -0700972 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700973 public void onChildViewAdded(View parent, View child) {
Winson Chungd2be3812013-07-16 11:11:32 -0700974 // Update the page indicator, we don't update the page indicator as we
975 // add/remove pages
976 if (mPageIndicator != null && !isReordering(false)) {
Winson Chung82dfe582013-07-26 15:07:49 -0700977 int pageIndex = indexOfChild(child);
Winson Chung7819a562013-09-19 15:55:45 -0700978 mPageIndicator.addMarker(pageIndex,
979 getPageIndicatorMarker(pageIndex),
Sunny Goyal8e2133b2015-05-06 13:39:07 -0700980 true);
Winson Chungd2be3812013-07-16 11:11:32 -0700981 }
982
Adam Cohen2591f6a2011-10-25 14:36:40 -0700983 // This ensures that when children are added, they get the correct transforms / alphas
984 // in accordance with any scroll effects.
985 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700986 updateFreescrollBounds();
Adam Cohen2591f6a2011-10-25 14:36:40 -0700987 invalidate();
988 }
989
Michael Jurka8b805b12012-04-18 14:23:14 -0700990 @Override
991 public void onChildViewRemoved(View parent, View child) {
Adam Cohen7d30a372013-07-01 17:03:59 -0700992 mForceScreenScrolled = true;
Adam Cohen7a9e58a2013-10-01 18:02:13 -0700993 updateFreescrollBounds();
Adam Cohen7d30a372013-07-01 17:03:59 -0700994 invalidate();
Michael Jurka8b805b12012-04-18 14:23:14 -0700995 }
996
Winson Chungd2be3812013-07-16 11:11:32 -0700997 private void removeMarkerForView(int index) {
998 // Update the page indicator, we don't update the page indicator as we
999 // add/remove pages
1000 if (mPageIndicator != null && !isReordering(false)) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001001 mPageIndicator.removeMarker(index, true);
Winson Chungd2be3812013-07-16 11:11:32 -07001002 }
1003 }
1004
1005 @Override
1006 public void removeView(View v) {
1007 // XXX: We should find a better way to hook into this before the view
1008 // gets removed form its parent...
1009 removeMarkerForView(indexOfChild(v));
1010 super.removeView(v);
1011 }
1012 @Override
1013 public void removeViewInLayout(View v) {
1014 // XXX: We should find a better way to hook into this before the view
1015 // gets removed form its parent...
1016 removeMarkerForView(indexOfChild(v));
1017 super.removeViewInLayout(v);
1018 }
1019 @Override
1020 public void removeViewAt(int index) {
1021 // XXX: We should find a better way to hook into this before the view
1022 // gets removed form its parent...
Adam Cohen5b139a52015-06-15 11:16:18 -07001023 removeMarkerForView(index);
Winson Chungd2be3812013-07-16 11:11:32 -07001024 super.removeViewAt(index);
1025 }
1026 @Override
1027 public void removeAllViewsInLayout() {
1028 // Update the page indicator, we don't update the page indicator as we
1029 // add/remove pages
1030 if (mPageIndicator != null) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001031 mPageIndicator.removeAllMarkers(true);
Winson Chungd2be3812013-07-16 11:11:32 -07001032 }
1033
1034 super.removeAllViewsInLayout();
1035 }
1036
Adam Cohen73894962011-10-31 13:17:17 -07001037 protected int getChildOffset(int index) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001038 if (index < 0 || index > getChildCount() - 1) return 0;
1039
Adam Cohenf698c6e2013-07-17 18:44:25 -07001040 int offset = getPageAt(index).getLeft() - getViewportOffsetX();
Adam Cohen73894962011-10-31 13:17:17 -07001041
Adam Cohenf698c6e2013-07-17 18:44:25 -07001042 return offset;
Adam Cohen73894962011-10-31 13:17:17 -07001043 }
1044
Adam Cohen6f127a62014-06-12 14:54:41 -07001045 protected void getFreeScrollPageRange(int[] range) {
Winson Chungc9ca2982013-07-19 12:07:38 -07001046 range[0] = 0;
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001047 range[1] = Math.max(0, getChildCount() - 1);
Adam Cohen7d30a372013-07-01 17:03:59 -07001048 }
1049
Michael Jurkadde558b2011-11-09 22:09:06 -08001050 protected void getVisiblePages(int[] range) {
Sunny Goyalee688162016-02-12 14:24:21 -08001051 final int count = getChildCount();
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001052 range[0] = -1;
1053 range[1] = -1;
1054
Sunny Goyalee688162016-02-12 14:24:21 -08001055 if (count > 0) {
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001056 final int visibleLeft = -getLeft();
1057 final int visibleRight = visibleLeft + getViewportWidth();
Sunny Goyalee688162016-02-12 14:24:21 -08001058 final Matrix pageShiftMatrix = getPageShiftMatrix();
1059 int curScreen = 0;
Adam Cohen7d30a372013-07-01 17:03:59 -07001060
Sunny Goyalee688162016-02-12 14:24:21 -08001061 for (int i = 0; i < count; i++) {
1062 View currPage = getPageAt(i);
Winson Chungc9ca2982013-07-19 12:07:38 -07001063
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001064 // Verify if the page bounds are within the visible range.
1065 sTmpRectF.left = 0;
1066 sTmpRectF.right = currPage.getMeasuredWidth();
1067 currPage.getMatrix().mapRect(sTmpRectF);
1068 sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
Sunny Goyalee688162016-02-12 14:24:21 -08001069 pageShiftMatrix.mapRect(sTmpRectF);
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001070
Sunny Goyal8bf6f312016-01-23 14:40:35 -08001071 if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001072 if (range[0] == -1) {
1073 continue;
1074 } else {
1075 break;
1076 }
1077 }
Sunny Goyalee688162016-02-12 14:24:21 -08001078 curScreen = i;
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001079 if (range[0] < 0) {
Sunny Goyalee688162016-02-12 14:24:21 -08001080 range[0] = curScreen;
Winson Chungc9ca2982013-07-19 12:07:38 -07001081 }
1082 }
Adam Cohenaf9b0e52013-09-23 19:27:38 -07001083
Sunny Goyalee688162016-02-12 14:24:21 -08001084 range[1] = curScreen;
Michael Jurkadde558b2011-11-09 22:09:06 -08001085 } else {
1086 range[0] = -1;
1087 range[1] = -1;
1088 }
1089 }
1090
Sunny Goyalee688162016-02-12 14:24:21 -08001091 protected Matrix getPageShiftMatrix() {
1092 return getMatrix();
1093 }
1094
Michael Jurka920d7f42012-05-14 16:29:55 -07001095 protected boolean shouldDrawChild(View child) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07001096 return child.getVisibility() == VISIBLE;
Michael Jurka920d7f42012-05-14 16:29:55 -07001097 }
1098
Michael Jurkadde558b2011-11-09 22:09:06 -08001099 @Override
1100 protected void dispatchDraw(Canvas canvas) {
Michael Jurkadde558b2011-11-09 22:09:06 -08001101 // Find out which screens are visible; as an optimization we only call draw on them
1102 final int pageCount = getChildCount();
1103 if (pageCount > 0) {
Adam Cohen4de09742013-12-12 16:16:39 -08001104 int halfScreenSize = getViewportWidth() / 2;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001105 int screenCenter = getScrollX() + halfScreenSize;
Adam Cohen4de09742013-12-12 16:16:39 -08001106
1107 if (screenCenter != mLastScreenCenter || mForceScreenScrolled) {
1108 // set mForceScreenScrolled before calling screenScrolled so that screenScrolled can
1109 // set it for the next frame
1110 mForceScreenScrolled = false;
1111 screenScrolled(screenCenter);
1112 mLastScreenCenter = screenCenter;
1113 }
1114
Michael Jurkadde558b2011-11-09 22:09:06 -08001115 getVisiblePages(mTempVisiblePagesRange);
1116 final int leftScreen = mTempVisiblePagesRange[0];
1117 final int rightScreen = mTempVisiblePagesRange[1];
Winson Chungc6f10b92011-11-14 11:39:07 -08001118 if (leftScreen != -1 && rightScreen != -1) {
1119 final long drawingTime = getDrawingTime();
1120 // Clip to the bounds
1121 canvas.save();
Michael Jurka8b805b12012-04-18 14:23:14 -07001122 canvas.clipRect(getScrollX(), getScrollY(), getScrollX() + getRight() - getLeft(),
1123 getScrollY() + getBottom() - getTop());
Michael Jurka0142d492010-08-25 17:46:15 -07001124
Adam Cohen7d30a372013-07-01 17:03:59 -07001125 // Draw all the children, leaving the drag view for last
1126 for (int i = pageCount - 1; i >= 0; i--) {
Michael Jurka80c69852011-12-16 14:16:32 -08001127 final View v = getPageAt(i);
Adam Cohen7d30a372013-07-01 17:03:59 -07001128 if (v == mDragView) continue;
Sunny Goyalcf25b522015-07-09 00:01:18 -07001129 if (leftScreen <= i && i <= rightScreen && shouldDrawChild(v)) {
Michael Jurka80c69852011-12-16 14:16:32 -08001130 drawChild(canvas, v, drawingTime);
Michael Jurka80c69852011-12-16 14:16:32 -08001131 }
Winson Chungc6f10b92011-11-14 11:39:07 -08001132 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001133 // Draw the drag view on top (if there is one)
1134 if (mDragView != null) {
1135 drawChild(canvas, mDragView, drawingTime);
1136 }
1137
Winson Chungc6f10b92011-11-14 11:39:07 -08001138 canvas.restore();
Michael Jurkac4fb9172010-09-02 17:19:20 -07001139 }
Michael Jurka0142d492010-08-25 17:46:15 -07001140 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001141 }
1142
1143 @Override
Sunny Goyal4d113a52015-05-27 10:05:28 -07001144 public void draw(Canvas canvas) {
1145 super.draw(canvas);
1146 if (getPageCount() > 0) {
1147 if (!mEdgeGlowLeft.isFinished()) {
1148 final int restoreCount = canvas.save();
1149 Rect display = mViewport;
1150 canvas.translate(display.left, display.top);
1151 canvas.rotate(270);
1152
1153 getEdgeVerticalPostion(sTmpIntPoint);
1154 canvas.translate(display.top - sTmpIntPoint[1], 0);
1155 mEdgeGlowLeft.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
1156 if (mEdgeGlowLeft.draw(canvas)) {
1157 postInvalidateOnAnimation();
1158 }
1159 canvas.restoreToCount(restoreCount);
1160 }
1161 if (!mEdgeGlowRight.isFinished()) {
1162 final int restoreCount = canvas.save();
1163 Rect display = mViewport;
Sunny Goyalcb037ee2015-07-08 16:41:21 -07001164 canvas.translate(display.left + mPageScrolls[mIsRtl ? 0 : (getPageCount() - 1)], display.top);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001165 canvas.rotate(90);
1166
1167 getEdgeVerticalPostion(sTmpIntPoint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -07001168
Sunny Goyal93264612015-11-23 11:47:50 -08001169 canvas.translate(sTmpIntPoint[0] - display.top, -display.width());
1170 mEdgeGlowRight.setSize(sTmpIntPoint[1] - sTmpIntPoint[0], display.width());
Sunny Goyal4d113a52015-05-27 10:05:28 -07001171 if (mEdgeGlowRight.draw(canvas)) {
1172 postInvalidateOnAnimation();
1173 }
1174 canvas.restoreToCount(restoreCount);
1175 }
1176 }
1177 }
1178
1179 /**
1180 * Returns the top and bottom position for the edge effect.
1181 */
1182 protected abstract void getEdgeVerticalPostion(int[] pos);
1183
1184 @Override
Winson Chung321e9ee2010-08-09 13:37:56 -07001185 public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) {
Adam Cohenae4f1552011-10-20 00:15:42 -07001186 int page = indexToPage(indexOfChild(child));
Winson Chung86f77532010-08-24 11:08:22 -07001187 if (page != mCurrentPage || !mScroller.isFinished()) {
1188 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001189 return true;
1190 }
1191 return false;
1192 }
1193
1194 @Override
1195 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
Winson Chung86f77532010-08-24 11:08:22 -07001196 int focusablePage;
1197 if (mNextPage != INVALID_PAGE) {
1198 focusablePage = mNextPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001199 } else {
Winson Chung86f77532010-08-24 11:08:22 -07001200 focusablePage = mCurrentPage;
Winson Chung321e9ee2010-08-09 13:37:56 -07001201 }
Winson Chung86f77532010-08-24 11:08:22 -07001202 View v = getPageAt(focusablePage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001203 if (v != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001204 return v.requestFocus(direction, previouslyFocusedRect);
Winson Chung321e9ee2010-08-09 13:37:56 -07001205 }
1206 return false;
1207 }
1208
1209 @Override
1210 public boolean dispatchUnhandledMove(View focused, int direction) {
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001211 if (super.dispatchUnhandledMove(focused, direction)) {
1212 return true;
1213 }
1214
1215 if (mIsRtl) {
1216 if (direction == View.FOCUS_LEFT) {
1217 direction = View.FOCUS_RIGHT;
1218 } else if (direction == View.FOCUS_RIGHT) {
1219 direction = View.FOCUS_LEFT;
1220 }
1221 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001222 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001223 if (getCurrentPage() > 0) {
1224 snapToPage(getCurrentPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001225 return true;
1226 }
1227 } else if (direction == View.FOCUS_RIGHT) {
Winson Chung86f77532010-08-24 11:08:22 -07001228 if (getCurrentPage() < getPageCount() - 1) {
1229 snapToPage(getCurrentPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07001230 return true;
1231 }
1232 }
Sunny Goyal0c4e3722015-12-01 13:21:49 -08001233 return false;
Winson Chung321e9ee2010-08-09 13:37:56 -07001234 }
1235
1236 @Override
1237 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001238 // XXX-RTL: This will be fixed in a future CL
Winson Chung86f77532010-08-24 11:08:22 -07001239 if (mCurrentPage >= 0 && mCurrentPage < getPageCount()) {
alanvaf519952012-05-07 17:33:22 -07001240 getPageAt(mCurrentPage).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001241 }
1242 if (direction == View.FOCUS_LEFT) {
Winson Chung86f77532010-08-24 11:08:22 -07001243 if (mCurrentPage > 0) {
alanvaf519952012-05-07 17:33:22 -07001244 getPageAt(mCurrentPage - 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001245 }
1246 } else if (direction == View.FOCUS_RIGHT){
Winson Chung86f77532010-08-24 11:08:22 -07001247 if (mCurrentPage < getPageCount() - 1) {
alanvaf519952012-05-07 17:33:22 -07001248 getPageAt(mCurrentPage + 1).addFocusables(views, direction, focusableMode);
Winson Chung321e9ee2010-08-09 13:37:56 -07001249 }
1250 }
1251 }
1252
1253 /**
1254 * If one of our descendant views decides that it could be focused now, only
Winson Chung86f77532010-08-24 11:08:22 -07001255 * pass that along if it's on the current page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001256 *
Winson Chung86f77532010-08-24 11:08:22 -07001257 * This happens when live folders requery, and if they're off page, they
1258 * end up calling requestFocus, which pulls it on page.
Winson Chung321e9ee2010-08-09 13:37:56 -07001259 */
1260 @Override
1261 public void focusableViewAvailable(View focused) {
Winson Chung86f77532010-08-24 11:08:22 -07001262 View current = getPageAt(mCurrentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001263 View v = focused;
1264 while (true) {
1265 if (v == current) {
1266 super.focusableViewAvailable(focused);
1267 return;
1268 }
1269 if (v == this) {
1270 return;
1271 }
1272 ViewParent parent = v.getParent();
1273 if (parent instanceof View) {
1274 v = (View)v.getParent();
1275 } else {
1276 return;
1277 }
1278 }
1279 }
1280
1281 /**
1282 * {@inheritDoc}
1283 */
1284 @Override
1285 public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
1286 if (disallowIntercept) {
1287 // We need to make sure to cancel our long press if
1288 // a scrollable widget takes over touch events
Adam Cohen22f823d2011-09-01 17:22:18 -07001289 final View currentPage = getPageAt(mCurrentPage);
Winson Chung86f77532010-08-24 11:08:22 -07001290 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001291 }
1292 super.requestDisallowInterceptTouchEvent(disallowIntercept);
1293 }
1294
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001295 /**
1296 * Return true if a tap at (x, y) should trigger a flip to the previous page.
1297 */
1298 protected boolean hitsPreviousPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001299 if (mIsRtl) {
Adam Cohen0ffac432013-07-10 11:19:26 -07001300 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001301 getPaddingRight() - mPageSpacing));
Adam Cohen0ffac432013-07-10 11:19:26 -07001302 }
Adam Cohen3b185e22013-10-29 14:45:58 -07001303 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001304 }
1305
1306 /**
1307 * Return true if a tap at (x, y) should trigger a flip to the next page.
1308 */
1309 protected boolean hitsNextPage(float x, float y) {
Sunny Goyal70660032015-05-14 00:07:08 -07001310 if (mIsRtl) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001311 return (x < getViewportOffsetX() + getPaddingLeft() + mPageSpacing);
Adam Cohen0ffac432013-07-10 11:19:26 -07001312 }
1313 return (x > (getViewportOffsetX() + getViewportWidth() -
Adam Cohen3b185e22013-10-29 14:45:58 -07001314 getPaddingRight() - mPageSpacing));
Adam Cohen7d30a372013-07-01 17:03:59 -07001315 }
Winson Chung52aee602013-01-30 12:01:02 -08001316
Adam Cohen7d30a372013-07-01 17:03:59 -07001317 /** Returns whether x and y originated within the buffered viewport */
1318 private boolean isTouchPointInViewportWithBuffer(int x, int y) {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001319 sTmpRect.set(mViewport.left - mViewport.width() / 2, mViewport.top,
Adam Cohen7d30a372013-07-01 17:03:59 -07001320 mViewport.right + mViewport.width() / 2, mViewport.bottom);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001321 return sTmpRect.contains(x, y);
Adam Cohen7d30a372013-07-01 17:03:59 -07001322 }
1323
Winson Chung321e9ee2010-08-09 13:37:56 -07001324 @Override
1325 public boolean onInterceptTouchEvent(MotionEvent ev) {
1326 /*
1327 * This method JUST determines whether we want to intercept the motion.
1328 * If we return true, onTouchEvent will be called and we do the actual
1329 * scrolling there.
1330 */
Adam Cohen6342bba2011-03-10 11:33:35 -08001331 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001332
Winson Chung45e1d6e2010-11-09 17:19:49 -08001333 // Skip touch handling if there are no pages to swipe
1334 if (getChildCount() <= 0) return super.onInterceptTouchEvent(ev);
1335
Winson Chung321e9ee2010-08-09 13:37:56 -07001336 /*
1337 * Shortcut the most recurring case: the user is in the dragging
1338 * state and he is moving his finger. We want to intercept this
1339 * motion.
1340 */
1341 final int action = ev.getAction();
1342 if ((action == MotionEvent.ACTION_MOVE) &&
1343 (mTouchState == TOUCH_STATE_SCROLLING)) {
1344 return true;
1345 }
1346
Winson Chung321e9ee2010-08-09 13:37:56 -07001347 switch (action & MotionEvent.ACTION_MASK) {
1348 case MotionEvent.ACTION_MOVE: {
1349 /*
1350 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
1351 * whether the user has moved far enough from his original down touch.
1352 */
Michael Jurka1ff706b2010-09-14 17:35:20 -07001353 if (mActivePointerId != INVALID_POINTER) {
1354 determineScrollingStart(ev);
Michael Jurka1ff706b2010-09-14 17:35:20 -07001355 }
1356 // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
1357 // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
1358 // i.e. fall through to the next case (don't break)
1359 // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
1360 // while it's small- this was causing a crash before we checked for INVALID_POINTER)
Adam Cohen93c97562013-09-26 13:48:01 -07001361 break;
Winson Chung321e9ee2010-08-09 13:37:56 -07001362 }
1363
1364 case MotionEvent.ACTION_DOWN: {
1365 final float x = ev.getX();
1366 final float y = ev.getY();
1367 // Remember location of down touch
1368 mDownMotionX = x;
Adam Cohen7d30a372013-07-01 17:03:59 -07001369 mDownMotionY = y;
1370 mDownScrollX = getScrollX();
Winson Chung321e9ee2010-08-09 13:37:56 -07001371 mLastMotionX = x;
1372 mLastMotionY = y;
Adam Cohen7d30a372013-07-01 17:03:59 -07001373 float[] p = mapPointFromViewToParent(this, x, y);
1374 mParentDownMotionX = p[0];
1375 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001376 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001377 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001378 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001379
Winson Chung321e9ee2010-08-09 13:37:56 -07001380 /*
1381 * If being flinged and user touches the screen, initiate drag;
1382 * otherwise don't. mScroller.isFinished should be false when
1383 * being flinged.
1384 */
Michael Jurkafd177c12010-10-19 15:50:43 -07001385 final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
Adam Cohena7652152013-11-18 20:06:55 +00001386 final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop / 3);
Adam Cohen2da0a052013-11-08 06:28:17 -08001387
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001388 if (finishedScrolling) {
1389 mTouchState = TOUCH_STATE_REST;
Adam Cohen59b5c792013-12-04 16:09:07 -08001390 if (!mScroller.isFinished() && !mFreeScroll) {
Adam Cohena7652152013-11-18 20:06:55 +00001391 setCurrentPage(getNextPage());
1392 pageEndMoving();
Adam Cohen2da0a052013-11-08 06:28:17 -08001393 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001394 } else {
Adam Cohen7d30a372013-07-01 17:03:59 -07001395 if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
1396 mTouchState = TOUCH_STATE_SCROLLING;
1397 } else {
1398 mTouchState = TOUCH_STATE_REST;
1399 }
Winson Chung5f2aa4e2010-08-20 14:49:25 -07001400 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001401
Winson Chung321e9ee2010-08-09 13:37:56 -07001402 break;
1403 }
1404
Winson Chung321e9ee2010-08-09 13:37:56 -07001405 case MotionEvent.ACTION_UP:
Jeff Brown1d0867c2010-12-02 18:27:39 -08001406 case MotionEvent.ACTION_CANCEL:
Adam Cohen7d30a372013-07-01 17:03:59 -07001407 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001408 break;
1409
1410 case MotionEvent.ACTION_POINTER_UP:
1411 onSecondaryPointerUp(ev);
Adam Cohen6342bba2011-03-10 11:33:35 -08001412 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001413 break;
1414 }
1415
1416 /*
1417 * The only time we want to intercept motion events is if we are in the
1418 * drag mode.
1419 */
1420 return mTouchState != TOUCH_STATE_REST;
1421 }
1422
Adam Cohenf8d28232011-02-01 21:47:00 -08001423 protected void determineScrollingStart(MotionEvent ev) {
1424 determineScrollingStart(ev, 1.0f);
1425 }
1426
Winson Chung321e9ee2010-08-09 13:37:56 -07001427 /*
1428 * Determines if we should change the touch state to start scrolling after the
1429 * user moves their touch point too far.
1430 */
Adam Cohenf8d28232011-02-01 21:47:00 -08001431 protected void determineScrollingStart(MotionEvent ev, float touchSlopScale) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001432 // Disallow scrolling if we don't have a valid pointer index
Winson Chung321e9ee2010-08-09 13:37:56 -07001433 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001434 if (pointerIndex == -1) return;
1435
1436 // Disallow scrolling if we started the gesture from outside the viewport
Winson Chung321e9ee2010-08-09 13:37:56 -07001437 final float x = ev.getX(pointerIndex);
1438 final float y = ev.getY(pointerIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001439 if (!isTouchPointInViewportWithBuffer((int) x, (int) y)) return;
1440
Winson Chung321e9ee2010-08-09 13:37:56 -07001441 final int xDiff = (int) Math.abs(x - mLastMotionX);
Winson Chung321e9ee2010-08-09 13:37:56 -07001442
Adam Cohenf8d28232011-02-01 21:47:00 -08001443 final int touchSlop = Math.round(touchSlopScale * mTouchSlop);
Winson Chung321e9ee2010-08-09 13:37:56 -07001444 boolean xMoved = xDiff > touchSlop;
Winson Chung321e9ee2010-08-09 13:37:56 -07001445
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001446 if (xMoved) {
1447 // Scroll if the user moved far enough along the X axis
1448 mTouchState = TOUCH_STATE_SCROLLING;
1449 mTotalMotionX += Math.abs(mLastMotionX - x);
1450 mLastMotionX = x;
1451 mLastMotionXRemainder = 0;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001452 onScrollInteractionBegin();
1453 pageBeginMoving();
Tony Wickhamdadb3042016-02-24 11:07:00 -08001454 // Stop listening for things like pinches.
1455 requestDisallowInterceptTouchEvent(true);
Adam Cohenf8d28232011-02-01 21:47:00 -08001456 }
1457 }
1458
1459 protected void cancelCurrentPageLongPress() {
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001460 // Try canceling the long press. It could also have been scheduled
1461 // by a distant descendant, so use the mAllowLongPress flag to block
1462 // everything
1463 final View currentPage = getPageAt(mCurrentPage);
1464 if (currentPage != null) {
1465 currentPage.cancelLongPress();
Winson Chung321e9ee2010-08-09 13:37:56 -07001466 }
1467 }
1468
Adam Cohenb5ba0972011-09-07 18:02:31 -07001469 protected float getScrollProgress(int screenCenter, View v, int page) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001470 final int halfScreenSize = getViewportWidth() / 2;
Adam Cohenb5ba0972011-09-07 18:02:31 -07001471
Adam Cohenedb40762013-07-18 16:45:45 -07001472 int delta = screenCenter - (getScrollForPage(page) + halfScreenSize);
Adam Cohen3b185e22013-10-29 14:45:58 -07001473 int count = getChildCount();
1474
1475 final int totalDistance;
1476
1477 int adjacentPage = page + 1;
Sunny Goyal70660032015-05-14 00:07:08 -07001478 if ((delta < 0 && !mIsRtl) || (delta > 0 && mIsRtl)) {
Adam Cohen3b185e22013-10-29 14:45:58 -07001479 adjacentPage = page - 1;
1480 }
1481
1482 if (adjacentPage < 0 || adjacentPage > count - 1) {
1483 totalDistance = v.getMeasuredWidth() + mPageSpacing;
1484 } else {
1485 totalDistance = Math.abs(getScrollForPage(adjacentPage) - getScrollForPage(page));
1486 }
Adam Cohenb5ba0972011-09-07 18:02:31 -07001487
1488 float scrollProgress = delta / (totalDistance * 1.0f);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001489 scrollProgress = Math.min(scrollProgress, MAX_SCROLL_PROGRESS);
1490 scrollProgress = Math.max(scrollProgress, - MAX_SCROLL_PROGRESS);
Adam Cohenb5ba0972011-09-07 18:02:31 -07001491 return scrollProgress;
1492 }
1493
Adam Cohenedb40762013-07-18 16:45:45 -07001494 public int getScrollForPage(int index) {
Adam Cohen1f1f45d2013-10-02 09:40:18 -07001495 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
Adam Cohenedb40762013-07-18 16:45:45 -07001496 return 0;
1497 } else {
1498 return mPageScrolls[index];
1499 }
1500 }
1501
Adam Cohen564a2e72013-10-09 14:47:32 -07001502 // While layout transitions are occurring, a child's position may stray from its baseline
1503 // position. This method returns the magnitude of this stray at any given time.
1504 public int getLayoutTransitionOffsetForPage(int index) {
1505 if (mPageScrolls == null || index >= mPageScrolls.length || index < 0) {
1506 return 0;
1507 } else {
1508 View child = getChildAt(index);
Adam Cohen3b185e22013-10-29 14:45:58 -07001509
1510 int scrollOffset = 0;
1511 LayoutParams lp = (LayoutParams) child.getLayoutParams();
1512 if (!lp.isFullScreenPage) {
Sunny Goyal70660032015-05-14 00:07:08 -07001513 scrollOffset = mIsRtl ? getPaddingRight() : getPaddingLeft();
Adam Cohen3b185e22013-10-29 14:45:58 -07001514 }
1515
Adam Cohen564a2e72013-10-09 14:47:32 -07001516 int baselineX = mPageScrolls[index] + scrollOffset + getViewportOffsetX();
1517 return (int) (child.getX() - baselineX);
1518 }
1519 }
1520
Adam Cohenb5ba0972011-09-07 18:02:31 -07001521 protected void dampedOverScroll(float amount) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001522 int screenSize = getViewportWidth();
Adam Cohene0f66b52010-11-23 15:06:07 -08001523 float f = (amount / screenSize);
Sunny Goyal4d113a52015-05-27 10:05:28 -07001524 if (f < 0) {
1525 mEdgeGlowLeft.onPull(-f);
1526 } else if (f > 0) {
1527 mEdgeGlowRight.onPull(f);
Adam Cohen68d73932010-11-15 10:50:58 -08001528 } else {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001529 return;
Adam Cohen68d73932010-11-15 10:50:58 -08001530 }
1531 invalidate();
1532 }
1533
Adam Cohenb5ba0972011-09-07 18:02:31 -07001534 protected void overScroll(float amount) {
1535 dampedOverScroll(amount);
1536 }
1537
Winson Chungdc61c4d2015-04-20 18:26:57 -07001538 public void enableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001539 setEnableFreeScroll(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001540 }
1541
Winson Chungdc61c4d2015-04-20 18:26:57 -07001542 public void disableFreeScroll() {
Adam Cohenf9618852013-11-08 06:45:03 -08001543 setEnableFreeScroll(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001544 }
1545
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001546 void updateFreescrollBounds() {
Adam Cohen6f127a62014-06-12 14:54:41 -07001547 getFreeScrollPageRange(mTempVisiblePagesRange);
Sunny Goyal70660032015-05-14 00:07:08 -07001548 if (mIsRtl) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001549 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1550 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1551 } else {
1552 mFreeScrollMinScrollX = getScrollForPage(mTempVisiblePagesRange[0]);
1553 mFreeScrollMaxScrollX = getScrollForPage(mTempVisiblePagesRange[1]);
1554 }
1555 }
1556
Adam Cohenf9618852013-11-08 06:45:03 -08001557 private void setEnableFreeScroll(boolean freeScroll) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001558 mFreeScroll = freeScroll;
1559
Adam Cohenf9618852013-11-08 06:45:03 -08001560 if (mFreeScroll) {
Adam Cohen7a9e58a2013-10-01 18:02:13 -07001561 updateFreescrollBounds();
Adam Cohen6f127a62014-06-12 14:54:41 -07001562 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001563 if (getCurrentPage() < mTempVisiblePagesRange[0]) {
1564 setCurrentPage(mTempVisiblePagesRange[0]);
1565 } else if (getCurrentPage() > mTempVisiblePagesRange[1]) {
1566 setCurrentPage(mTempVisiblePagesRange[1]);
1567 }
1568 }
1569
1570 setEnableOverscroll(!freeScroll);
1571 }
1572
Sunny Goyal2f0ec852015-03-26 13:38:27 -07001573 protected void setEnableOverscroll(boolean enable) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001574 mAllowOverScroll = enable;
1575 }
1576
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001577 private int getNearestHoverOverPageIndex() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001578 if (mDragView != null) {
1579 int dragX = (int) (mDragView.getLeft() + (mDragView.getMeasuredWidth() / 2)
1580 + mDragView.getTranslationX());
Adam Cohen6f127a62014-06-12 14:54:41 -07001581 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001582 int minDistance = Integer.MAX_VALUE;
1583 int minIndex = indexOfChild(mDragView);
1584 for (int i = mTempVisiblePagesRange[0]; i <= mTempVisiblePagesRange[1]; i++) {
1585 View page = getPageAt(i);
1586 int pageX = (int) (page.getLeft() + page.getMeasuredWidth() / 2);
1587 int d = Math.abs(dragX - pageX);
1588 if (d < minDistance) {
1589 minIndex = i;
1590 minDistance = d;
1591 }
1592 }
1593 return minIndex;
1594 }
1595 return -1;
1596 }
1597
Winson Chung321e9ee2010-08-09 13:37:56 -07001598 @Override
1599 public boolean onTouchEvent(MotionEvent ev) {
Adam Cohen1697b792013-09-17 19:08:21 -07001600 super.onTouchEvent(ev);
1601
Winson Chung45e1d6e2010-11-09 17:19:49 -08001602 // Skip touch handling if there are no pages to swipe
1603 if (getChildCount() <= 0) return super.onTouchEvent(ev);
1604
Michael Jurkab8f06722010-10-10 15:58:46 -07001605 acquireVelocityTrackerAndAddMovement(ev);
Winson Chung321e9ee2010-08-09 13:37:56 -07001606
1607 final int action = ev.getAction();
1608
1609 switch (action & MotionEvent.ACTION_MASK) {
1610 case MotionEvent.ACTION_DOWN:
1611 /*
1612 * If being flinged and user touches, stop the fling. isFinished
1613 * will be false if being flinged.
1614 */
1615 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001616 abortScrollerAnimation(false);
Winson Chung321e9ee2010-08-09 13:37:56 -07001617 }
1618
1619 // Remember where the motion event started
1620 mDownMotionX = mLastMotionX = ev.getX();
Adam Cohen7d30a372013-07-01 17:03:59 -07001621 mDownMotionY = mLastMotionY = ev.getY();
1622 mDownScrollX = getScrollX();
1623 float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1624 mParentDownMotionX = p[0];
1625 mParentDownMotionY = p[1];
Winson Chungc0844aa2011-02-02 15:25:58 -08001626 mLastMotionXRemainder = 0;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001627 mTotalMotionX = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001628 mActivePointerId = ev.getPointerId(0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001629
Michael Jurka0142d492010-08-25 17:46:15 -07001630 if (mTouchState == TOUCH_STATE_SCROLLING) {
Adam Cohenc2d6e892014-10-16 09:49:24 -07001631 onScrollInteractionBegin();
Michael Jurka0142d492010-08-25 17:46:15 -07001632 pageBeginMoving();
1633 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001634 break;
1635
1636 case MotionEvent.ACTION_MOVE:
1637 if (mTouchState == TOUCH_STATE_SCROLLING) {
1638 // Scroll to follow the motion event
1639 final int pointerIndex = ev.findPointerIndex(mActivePointerId);
Adam Cohen7d30a372013-07-01 17:03:59 -07001640
1641 if (pointerIndex == -1) return true;
1642
Winson Chung321e9ee2010-08-09 13:37:56 -07001643 final float x = ev.getX(pointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001644 final float deltaX = mLastMotionX + mLastMotionXRemainder - x;
Winson Chung321e9ee2010-08-09 13:37:56 -07001645
Adam Cohenaefd4e12011-02-14 16:39:38 -08001646 mTotalMotionX += Math.abs(deltaX);
1647
Winson Chungc0844aa2011-02-02 15:25:58 -08001648 // Only scroll and update mLastMotionX if we have moved some discrete amount. We
1649 // keep the remainder because we are actually testing if we've moved from the last
1650 // scrolled position (which is discrete).
1651 if (Math.abs(deltaX) >= 1.0f) {
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07001652 scrollBy((int) deltaX, 0);
Winson Chungc0844aa2011-02-02 15:25:58 -08001653 mLastMotionX = x;
1654 mLastMotionXRemainder = deltaX - (int) deltaX;
Winson Chung321e9ee2010-08-09 13:37:56 -07001655 } else {
1656 awakenScrollBars();
1657 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001658 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1659 // Update the last motion position
1660 mLastMotionX = ev.getX();
1661 mLastMotionY = ev.getY();
1662
1663 // Update the parent down so that our zoom animations take this new movement into
1664 // account
1665 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1666 mParentDownMotionX = pt[0];
1667 mParentDownMotionY = pt[1];
1668 updateDragViewTranslationDuringDrag();
1669
1670 // Find the closest page to the touch point
1671 final int dragViewIndex = indexOfChild(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001672
Adam Cohen7d30a372013-07-01 17:03:59 -07001673 if (DEBUG) Log.d(TAG, "mLastMotionX: " + mLastMotionX);
1674 if (DEBUG) Log.d(TAG, "mLastMotionY: " + mLastMotionY);
1675 if (DEBUG) Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
1676 if (DEBUG) Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);
1677
Adam Cohenf358a4b2013-07-23 16:47:31 -07001678 final int pageUnderPointIndex = getNearestHoverOverPageIndex();
Sunny Goyal8e2133b2015-05-06 13:39:07 -07001679 if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)) {
Adam Cohen7d30a372013-07-01 17:03:59 -07001680 mTempVisiblePagesRange[0] = 0;
1681 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07001682 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07001683 if (mTempVisiblePagesRange[0] <= pageUnderPointIndex &&
1684 pageUnderPointIndex <= mTempVisiblePagesRange[1] &&
1685 pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
1686 mSidePageHoverIndex = pageUnderPointIndex;
1687 mSidePageHoverRunnable = new Runnable() {
1688 @Override
1689 public void run() {
Adam Cohen7d30a372013-07-01 17:03:59 -07001690 // Setup the scroll to the correct page before we swap the views
1691 snapToPage(pageUnderPointIndex);
1692
1693 // For each of the pages between the paged view and the drag view,
1694 // animate them from the previous position to the new position in
1695 // the layout (as a result of the drag view moving in the layout)
1696 int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
1697 int lowerIndex = (dragViewIndex < pageUnderPointIndex) ?
1698 dragViewIndex + 1 : pageUnderPointIndex;
1699 int upperIndex = (dragViewIndex > pageUnderPointIndex) ?
1700 dragViewIndex - 1 : pageUnderPointIndex;
1701 for (int i = lowerIndex; i <= upperIndex; ++i) {
1702 View v = getChildAt(i);
1703 // dragViewIndex < pageUnderPointIndex, so after we remove the
1704 // drag view all subsequent views to pageUnderPointIndex will
1705 // shift down.
1706 int oldX = getViewportOffsetX() + getChildOffset(i);
1707 int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);
1708
1709 // Animate the view translation from its old position to its new
1710 // position
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001711 ObjectAnimator anim = (ObjectAnimator) v.getTag();
Adam Cohen7d30a372013-07-01 17:03:59 -07001712 if (anim != null) {
1713 anim.cancel();
1714 }
1715
1716 v.setTranslationX(oldX - newX);
Sunny Goyal5d2fc322015-07-06 22:52:49 -07001717 anim = LauncherAnimUtils.ofFloat(v, View.TRANSLATION_X, 0);
Adam Cohen7d30a372013-07-01 17:03:59 -07001718 anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07001719 anim.start();
1720 v.setTag(anim);
1721 }
1722
1723 removeView(mDragView);
Adam Cohen7d30a372013-07-01 17:03:59 -07001724 addView(mDragView, pageUnderPointIndex);
Adam Cohen7d30a372013-07-01 17:03:59 -07001725 mSidePageHoverIndex = -1;
Winson Chung876a6192013-11-06 14:49:50 -08001726 if (mPageIndicator != null) {
1727 mPageIndicator.setActiveMarker(getNextPage());
1728 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001729 }
1730 };
1731 postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
1732 }
1733 } else {
1734 removeCallbacks(mSidePageHoverRunnable);
1735 mSidePageHoverIndex = -1;
1736 }
Adam Cohen564976a2010-10-13 18:52:07 -07001737 } else {
Winson Chung321e9ee2010-08-09 13:37:56 -07001738 determineScrollingStart(ev);
1739 }
1740 break;
1741
1742 case MotionEvent.ACTION_UP:
1743 if (mTouchState == TOUCH_STATE_SCROLLING) {
1744 final int activePointerId = mActivePointerId;
1745 final int pointerIndex = ev.findPointerIndex(activePointerId);
1746 final float x = ev.getX(pointerIndex);
1747 final VelocityTracker velocityTracker = mVelocityTracker;
1748 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
1749 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
Winson Chung9cfd25f2010-10-24 16:09:28 -07001750 final int deltaX = (int) (x - mDownMotionX);
Adam Cohen96d30a12013-07-16 18:13:21 -07001751 final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
Adam Cohen00481b32011-11-18 12:03:48 -08001752 boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
1753 SIGNIFICANT_MOVE_THRESHOLD;
Adam Cohenaefd4e12011-02-14 16:39:38 -08001754
Adam Cohenb64cb5a2011-02-15 13:53:42 -08001755 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
1756
Adam Cohen00481b32011-11-18 12:03:48 -08001757 boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
Adam Cohen265b9a62011-12-07 14:37:18 -08001758 Math.abs(velocityX) > mFlingThresholdVelocity;
Adam Cohen00481b32011-11-18 12:03:48 -08001759
Adam Cohenf358a4b2013-07-23 16:47:31 -07001760 if (!mFreeScroll) {
1761 // In the case that the page is moved far to one direction and then is flung
1762 // in the opposite direction, we use a threshold to determine whether we should
1763 // just return to the starting page, or if we should skip one further.
1764 boolean returnToOriginalPage = false;
1765 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
1766 Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
1767 returnToOriginalPage = true;
1768 }
Adam Cohenaefd4e12011-02-14 16:39:38 -08001769
Adam Cohenf358a4b2013-07-23 16:47:31 -07001770 int finalPage;
1771 // We give flings precedence over large moves, which is why we short-circuit our
1772 // test for a large move if a fling has been registered. That is, a large
1773 // move to the left and fling to the right will register as a fling to the right.
Sunny Goyal70660032015-05-14 00:07:08 -07001774 boolean isDeltaXLeft = mIsRtl ? deltaX > 0 : deltaX < 0;
1775 boolean isVelocityXLeft = mIsRtl ? velocityX > 0 : velocityX < 0;
Adam Cohenf358a4b2013-07-23 16:47:31 -07001776 if (((isSignificantMove && !isDeltaXLeft && !isFling) ||
1777 (isFling && !isVelocityXLeft)) && mCurrentPage > 0) {
1778 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
1779 snapToPageWithVelocity(finalPage, velocityX);
1780 } else if (((isSignificantMove && isDeltaXLeft && !isFling) ||
1781 (isFling && isVelocityXLeft)) &&
1782 mCurrentPage < getChildCount() - 1) {
1783 finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
1784 snapToPageWithVelocity(finalPage, velocityX);
1785 } else {
1786 snapToDestination();
Adam Cohenf358a4b2013-07-23 16:47:31 -07001787 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001788 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001789 if (!mScroller.isFinished()) {
Adam Cohen8bdbaab2013-10-29 15:25:02 -07001790 abortScrollerAnimation(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001791 }
1792
1793 float scaleX = getScaleX();
1794 int vX = (int) (-velocityX * scaleX);
1795 int initialScrollX = (int) (getScrollX() * scaleX);
1796
Adam Cohenf9618852013-11-08 06:45:03 -08001797 mScroller.setInterpolator(mDefaultInterpolator);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001798 mScroller.fling(initialScrollX,
1799 getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0, 0);
1800 invalidate();
Winson Chung321e9ee2010-08-09 13:37:56 -07001801 }
Adam Cohenc2d6e892014-10-16 09:49:24 -07001802 onScrollInteractionEnd();
Adam Cohencae7f572013-11-04 14:42:52 -08001803 } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
1804 // at this point we have not moved beyond the touch slop
1805 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1806 // we can just page
1807 int nextPage = Math.max(0, mCurrentPage - 1);
1808 if (nextPage != mCurrentPage) {
1809 snapToPage(nextPage);
1810 } else {
1811 snapToDestination();
1812 }
Patrick Dubroyd0ce1ec2011-01-19 18:47:27 -08001813 } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
Winson Chung321e9ee2010-08-09 13:37:56 -07001814 // at this point we have not moved beyond the touch slop
1815 // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
1816 // we can just page
Winson Chung86f77532010-08-24 11:08:22 -07001817 int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
1818 if (nextPage != mCurrentPage) {
1819 snapToPage(nextPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07001820 } else {
1821 snapToDestination();
1822 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001823 } else if (mTouchState == TOUCH_STATE_REORDERING) {
1824 // Update the last motion position
1825 mLastMotionX = ev.getX();
1826 mLastMotionY = ev.getY();
1827
1828 // Update the parent down so that our zoom animations take this new movement into
1829 // account
1830 float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
1831 mParentDownMotionX = pt[0];
1832 mParentDownMotionY = pt[1];
1833 updateDragViewTranslationDuringDrag();
Jeff Brown1d0867c2010-12-02 18:27:39 -08001834 } else {
Adam Cohendbdff6b2013-09-18 19:09:15 -07001835 if (!mCancelTap) {
1836 onUnhandledTap(ev);
1837 }
Winson Chung321e9ee2010-08-09 13:37:56 -07001838 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001839
1840 // Remove the callback to wait for the side page hover timeout
1841 removeCallbacks(mSidePageHoverRunnable);
1842 // End any intermediate reordering states
1843 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001844 break;
1845
1846 case MotionEvent.ACTION_CANCEL:
Michael Jurkab8f06722010-10-10 15:58:46 -07001847 if (mTouchState == TOUCH_STATE_SCROLLING) {
1848 snapToDestination();
1849 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001850 resetTouchState();
Winson Chung321e9ee2010-08-09 13:37:56 -07001851 break;
1852
1853 case MotionEvent.ACTION_POINTER_UP:
1854 onSecondaryPointerUp(ev);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001855 releaseVelocityTracker();
Winson Chung321e9ee2010-08-09 13:37:56 -07001856 break;
1857 }
1858
1859 return true;
1860 }
1861
Adam Cohen7d30a372013-07-01 17:03:59 -07001862 private void resetTouchState() {
1863 releaseVelocityTracker();
1864 endReordering();
Adam Cohendbdff6b2013-09-18 19:09:15 -07001865 mCancelTap = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07001866 mTouchState = TOUCH_STATE_REST;
1867 mActivePointerId = INVALID_POINTER;
Sunny Goyal4d113a52015-05-27 10:05:28 -07001868 mEdgeGlowLeft.onRelease();
1869 mEdgeGlowRight.onRelease();
Adam Cohen7d30a372013-07-01 17:03:59 -07001870 }
1871
Adam Cohenc2d6e892014-10-16 09:49:24 -07001872 /**
1873 * Triggered by scrolling via touch
1874 */
1875 protected void onScrollInteractionBegin() {
1876 }
1877
1878 protected void onScrollInteractionEnd() {
1879 }
1880
Adam Cohen1697b792013-09-17 19:08:21 -07001881 protected void onUnhandledTap(MotionEvent ev) {
1882 ((Launcher) getContext()).onClick(this);
1883 }
Adam Cohen7d30a372013-07-01 17:03:59 -07001884
Winson Chung185d7162011-02-28 13:47:29 -08001885 @Override
1886 public boolean onGenericMotionEvent(MotionEvent event) {
1887 if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) {
1888 switch (event.getAction()) {
1889 case MotionEvent.ACTION_SCROLL: {
1890 // Handle mouse (or ext. device) by shifting the page depending on the scroll
1891 final float vscroll;
1892 final float hscroll;
1893 if ((event.getMetaState() & KeyEvent.META_SHIFT_ON) != 0) {
1894 vscroll = 0;
1895 hscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1896 } else {
1897 vscroll = -event.getAxisValue(MotionEvent.AXIS_VSCROLL);
1898 hscroll = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
1899 }
1900 if (hscroll != 0 || vscroll != 0) {
Sunny Goyal70660032015-05-14 00:07:08 -07001901 boolean isForwardScroll = mIsRtl ? (hscroll < 0 || vscroll < 0)
Adam Cohen0ffac432013-07-10 11:19:26 -07001902 : (hscroll > 0 || vscroll > 0);
1903 if (isForwardScroll) {
Winson Chung185d7162011-02-28 13:47:29 -08001904 scrollRight();
1905 } else {
1906 scrollLeft();
1907 }
1908 return true;
1909 }
1910 }
1911 }
1912 }
1913 return super.onGenericMotionEvent(event);
1914 }
1915
Michael Jurkab8f06722010-10-10 15:58:46 -07001916 private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
1917 if (mVelocityTracker == null) {
1918 mVelocityTracker = VelocityTracker.obtain();
1919 }
1920 mVelocityTracker.addMovement(ev);
1921 }
1922
1923 private void releaseVelocityTracker() {
1924 if (mVelocityTracker != null) {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001925 mVelocityTracker.clear();
Michael Jurkab8f06722010-10-10 15:58:46 -07001926 mVelocityTracker.recycle();
1927 mVelocityTracker = null;
1928 }
1929 }
1930
Winson Chung321e9ee2010-08-09 13:37:56 -07001931 private void onSecondaryPointerUp(MotionEvent ev) {
1932 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>
1933 MotionEvent.ACTION_POINTER_INDEX_SHIFT;
1934 final int pointerId = ev.getPointerId(pointerIndex);
1935 if (pointerId == mActivePointerId) {
1936 // This was our active pointer going up. Choose a new
1937 // active pointer and adjust accordingly.
1938 // TODO: Make this decision more intelligent.
1939 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
1940 mLastMotionX = mDownMotionX = ev.getX(newPointerIndex);
1941 mLastMotionY = ev.getY(newPointerIndex);
Winson Chungc0844aa2011-02-02 15:25:58 -08001942 mLastMotionXRemainder = 0;
Winson Chung321e9ee2010-08-09 13:37:56 -07001943 mActivePointerId = ev.getPointerId(newPointerIndex);
1944 if (mVelocityTracker != null) {
1945 mVelocityTracker.clear();
1946 }
1947 }
Jeff Brown1d0867c2010-12-02 18:27:39 -08001948 }
1949
Winson Chung321e9ee2010-08-09 13:37:56 -07001950 @Override
1951 public void requestChildFocus(View child, View focused) {
1952 super.requestChildFocus(child, focused);
Adam Cohenae4f1552011-10-20 00:15:42 -07001953 int page = indexToPage(indexOfChild(child));
Winson Chung97d85d22011-04-13 11:27:36 -07001954 if (page >= 0 && page != getCurrentPage() && !isInTouchMode()) {
Winson Chung86f77532010-08-24 11:08:22 -07001955 snapToPage(page);
Winson Chung321e9ee2010-08-09 13:37:56 -07001956 }
1957 }
1958
Adam Cohend19d3ca2010-09-15 14:43:42 -07001959 int getPageNearestToCenterOfScreen() {
Adam Cohen22f823d2011-09-01 17:22:18 -07001960 int minDistanceFromScreenCenter = Integer.MAX_VALUE;
Winson Chung321e9ee2010-08-09 13:37:56 -07001961 int minDistanceFromScreenCenterIndex = -1;
Adam Cohen7d30a372013-07-01 17:03:59 -07001962 int screenCenter = getViewportOffsetX() + getScrollX() + (getViewportWidth() / 2);
Winson Chung321e9ee2010-08-09 13:37:56 -07001963 final int childCount = getChildCount();
1964 for (int i = 0; i < childCount; ++i) {
Adam Cohen22f823d2011-09-01 17:22:18 -07001965 View layout = (View) getPageAt(i);
Adam Cohen96d30a12013-07-16 18:13:21 -07001966 int childWidth = layout.getMeasuredWidth();
Winson Chung321e9ee2010-08-09 13:37:56 -07001967 int halfChildWidth = (childWidth / 2);
Adam Cohen7d30a372013-07-01 17:03:59 -07001968 int childCenter = getViewportOffsetX() + getChildOffset(i) + halfChildWidth;
Winson Chung321e9ee2010-08-09 13:37:56 -07001969 int distanceFromScreenCenter = Math.abs(childCenter - screenCenter);
1970 if (distanceFromScreenCenter < minDistanceFromScreenCenter) {
1971 minDistanceFromScreenCenter = distanceFromScreenCenter;
1972 minDistanceFromScreenCenterIndex = i;
1973 }
1974 }
Adam Cohend19d3ca2010-09-15 14:43:42 -07001975 return minDistanceFromScreenCenterIndex;
1976 }
1977
1978 protected void snapToDestination() {
Sunny Goyal4d113a52015-05-27 10:05:28 -07001979 snapToPage(getPageNearestToCenterOfScreen(), PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07001980 }
1981
Adam Cohene0f66b52010-11-23 15:06:07 -08001982 private static class ScrollInterpolator implements Interpolator {
1983 public ScrollInterpolator() {
1984 }
1985
1986 public float getInterpolation(float t) {
1987 t -= 1.0f;
1988 return t*t*t*t*t + 1;
1989 }
1990 }
1991
1992 // We want the duration of the page snap animation to be influenced by the distance that
1993 // the screen has to travel, however, we don't want this duration to be effected in a
1994 // purely linear fashion. Instead, we use this method to moderate the effect that the distance
1995 // of travel has on the overall snap duration.
Sunny Goyal4d113a52015-05-27 10:05:28 -07001996 private float distanceInfluenceForSnapDuration(float f) {
Adam Cohene0f66b52010-11-23 15:06:07 -08001997 f -= 0.5f; // center the values about 0.
1998 f *= 0.3f * Math.PI / 2.0f;
1999 return (float) Math.sin(f);
2000 }
2001
Michael Jurka0142d492010-08-25 17:46:15 -07002002 protected void snapToPageWithVelocity(int whichPage, int velocity) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002003 whichPage = validateNewPage(whichPage);
Adam Cohen7d30a372013-07-01 17:03:59 -07002004 int halfScreenSize = getViewportWidth() / 2;
Adam Cohene0f66b52010-11-23 15:06:07 -08002005
Adam Cohenedb40762013-07-18 16:45:45 -07002006 final int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002007 int delta = newX - getUnboundedScrollX();
Adam Cohene0f66b52010-11-23 15:06:07 -08002008 int duration = 0;
2009
Sunny Goyal4d113a52015-05-27 10:05:28 -07002010 if (Math.abs(velocity) < mMinFlingVelocity) {
Adam Cohene0f66b52010-11-23 15:06:07 -08002011 // If the velocity is low enough, then treat this more as an automatic page advance
2012 // as opposed to an apparent physical response to flinging
Sunny Goyal4d113a52015-05-27 10:05:28 -07002013 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Adam Cohene0f66b52010-11-23 15:06:07 -08002014 return;
2015 }
2016
2017 // Here we compute a "distance" that will be used in the computation of the overall
2018 // snap duration. This is a function of the actual distance that needs to be traveled;
2019 // we keep this value close to half screen size in order to reduce the variance in snap
2020 // duration as a function of the distance the page needs to travel.
Michael Jurka20b7ca92011-06-07 20:09:16 -07002021 float distanceRatio = Math.min(1f, 1.0f * Math.abs(delta) / (2 * halfScreenSize));
Adam Cohene0f66b52010-11-23 15:06:07 -08002022 float distance = halfScreenSize + halfScreenSize *
2023 distanceInfluenceForSnapDuration(distanceRatio);
2024
2025 velocity = Math.abs(velocity);
Adam Cohen265b9a62011-12-07 14:37:18 -08002026 velocity = Math.max(mMinSnapVelocity, velocity);
Adam Cohene0f66b52010-11-23 15:06:07 -08002027
2028 // we want the page's snap velocity to approximately match the velocity at which the
2029 // user flings, so we scale the duration by a value near to the derivative of the scroll
Michael Jurka20b7ca92011-06-07 20:09:16 -07002030 // interpolator at zero, ie. 5. We use 4 to make it a little slower.
2031 duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
Adam Cohene0f66b52010-11-23 15:06:07 -08002032
2033 snapToPage(whichPage, delta, duration);
Michael Jurka0142d492010-08-25 17:46:15 -07002034 }
2035
Sunny Goyal1740d902015-05-27 11:14:01 -07002036 public void snapToPage(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002037 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION);
Winson Chung321e9ee2010-08-09 13:37:56 -07002038 }
2039
Adam Cohenf9c184a2016-01-15 16:47:43 -08002040 public void snapToPageImmediately(int whichPage) {
Sunny Goyal4d113a52015-05-27 10:05:28 -07002041 snapToPage(whichPage, PAGE_SNAP_ANIMATION_DURATION, true, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002042 }
2043
Michael Jurka0142d492010-08-25 17:46:15 -07002044 protected void snapToPage(int whichPage, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002045 snapToPage(whichPage, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002046 }
2047
Adam Cohenf9618852013-11-08 06:45:03 -08002048 protected void snapToPage(int whichPage, int duration, TimeInterpolator interpolator) {
2049 snapToPage(whichPage, duration, false, interpolator);
2050 }
2051
2052 protected void snapToPage(int whichPage, int duration, boolean immediate,
2053 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002054 whichPage = validateNewPage(whichPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002055
Adam Cohenedb40762013-07-18 16:45:45 -07002056 int newX = getScrollForPage(whichPage);
Sunny Goyalc86df472016-02-25 09:19:38 -08002057 final int delta = newX - getUnboundedScrollX();
Adam Cohenf9618852013-11-08 06:45:03 -08002058 snapToPage(whichPage, delta, duration, immediate, interpolator);
Michael Jurka0142d492010-08-25 17:46:15 -07002059 }
2060
2061 protected void snapToPage(int whichPage, int delta, int duration) {
Adam Cohenf9618852013-11-08 06:45:03 -08002062 snapToPage(whichPage, delta, duration, false, null);
Adam Cohen7d30a372013-07-01 17:03:59 -07002063 }
Michael Jurka0142d492010-08-25 17:46:15 -07002064
Adam Cohenf9618852013-11-08 06:45:03 -08002065 protected void snapToPage(int whichPage, int delta, int duration, boolean immediate,
2066 TimeInterpolator interpolator) {
Adam Cohen6f127a62014-06-12 14:54:41 -07002067 whichPage = validateNewPage(whichPage);
2068
Adam Cohen7d30a372013-07-01 17:03:59 -07002069 mNextPage = whichPage;
Michael Jurka0142d492010-08-25 17:46:15 -07002070
2071 pageBeginMoving();
Winson Chung321e9ee2010-08-09 13:37:56 -07002072 awakenScrollBars(duration);
Adam Cohen7d30a372013-07-01 17:03:59 -07002073 if (immediate) {
2074 duration = 0;
2075 } else if (duration == 0) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002076 duration = Math.abs(delta);
2077 }
2078
Adam Cohenf358a4b2013-07-23 16:47:31 -07002079 if (!mScroller.isFinished()) {
Adam Cohenf9618852013-11-08 06:45:03 -08002080 abortScrollerAnimation(false);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002081 }
Adam Cohenf9618852013-11-08 06:45:03 -08002082
2083 if (interpolator != null) {
2084 mScroller.setInterpolator(interpolator);
2085 } else {
2086 mScroller.setInterpolator(mDefaultInterpolator);
2087 }
2088
Sunny Goyalc86df472016-02-25 09:19:38 -08002089 mScroller.startScroll(getUnboundedScrollX(), 0, delta, 0, duration);
Winson Chung80baf5a2010-08-09 16:03:15 -07002090
Adam Cohen674531f2013-12-13 15:07:14 -08002091 updatePageIndicator();
Adam Cohen7d30a372013-07-01 17:03:59 -07002092
2093 // Trigger a compute() to finish switching pages if necessary
2094 if (immediate) {
2095 computeScroll();
2096 }
2097
2098 mForceScreenScrolled = true;
Winson Chung321e9ee2010-08-09 13:37:56 -07002099 invalidate();
2100 }
2101
Winson Chung321e9ee2010-08-09 13:37:56 -07002102 public void scrollLeft() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002103 if (getNextPage() > 0) snapToPage(getNextPage() - 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002104 }
2105
2106 public void scrollRight() {
Adam Cohen2bf63d52013-09-29 17:46:49 -07002107 if (getNextPage() < getChildCount() -1) snapToPage(getNextPage() + 1);
Winson Chung321e9ee2010-08-09 13:37:56 -07002108 }
2109
Adam Cohendbdff6b2013-09-18 19:09:15 -07002110 @Override
2111 public boolean performLongClick() {
2112 mCancelTap = true;
2113 return super.performLongClick();
2114 }
2115
Winson Chung321e9ee2010-08-09 13:37:56 -07002116 public static class SavedState extends BaseSavedState {
Winson Chung86f77532010-08-24 11:08:22 -07002117 int currentPage = -1;
Winson Chung321e9ee2010-08-09 13:37:56 -07002118
2119 SavedState(Parcelable superState) {
2120 super(superState);
2121 }
2122
Adam Cohen091440a2015-03-18 14:16:05 -07002123 @Thunk SavedState(Parcel in) {
Winson Chung321e9ee2010-08-09 13:37:56 -07002124 super(in);
Winson Chung86f77532010-08-24 11:08:22 -07002125 currentPage = in.readInt();
Winson Chung321e9ee2010-08-09 13:37:56 -07002126 }
2127
2128 @Override
2129 public void writeToParcel(Parcel out, int flags) {
2130 super.writeToParcel(out, flags);
Winson Chung86f77532010-08-24 11:08:22 -07002131 out.writeInt(currentPage);
Winson Chung321e9ee2010-08-09 13:37:56 -07002132 }
2133
2134 public static final Parcelable.Creator<SavedState> CREATOR =
2135 new Parcelable.Creator<SavedState>() {
2136 public SavedState createFromParcel(Parcel in) {
2137 return new SavedState(in);
2138 }
2139
2140 public SavedState[] newArray(int size) {
2141 return new SavedState[size];
2142 }
2143 };
2144 }
2145
Adam Cohen7d30a372013-07-01 17:03:59 -07002146 // Animate the drag view back to the original position
Sunny Goyal4d113a52015-05-27 10:05:28 -07002147 private void animateDragViewToOriginalPosition() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002148 if (mDragView != null) {
Sunny Goyal5d2fc322015-07-06 22:52:49 -07002149 Animator anim = new LauncherViewPropertyAnimator(mDragView)
2150 .translationX(0)
2151 .translationY(0)
2152 .scaleX(1)
2153 .scaleY(1)
2154 .setDuration(REORDERING_DROP_REPOSITION_DURATION);
Adam Cohen7d30a372013-07-01 17:03:59 -07002155 anim.addListener(new AnimatorListenerAdapter() {
2156 @Override
2157 public void onAnimationEnd(Animator animation) {
2158 onPostReorderingAnimationCompleted();
2159 }
2160 });
2161 anim.start();
2162 }
Winson Chung3ac74c52011-06-30 17:39:37 -07002163 }
2164
Sunny Goyal1d08f702015-05-04 15:50:25 -07002165 public void onStartReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002166 // Set the touch state to reordering (allows snapping to pages, dragging a child, etc.)
2167 mTouchState = TOUCH_STATE_REORDERING;
2168 mIsReordering = true;
2169
Adam Cohen7d30a372013-07-01 17:03:59 -07002170 // We must invalidate to trigger a redraw to update the layers such that the drag view
2171 // is always drawn on top
2172 invalidate();
2173 }
2174
Adam Cohen091440a2015-03-18 14:16:05 -07002175 @Thunk void onPostReorderingAnimationCompleted() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002176 // Trigger the callback when reordering has settled
2177 --mPostReorderingPreZoomInRemainingAnimationCount;
2178 if (mPostReorderingPreZoomInRunnable != null &&
2179 mPostReorderingPreZoomInRemainingAnimationCount == 0) {
2180 mPostReorderingPreZoomInRunnable.run();
2181 mPostReorderingPreZoomInRunnable = null;
2182 }
2183 }
2184
Sunny Goyal1d08f702015-05-04 15:50:25 -07002185 public void onEndReordering() {
Adam Cohen7d30a372013-07-01 17:03:59 -07002186 mIsReordering = false;
Adam Cohen7d30a372013-07-01 17:03:59 -07002187 }
2188
Adam Cohenf358a4b2013-07-23 16:47:31 -07002189 public boolean startReordering(View v) {
Adam Cohen93c97562013-09-26 13:48:01 -07002190 int dragViewIndex = indexOfChild(v);
2191
Adam Cohen327acfe2014-06-06 11:52:52 -07002192 if (mTouchState != TOUCH_STATE_REST || dragViewIndex == -1) return false;
Adam Cohen93c97562013-09-26 13:48:01 -07002193
Adam Cohen7d30a372013-07-01 17:03:59 -07002194 mTempVisiblePagesRange[0] = 0;
2195 mTempVisiblePagesRange[1] = getPageCount() - 1;
Adam Cohen6f127a62014-06-12 14:54:41 -07002196 getFreeScrollPageRange(mTempVisiblePagesRange);
Adam Cohen7d30a372013-07-01 17:03:59 -07002197 mReorderingStarted = true;
2198
2199 // Check if we are within the reordering range
2200 if (mTempVisiblePagesRange[0] <= dragViewIndex &&
Adam Cohenf358a4b2013-07-23 16:47:31 -07002201 dragViewIndex <= mTempVisiblePagesRange[1]) {
2202 // Find the drag view under the pointer
2203 mDragView = getChildAt(dragViewIndex);
2204 mDragView.animate().scaleX(1.15f).scaleY(1.15f).setDuration(100).start();
2205 mDragViewBaselineLeft = mDragView.getLeft();
Adam Cohenf9618852013-11-08 06:45:03 -08002206 snapToPage(getPageNearestToCenterOfScreen());
2207 disableFreeScroll();
Adam Cohenf358a4b2013-07-23 16:47:31 -07002208 onStartReordering();
Adam Cohen7d30a372013-07-01 17:03:59 -07002209 return true;
2210 }
2211 return false;
2212 }
2213
2214 boolean isReordering(boolean testTouchState) {
2215 boolean state = mIsReordering;
2216 if (testTouchState) {
2217 state &= (mTouchState == TOUCH_STATE_REORDERING);
2218 }
2219 return state;
2220 }
2221 void endReordering() {
2222 // For simplicity, we call endReordering sometimes even if reordering was never started.
2223 // In that case, we don't want to do anything.
2224 if (!mReorderingStarted) return;
2225 mReorderingStarted = false;
2226
2227 // If we haven't flung-to-delete the current child, then we just animate the drag view
2228 // back into position
2229 final Runnable onCompleteRunnable = new Runnable() {
2230 @Override
2231 public void run() {
2232 onEndReordering();
2233 }
2234 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002235
2236 mPostReorderingPreZoomInRunnable = new Runnable() {
2237 public void run() {
2238 onCompleteRunnable.run();
2239 enableFreeScroll();
Adam Cohen7d30a372013-07-01 17:03:59 -07002240 };
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002241 };
Adam Cohen7d30a372013-07-01 17:03:59 -07002242
Sunny Goyal8e2133b2015-05-06 13:39:07 -07002243 mPostReorderingPreZoomInRemainingAnimationCount =
2244 NUM_ANIMATIONS_RUNNING_BEFORE_ZOOM_OUT;
2245 // Snap to the current page
2246 snapToPage(indexOfChild(mDragView), 0);
2247 // Animate the drag view back to the front position
2248 animateDragViewToOriginalPosition();
Adam Cohen7d30a372013-07-01 17:03:59 -07002249 }
2250
Winson Chung6a0f57d2011-06-29 20:10:49 -07002251 /* Accessibility */
Sunny Goyalcf25b522015-07-09 00:01:18 -07002252 @SuppressWarnings("deprecation")
Sunny Goyal316490e2015-06-02 09:38:28 -07002253 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chung6a0f57d2011-06-29 20:10:49 -07002254 @Override
2255 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
2256 super.onInitializeAccessibilityNodeInfo(info);
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002257 info.setScrollable(getPageCount() > 1);
2258 if (getCurrentPage() < getPageCount() - 1) {
2259 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
2260 }
2261 if (getCurrentPage() > 0) {
2262 info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
2263 }
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002264 info.setClassName(getClass().getName());
2265
2266 // Accessibility-wise, PagedView doesn't support long click, so disabling it.
2267 // Besides disabling the accessibility long-click, this also prevents this view from getting
2268 // accessibility focus.
2269 info.setLongClickable(false);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002270 if (Utilities.ATLEAST_LOLLIPOP) {
Vadim Tryshev7af0d442015-05-15 08:48:11 -07002271 info.removeAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_LONG_CLICK);
2272 }
Winson Chung6a0f57d2011-06-29 20:10:49 -07002273 }
2274
2275 @Override
Alan Viverette254139a2013-10-08 13:13:46 -07002276 public void sendAccessibilityEvent(int eventType) {
2277 // Don't let the view send real scroll events.
2278 if (eventType != AccessibilityEvent.TYPE_VIEW_SCROLLED) {
2279 super.sendAccessibilityEvent(eventType);
2280 }
2281 }
2282
2283 @Override
Winson Chung6a0f57d2011-06-29 20:10:49 -07002284 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
2285 super.onInitializeAccessibilityEvent(event);
Vadim Tryshev7066c122015-05-21 14:06:35 -07002286 event.setScrollable(getPageCount() > 1);
Winson Chung6a0f57d2011-06-29 20:10:49 -07002287 }
2288
Svetoslav Ganov08055f62012-05-15 11:06:36 -07002289 @Override
2290 public boolean performAccessibilityAction(int action, Bundle arguments) {
2291 if (super.performAccessibilityAction(action, arguments)) {
2292 return true;
2293 }
2294 switch (action) {
2295 case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: {
2296 if (getCurrentPage() < getPageCount() - 1) {
2297 scrollRight();
2298 return true;
2299 }
2300 } break;
2301 case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
2302 if (getCurrentPage() > 0) {
2303 scrollLeft();
2304 return true;
2305 }
2306 } break;
2307 }
2308 return false;
2309 }
2310
Adam Cohen0ffac432013-07-10 11:19:26 -07002311 protected String getCurrentPageDescription() {
Sunny Goyalf4f89ef2015-09-02 15:06:12 -07002312 return getContext().getString(R.string.default_scroll_format,
Adam Cohen0ffac432013-07-10 11:19:26 -07002313 getNextPage() + 1, getChildCount());
2314 }
2315
Winson Chungd11265e2011-08-30 13:37:23 -07002316 @Override
2317 public boolean onHoverEvent(android.view.MotionEvent event) {
2318 return true;
2319 }
Adam Cohen5084cba2013-09-03 12:01:16 -07002320}