OverScroller.java revision 34961cc2e5cd483b15dfd1ff1eb33b01b849e0dc
1637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell/*
2637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Copyright (C) 2010 The Android Open Source Project
3637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell *
4637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Licensed under the Apache License, Version 2.0 (the "License");
5637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * you may not use this file except in compliance with the License.
6637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * You may obtain a copy of the License at
7637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell *
8637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell *      http://www.apache.org/licenses/LICENSE-2.0
9637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell *
10637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * Unless required by applicable law or agreed to in writing, software
11637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * distributed under the License is distributed on an "AS IS" BASIS,
12637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * See the License for the specific language governing permissions and
14637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * limitations under the License.
15637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */
16637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
17637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellpackage android.widget;
18637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
19637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellimport android.content.Context;
20fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.hardware.SensorManager;
21fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.util.FloatMath;
22fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.util.Log;
23637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellimport android.view.ViewConfiguration;
24637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellimport android.view.animation.AnimationUtils;
25fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunneimport android.view.animation.Interpolator;
26637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
27637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell/**
28637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * This class encapsulates scrolling with the ability to overshoot the bounds
29637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * of a scrolling operation. This class is a drop-in replacement for
30637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell * {@link android.widget.Scroller} in most cases.
31637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell */
32637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powellpublic class OverScroller {
33fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    private int mMode;
34637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
35fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    private final SplineOverScroller mScrollerX;
36fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    private final SplineOverScroller mScrollerY;
37637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
38fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    private final Interpolator mInterpolator;
39637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
40fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    private final boolean mFlywheel;
41637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
42637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    private static final int DEFAULT_DURATION = 250;
43637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    private static final int SCROLL_MODE = 0;
44637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    private static final int FLING_MODE = 1;
45637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
46fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    /**
47c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * Creates an OverScroller with a viscous fluid scroll interpolator and flywheel.
48fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param context
49fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     */
50fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    public OverScroller(Context context) {
51fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        this(context, null);
52fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    }
53637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
54fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    /**
55c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * Creates an OverScroller with flywheel enabled.
56fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param context The context of this application.
57fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will
58fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * be used.
59fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     */
60fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    public OverScroller(Context context, Interpolator interpolator) {
61c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        this(context, interpolator, true);
62c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne    }
63c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne
64c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne    /**
65c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * Creates an OverScroller.
66c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * @param context The context of this application.
67c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will
68c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * be used.
69c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * @param flywheel If true, successive fling motions will keep on increasing scroll speed.
70086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville     * @hide
71c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     */
72c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne    public OverScroller(Context context, Interpolator interpolator, boolean flywheel) {
73c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        mInterpolator = interpolator;
74c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        mFlywheel = flywheel;
75c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        mScrollerX = new SplineOverScroller();
76c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        mScrollerY = new SplineOverScroller();
77c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne
78c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        SplineOverScroller.initFromContext(context);
79637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
80637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
81fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    /**
82fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * Creates an OverScroller with flywheel enabled.
83fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param context The context of this application.
84fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will
85fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * be used.
86fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the
87fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * velocity which is preserved in the bounce when the horizontal edge is reached. A null value
88c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * means no bounce. This behavior is no longer supported and this coefficient has no effect.
89c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This
90c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * behavior is no longer supported and this coefficient has no effect.
91086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville     * !deprecated Use {!link #OverScroller(Context, Interpolator, boolean)} instead.
92fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     */
93fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    public OverScroller(Context context, Interpolator interpolator,
94fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            float bounceCoefficientX, float bounceCoefficientY) {
95c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        this(context, interpolator, true);
96637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
97637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
98637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
99637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Creates an OverScroller.
100637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param context The context of this application.
101637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param interpolator The scroll interpolator. If null, a default (viscous) interpolator will
102637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * be used.
103637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param bounceCoefficientX A value between 0 and 1 that will determine the proportion of the
104637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * velocity which is preserved in the bounce when the horizontal edge is reached. A null value
105c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * means no bounce. This behavior is no longer supported and this coefficient has no effect.
106c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * @param bounceCoefficientY Same as bounceCoefficientX but for the vertical direction. This
107c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * behavior is no longer supported and this coefficient has no effect.
108fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne     * @param flywheel If true, successive fling motions will keep on increasing scroll speed.
109086c0611d3680fbc2951f2b469bf78257ef4aed7Wink Saville     * !deprecated Use {!link OverScroller(Context, Interpolator, boolean)} instead.
110637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
111637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public OverScroller(Context context, Interpolator interpolator,
112637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            float bounceCoefficientX, float bounceCoefficientY, boolean flywheel) {
113c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        this(context, interpolator, flywheel);
114637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
115637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
116637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
117637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * The amount of friction applied to flings. The default value
118637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * is {@link ViewConfiguration#getScrollFriction}.
119637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
120637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param friction A scalar dimension-less value representing the coefficient of
121637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *         friction.
122637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
123637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final void setFriction(float friction) {
124fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        mScrollerX.setFriction(friction);
125fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        mScrollerY.setFriction(friction);
126637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
127637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
128637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
129637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
130637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns whether the scroller has finished scrolling.
131637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
132637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return True if the scroller has finished scrolling, false otherwise.
133637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
134637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final boolean isFinished() {
135637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerX.mFinished && mScrollerY.mFinished;
136637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
137637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
138637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
139637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Force the finished field to a particular value. Contrary to
140637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * {@link #abortAnimation()}, forcing the animation to finished
141637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * does NOT cause the scroller to move to the final x and y
142637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * position.
143637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
144637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param finished The new finished value.
145637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
146637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final void forceFinished(boolean finished) {
147637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.mFinished = mScrollerY.mFinished = finished;
148637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
149637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
150637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
151637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns the current X offset in the scroll.
152637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
153637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The new X offset as an absolute distance from the origin.
154637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
155637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getCurrX() {
156637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerX.mCurrentPosition;
157637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
158637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
159637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
160637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns the current Y offset in the scroll.
161637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
162637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The new Y offset as an absolute distance from the origin.
163637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
164637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getCurrY() {
165637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerY.mCurrentPosition;
166637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
167637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
168637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
169637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide
170c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne     * Returns the absolute value of the current velocity.
171637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
172637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The original velocity less the deceleration, norm of the X and Y velocity vector.
173637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
174637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public float getCurrVelocity() {
175637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        float squaredNorm = mScrollerX.mCurrVelocity * mScrollerX.mCurrVelocity;
176637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        squaredNorm += mScrollerY.mCurrVelocity * mScrollerY.mCurrVelocity;
177fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        return FloatMath.sqrt(squaredNorm);
178637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
179637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
180637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
181637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns the start X offset in the scroll.
182637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
183637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The start X offset as an absolute distance from the origin.
184637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
185637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getStartX() {
186637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerX.mStart;
187637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
188637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
189637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
190637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns the start Y offset in the scroll.
191637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
192637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The start Y offset as an absolute distance from the origin.
193637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
194637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getStartY() {
195637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerY.mStart;
196637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
197637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
198637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
199637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns where the scroll will end. Valid only for "fling" scrolls.
200637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
201637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The final X offset as an absolute distance from the origin.
202637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
203637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getFinalX() {
204637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerX.mFinal;
205637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
206637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
207637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
208637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns where the scroll will end. Valid only for "fling" scrolls.
209637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
210637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The final Y offset as an absolute distance from the origin.
211637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
212637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getFinalY() {
213637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return mScrollerY.mFinal;
214637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
215637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
216637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
217637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns how long the scroll event will take, in milliseconds.
218637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
219637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The duration of the scroll in milliseconds.
220637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
221637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide Pending removal once nothing depends on it
222637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @deprecated OverScrollers don't necessarily have a fixed duration.
223637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             This function will lie to the best of its ability.
224637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
225637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    @Deprecated
226637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public final int getDuration() {
227637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return Math.max(mScrollerX.mDuration, mScrollerY.mDuration);
228637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
229637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
230637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
231637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Extend the scroll animation. This allows a running animation to scroll
232637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * further and longer, when used with {@link #setFinalX(int)} or {@link #setFinalY(int)}.
233637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
234637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param extend Additional time to scroll in milliseconds.
235637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #setFinalX(int)
236637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #setFinalY(int)
237637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
238637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide Pending removal once nothing depends on it
239637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @deprecated OverScrollers don't necessarily have a fixed duration.
240637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             Instead of setting a new final position and extending
241637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             the duration of an existing scroll, use startScroll
242637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             to begin a new animation.
243637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
244637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    @Deprecated
245637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void extendDuration(int extend) {
246637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.extendDuration(extend);
247637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerY.extendDuration(extend);
248637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
249637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
250637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
251637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Sets the final position (X) for this scroller.
252637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
253637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param newX The new X offset as an absolute distance from the origin.
254637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #extendDuration(int)
255637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #setFinalY(int)
256637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
257637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide Pending removal once nothing depends on it
258637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @deprecated OverScroller's final position may change during an animation.
259637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             Instead of setting a new final position and extending
260637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             the duration of an existing scroll, use startScroll
261637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             to begin a new animation.
262637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
263637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    @Deprecated
264637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void setFinalX(int newX) {
265637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.setFinalPosition(newX);
266637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
267637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
268637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
269637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Sets the final position (Y) for this scroller.
270637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
271637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param newY The new Y offset as an absolute distance from the origin.
272637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #extendDuration(int)
273637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #setFinalX(int)
274637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
275637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide Pending removal once nothing depends on it
276637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @deprecated OverScroller's final position may change during an animation.
277637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             Instead of setting a new final position and extending
278637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             the duration of an existing scroll, use startScroll
279637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *             to begin a new animation.
280637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
281637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    @Deprecated
282637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void setFinalY(int newY) {
283637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerY.setFinalPosition(newY);
284637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
285637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
286637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
287637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Call this when you want to know the new location. If it returns true, the
288637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * animation is not yet finished.
289637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
290637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public boolean computeScrollOffset() {
291637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        if (isFinished()) {
292637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            return false;
293637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
294637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
295637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        switch (mMode) {
296637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            case SCROLL_MODE:
297637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                long time = AnimationUtils.currentAnimationTimeMillis();
298637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                // Any scroller can be used for time, since they were started
299637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                // together in scroll mode. We use X here.
300637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final long elapsedTime = time - mScrollerX.mStartTime;
301637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
302637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final int duration = mScrollerX.mDuration;
303637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                if (elapsedTime < duration) {
304637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    float q = (float) (elapsedTime) / duration;
305637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
306fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    if (mInterpolator == null) {
307fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                        q = Scroller.viscousFluid(q);
308fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    } else {
309fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                        q = mInterpolator.getInterpolation(q);
310fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    }
311637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
312637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    mScrollerX.updateScroll(q);
313637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    mScrollerY.updateScroll(q);
314637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                } else {
315637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    abortAnimation();
316637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
317637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                break;
318637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
319637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            case FLING_MODE:
320637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                if (!mScrollerX.mFinished) {
321637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    if (!mScrollerX.update()) {
322637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        if (!mScrollerX.continueWhenFinished()) {
323637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                            mScrollerX.finish();
324637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        }
325637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    }
326637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
327637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
328637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                if (!mScrollerY.mFinished) {
329637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    if (!mScrollerY.update()) {
330637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        if (!mScrollerY.continueWhenFinished()) {
331637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                            mScrollerY.finish();
332637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        }
333637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    }
334637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
335637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
336637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                break;
337637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
338637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
339637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return true;
340637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
341637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
342637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
343637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Start scrolling by providing a starting point and the distance to travel.
344637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * The scroll will use the default value of 250 milliseconds for the
345637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * duration.
346637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
347637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startX Starting horizontal scroll offset in pixels. Positive
348637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        numbers will scroll the content to the left.
349637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startY Starting vertical scroll offset in pixels. Positive numbers
350637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        will scroll the content up.
351637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param dx Horizontal distance to travel. Positive numbers will scroll the
352637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        content to the left.
353637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param dy Vertical distance to travel. Positive numbers will scroll the
354637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        content up.
355637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
356637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void startScroll(int startX, int startY, int dx, int dy) {
357637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        startScroll(startX, startY, dx, dy, DEFAULT_DURATION);
358637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
359637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
360637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
361637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Start scrolling by providing a starting point and the distance to travel.
362637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
363637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startX Starting horizontal scroll offset in pixels. Positive
364637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        numbers will scroll the content to the left.
365637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startY Starting vertical scroll offset in pixels. Positive numbers
366637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        will scroll the content up.
367637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param dx Horizontal distance to travel. Positive numbers will scroll the
368637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        content to the left.
369637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param dy Vertical distance to travel. Positive numbers will scroll the
370637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *        content up.
371637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param duration Duration of the scroll in milliseconds.
372637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
373637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
374637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mMode = SCROLL_MODE;
375637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.startScroll(startX, dx, duration);
376637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerY.startScroll(startY, dy, duration);
377637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
378637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
379637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
380637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Call this when you want to 'spring back' into a valid coordinate range.
381637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
382637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startX Starting X coordinate
383637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startY Starting Y coordinate
384637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param minX Minimum valid X value
385637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param maxX Maximum valid X value
386637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param minY Minimum valid Y value
387637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param maxY Minimum valid Y value
388637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return true if a springback was initiated, false if startX and startY were
389637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *          already within the valid range.
390637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
391637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public boolean springBack(int startX, int startY, int minX, int maxX, int minY, int maxY) {
392637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mMode = FLING_MODE;
393637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
394637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Make sure both methods are called.
395637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        final boolean spingbackX = mScrollerX.springback(startX, minX, maxX);
396637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        final boolean spingbackY = mScrollerY.springback(startY, minY, maxY);
397637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return spingbackX || spingbackY;
398637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
399637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
400637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void fling(int startX, int startY, int velocityX, int velocityY,
401637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            int minX, int maxX, int minY, int maxY) {
402637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0);
403637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
404637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
405637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
406637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Start scrolling based on a fling gesture. The distance traveled will
407637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * depend on the initial velocity of the fling.
408637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
409637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startX Starting point of the scroll (X)
410637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startY Starting point of the scroll (Y)
411637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param velocityX Initial velocity of the fling (X) measured in pixels per
412637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            second.
413637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param velocityY Initial velocity of the fling (Y) measured in pixels per
414637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            second
415637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param minX Minimum X value. The scroller will not scroll past this point
416637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            unless overX > 0. If overfling is allowed, it will use minX as
417637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            a springback boundary.
418637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param maxX Maximum X value. The scroller will not scroll past this point
419637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            unless overX > 0. If overfling is allowed, it will use maxX as
420637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            a springback boundary.
421637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param minY Minimum Y value. The scroller will not scroll past this point
422637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            unless overY > 0. If overfling is allowed, it will use minY as
423637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            a springback boundary.
424637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param maxY Maximum Y value. The scroller will not scroll past this point
425637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            unless overY > 0. If overfling is allowed, it will use maxY as
426637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            a springback boundary.
427637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param overX Overfling range. If > 0, horizontal overfling in either
428637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            direction will be possible.
429637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param overY Overfling range. If > 0, vertical overfling in either
430637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *            direction will be possible.
431637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
432637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void fling(int startX, int startY, int velocityX, int velocityY,
433637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            int minX, int maxX, int minY, int maxY, int overX, int overY) {
434637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Continue a scroll or fling in progress
435637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        if (mFlywheel && !isFinished()) {
436637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            float oldVelocityX = mScrollerX.mCurrVelocity;
437637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            float oldVelocityY = mScrollerY.mCurrVelocity;
438637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (Math.signum(velocityX) == Math.signum(oldVelocityX) &&
439637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    Math.signum(velocityY) == Math.signum(oldVelocityY)) {
440637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                velocityX += oldVelocityX;
441637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                velocityY += oldVelocityY;
442637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
443637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
444637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
445637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mMode = FLING_MODE;
446637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.fling(startX, velocityX, minX, maxX, overX);
447637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerY.fling(startY, velocityY, minY, maxY, overY);
448637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
449637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
450637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
451637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Notify the scroller that we've reached a horizontal boundary.
452637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Normally the information to handle this will already be known
453637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * when the animation is started, such as in a call to one of the
454637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * fling functions. However there are cases where this cannot be known
455637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * in advance. This function will transition the current motion and
456637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * animate from startX to finalX as appropriate.
457637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
458637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startX Starting/current X position
459637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param finalX Desired final X position
460637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param overX Magnitude of overscroll allowed. This should be the maximum
461637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *              desired distance from finalX. Absolute value - must be positive.
462637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
463637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void notifyHorizontalEdgeReached(int startX, int finalX, int overX) {
464637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.notifyEdgeReached(startX, finalX, overX);
465637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
466637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
467637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
468637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Notify the scroller that we've reached a vertical boundary.
469637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Normally the information to handle this will already be known
470637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * when the animation is started, such as in a call to one of the
471637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * fling functions. However there are cases where this cannot be known
472637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * in advance. This function will animate a parabolic motion from
473637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * startY to finalY.
474637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
475637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param startY Starting/current Y position
476637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param finalY Desired final Y position
477637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @param overY Magnitude of overscroll allowed. This should be the maximum
478637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *              desired distance from finalY. Absolute value - must be positive.
479637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
480637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void notifyVerticalEdgeReached(int startY, int finalY, int overY) {
481637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerY.notifyEdgeReached(startY, finalY, overY);
482637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
483637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
484637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
485637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns whether the current Scroller is currently returning to a valid position.
486637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Valid bounds were provided by the
487637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * {@link #fling(int, int, int, int, int, int, int, int, int, int)} method.
488637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
489637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * One should check this value before calling
490637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * {@link #startScroll(int, int, int, int)} as the interpolation currently in progress
491637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * to restore a valid position will then be stopped. The caller has to take into account
492637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * the fact that the started scroll will start from an overscrolled position.
493637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
494637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return true when the current position is overscrolled and in the process of
495637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *         interpolating back to a valid value.
496637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
497637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public boolean isOverScrolled() {
498637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return ((!mScrollerX.mFinished &&
499c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                mScrollerX.mState != SplineOverScroller.SPLINE) ||
500637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                (!mScrollerY.mFinished &&
501c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                        mScrollerY.mState != SplineOverScroller.SPLINE));
502637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
503637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
504637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
505637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Stops the animation. Contrary to {@link #forceFinished(boolean)},
506637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * aborting the animating causes the scroller to move to the final x and y
507637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * positions.
508637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
509637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @see #forceFinished(boolean)
510637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
511637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public void abortAnimation() {
512637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerX.finish();
513637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        mScrollerY.finish();
514637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
515637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
516637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
517637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * Returns the time elapsed since the beginning of the scrolling.
518637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
519637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @return The elapsed time in milliseconds.
520637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     *
521637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide
522637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
523637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public int timePassed() {
524637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        final long time = AnimationUtils.currentAnimationTimeMillis();
525637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        final long startTime = Math.min(mScrollerX.mStartTime, mScrollerY.mStartTime);
526637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return (int) (time - startTime);
527637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
528637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
529637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    /**
530637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     * @hide
531637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell     */
532637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    public boolean isScrollingInDirection(float xvel, float yvel) {
533637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        final int dx = mScrollerX.mFinal - mScrollerX.mStart;
534637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        final int dy = mScrollerY.mFinal - mScrollerY.mStart;
535637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        return !isFinished() && Math.signum(xvel) == Math.signum(dx) &&
536637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        Math.signum(yvel) == Math.signum(dy);
537637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
538637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
539fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne    static class SplineOverScroller {
540637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Initial position
541fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mStart;
542637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
543637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Current position
544fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mCurrentPosition;
545637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
546637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Final position
547fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mFinal;
548637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
549637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Initial velocity
550fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mVelocity;
551637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
552637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Current velocity
553fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private float mCurrVelocity;
554637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
555637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Constant current deceleration
556fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private float mDeceleration;
557637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
558637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Animation starting time, in system milliseconds
559fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private long mStartTime;
560637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
561637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Animation duration, in milliseconds
562fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mDuration;
563637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
564637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Duration to complete spline component of animation
565fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mSplineDuration;
566637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
567637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Distance to travel along spline animation
568fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mSplineDistance;
569637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
570637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        // Whether the animation is currently in progress
571fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private boolean mFinished;
572637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
573fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        // The allowed overshot distance before boundary is reached.
574fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int mOver;
575fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
576fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        // Fling friction
577fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private float mFlingFriction = ViewConfiguration.getScrollFriction();
578fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
579fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        // Current state of the animation.
580c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private int mState = SPLINE;
581637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
582fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        // Constant gravity value, used in the deceleration phase.
583fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static final float GRAVITY = 2000.0f;
584fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
585fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        // A device specific coefficient adjusted to physical values.
586fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static float PHYSICAL_COEF;
587fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
588fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static float DECELERATION_RATE = (float) (Math.log(0.75) / Math.log(0.9));
589c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private static final float INFLEXION = 0.4f; // Tension lines cross at (INFLEXION, 1)
590c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private static final float START_TENSION = 1.0f;
591c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private static final float END_TENSION = 0.6666f;
592fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static final float P1 = START_TENSION * INFLEXION;
593fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static final float P2 = 1.0f - END_TENSION * (1.0f - INFLEXION);
594fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
595fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static final int NB_SAMPLES = 100;
596fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static final float[] SPLINE_POSITION = new float[NB_SAMPLES + 1];
597fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private static final float[] SPLINE_TIME = new float[NB_SAMPLES + 1];
598fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
599c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private static final int SPLINE = 0;
600c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private static final int CUBIC = 1;
601c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne        private static final int BALLISTIC = 2;
602637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
603fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        static {
604fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            float x_min = 0.0f;
605fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            float y_min = 0.0f;
606fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            for (int i = 0; i < NB_SAMPLES; i++) {
607fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                final float alpha = (float) i / NB_SAMPLES;
608fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
609fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                float x_max = 1.0f;
610fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                float x, tx, coef;
611fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                while (true) {
612fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    x = x_min + (x_max - x_min) / 2.0f;
613fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    coef = 3.0f * x * (1.0f - x);
614fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    tx = coef * ((1.0f - x) * P1 + x * P2) + x * x * x;
615fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    if (Math.abs(tx - alpha) < 1E-5) break;
616fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    if (tx > alpha) x_max = x;
617fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    else x_min = x;
618fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                }
619fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                SPLINE_POSITION[i] = coef * ((1.0f - x) * START_TENSION + x) + x * x * x;
620fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
621fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                float y_max = 1.0f;
622fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                float y, dy;
623fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                while (true) {
624fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    y = y_min + (y_max - y_min) / 2.0f;
625fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    coef = 3.0f * y * (1.0f - y);
626fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    dy = coef * ((1.0f - y) * START_TENSION + y) + y * y * y;
627fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    if (Math.abs(dy - alpha) < 1E-5) break;
628fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    if (dy > alpha) y_max = y;
629fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    else y_min = y;
630fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                }
631fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                SPLINE_TIME[i] = coef * ((1.0f - y) * P1 + y * P2) + y * y * y;
632fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            }
633fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            SPLINE_POSITION[NB_SAMPLES] = SPLINE_TIME[NB_SAMPLES] = 1.0f;
634fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        }
635637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
636fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        static void initFromContext(Context context) {
637fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            final float ppi = context.getResources().getDisplayMetrics().density * 160.0f;
638fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            PHYSICAL_COEF = SensorManager.GRAVITY_EARTH // g (m/s^2)
639fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    * 39.37f // inch/meter
640fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    * ppi
641fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                    * 0.84f; // look and feel tuning
642fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        }
643fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
644fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        void setFriction(float friction) {
645fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            mFlingFriction = friction;
646fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        }
647fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
648fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        SplineOverScroller() {
649637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = true;
650637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
651637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
652637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void updateScroll(float q) {
653637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mCurrentPosition = mStart + Math.round(q * (mFinal - mStart));
654637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
655637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
656637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        /*
657637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         * Get a signed deceleration that will reduce the velocity.
658637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         */
659fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        static private float getDeceleration(int velocity) {
660fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            return velocity > 0 ? -GRAVITY : GRAVITY;
661637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
662637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
663637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        /*
664637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         * Modifies mDuration to the duration it takes to get from start to newFinal using the
665637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         * spline interpolation. The previous duration was needed to get to oldFinal.
666637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         */
667fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private void adjustDuration(int start, int oldFinal, int newFinal) {
668637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final int oldDistance = oldFinal - start;
669637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final int newDistance = newFinal - start;
670fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            final float x = Math.abs((float) newDistance / oldDistance);
671637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final int index = (int) (NB_SAMPLES * x);
672637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (index < NB_SAMPLES) {
673637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final float x_inf = (float) index / NB_SAMPLES;
674637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final float x_sup = (float) (index + 1) / NB_SAMPLES;
675637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final float t_inf = SPLINE_TIME[index];
676637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final float t_sup = SPLINE_TIME[index + 1];
677637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                final float timeCoef = t_inf + (x - x_inf) / (x_sup - x_inf) * (t_sup - t_inf);
678637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                mDuration *= timeCoef;
679637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
680637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
681637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
682637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void startScroll(int start, int distance, int duration) {
683637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = false;
684637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
685637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStart = start;
686637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinal = start + distance;
687637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
688637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStartTime = AnimationUtils.currentAnimationTimeMillis();
689637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mDuration = duration;
690637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
691637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // Unused
692637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mDeceleration = 0.0f;
693637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mVelocity = 0;
694637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
695637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
696637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void finish() {
697637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mCurrentPosition = mFinal;
698637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // Not reset since WebView relies on this value for fast fling.
699637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // TODO: restore when WebView uses the fast fling implemented in this class.
700637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // mCurrVelocity = 0.0f;
701637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = true;
702637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
703637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
704637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void setFinalPosition(int position) {
705637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinal = position;
706637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = false;
707637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
708637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
709637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void extendDuration(int extend) {
710637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final long time = AnimationUtils.currentAnimationTimeMillis();
711637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final int elapsedTime = (int) (time - mStartTime);
712637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mDuration = elapsedTime + extend;
713637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = false;
714637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
715637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
716637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        boolean springback(int start, int min, int max) {
717637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = true;
718637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
719637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStart = mFinal = start;
720637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mVelocity = 0;
721637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
722637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStartTime = AnimationUtils.currentAnimationTimeMillis();
723637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mDuration = 0;
724637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
725637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (start < min) {
726637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                startSpringback(start, min, 0);
727637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            } else if (start > max) {
728637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                startSpringback(start, max, 0);
729637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
730637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
731637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            return !mFinished;
732637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
733637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
734637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        private void startSpringback(int start, int end, int velocity) {
735c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            // mStartTime has been set
736637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = false;
737c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mState = CUBIC;
738c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mStart = start;
739c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mFinal = end;
740c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            final int delta = start - end;
741c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mDeceleration = getDeceleration(delta);
742c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            // TODO take velocity into account
743c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mVelocity = -delta; // only sign is used
744c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mOver = Math.abs(delta);
745c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mDuration = (int) (1000.0 * Math.sqrt(-2.0 * delta / mDeceleration));
746637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
747637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
748637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void fling(int start, int velocity, int min, int max, int over) {
749637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mOver = over;
750637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinished = false;
751637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mCurrVelocity = mVelocity = velocity;
752637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mDuration = mSplineDuration = 0;
753637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStartTime = AnimationUtils.currentAnimationTimeMillis();
754fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            mCurrentPosition = mStart = start;
755637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
756637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (start > max || start < min) {
757637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                startAfterEdge(start, min, max, velocity);
758637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                return;
759637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
760637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
761c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mState = SPLINE;
762637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            double totalDistance = 0.0;
763637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
764637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (velocity != 0) {
765fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                mDuration = mSplineDuration = getSplineFlingDuration(velocity);
766fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                totalDistance = getSplineFlingDistance(velocity);
767637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
768637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
769637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mSplineDistance = (int) (totalDistance * Math.signum(velocity));
770637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mFinal = start + mSplineDistance;
771637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
772637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // Clamp to a valid final position
773637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (mFinal < min) {
774637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                adjustDuration(mStart, mFinal, min);
775637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                mFinal = min;
776637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
777637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
778637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (mFinal > max) {
779637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                adjustDuration(mStart, mFinal, max);
780637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                mFinal = max;
781637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
782637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
783637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
784fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private double getSplineDeceleration(int velocity) {
785fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            return Math.log(INFLEXION * Math.abs(velocity) / (mFlingFriction * PHYSICAL_COEF));
786fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        }
787fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
788fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private double getSplineFlingDistance(int velocity) {
789fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            final double l = getSplineDeceleration(velocity);
790fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            final double decelMinusOne = DECELERATION_RATE - 1.0;
791fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            return mFlingFriction * PHYSICAL_COEF * Math.exp(DECELERATION_RATE / decelMinusOne * l);
792fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        }
793fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
794fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        /* Returns the duration, expressed in milliseconds */
795fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        private int getSplineFlingDuration(int velocity) {
796fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            final double l = getSplineDeceleration(velocity);
797fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            final double decelMinusOne = DECELERATION_RATE - 1.0;
798fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            return (int) (1000.0 * Math.exp(l / decelMinusOne));
799fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne        }
800fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne
801637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        private void fitOnBounceCurve(int start, int end, int velocity) {
802637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // Simulate a bounce that started from edge
803637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final float durationToApex = - velocity / mDeceleration;
804637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final float distanceToApex = velocity * velocity / 2.0f / Math.abs(mDeceleration);
805637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final float distanceToEdge = Math.abs(end - start);
806637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final float totalDuration = (float) Math.sqrt(
807637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    2.0 * (distanceToApex + distanceToEdge) / Math.abs(mDeceleration));
808637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStartTime -= (int) (1000.0f * (totalDuration - durationToApex));
809637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mStart = end;
810637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mVelocity = (int) (- mDeceleration * totalDuration);
811637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
812637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
813637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        private void startBounceAfterEdge(int start, int end, int velocity) {
814637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mDeceleration = getDeceleration(velocity == 0 ? start - end : velocity);
815637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            fitOnBounceCurve(start, end, velocity);
816637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            onEdgeReached();
817637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
818637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
819637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        private void startAfterEdge(int start, int min, int max, int velocity) {
820637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (start > min && start < max) {
821fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                Log.e("OverScroller", "startAfterEdge called from a valid position");
822637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                mFinished = true;
823637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                return;
824637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
825637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final boolean positive = start > max;
826637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final int edge = positive ? max : min;
827637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final int overDistance = start - edge;
828637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            boolean keepIncreasing = overDistance * velocity >= 0;
829637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (keepIncreasing) {
830637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                // Will result in a bounce or a to_boundary depending on velocity.
831637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                startBounceAfterEdge(start, edge, velocity);
832637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            } else {
833fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                final double totalDistance = getSplineFlingDistance(velocity);
834637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                if (totalDistance > Math.abs(overDistance)) {
835637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    fling(start, velocity, positive ? min : start, positive ? start : max, mOver);
836637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                } else {
837637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    startSpringback(start, edge, velocity);
838637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
839637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
840637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
841637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
842637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        void notifyEdgeReached(int start, int end, int over) {
843c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            // mState is used to detect successive notifications
844c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            if (mState == SPLINE) {
845fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                mOver = over;
846fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                mStartTime = AnimationUtils.currentAnimationTimeMillis();
847fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                // We were in fling/scroll mode before: current velocity is such that distance to
848c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                // edge is increasing. This ensures that startAfterEdge will not start a new fling.
849fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne                startAfterEdge(start, end, end, (int) mCurrVelocity);
850fff4ab09b6c69e437537f322aaca7829f009ff1dGilles Debunne            }
851637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
852637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
853637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        private void onEdgeReached() {
854637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            // mStart, mVelocity and mStartTime were adjusted to their values when edge was reached.
855c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            float distance = mVelocity * mVelocity / (2.0f * Math.abs(mDeceleration));
856c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            final float sign = Math.signum(mVelocity);
857637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
858c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            if (distance > mOver) {
859c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                // Default deceleration is not sufficient to slow us down before boundary
860c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                 mDeceleration = - sign * mVelocity * mVelocity / (2.0f * mOver);
861c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                 distance = mOver;
862637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
863c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne
864c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mOver = (int) distance;
865c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mState = BALLISTIC;
866c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mFinal = mStart + (int) (mVelocity > 0 ? distance : -distance);
867c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne            mDuration = - (int) (1000.0f * mVelocity / mDeceleration);
868637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
869637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
870637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        boolean continueWhenFinished() {
871637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            switch (mState) {
872c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                case SPLINE:
873637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    // Duration from start to null velocity
874637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    if (mDuration < mSplineDuration) {
875637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        // If the animation was clamped, we reached the edge
876637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        mStart = mFinal;
877c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                        // TODO Better compute speed when edge was reached
878637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        mVelocity = (int) mCurrVelocity;
879637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        mDeceleration = getDeceleration(mVelocity);
880637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        mStartTime += mDuration;
881637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        onEdgeReached();
882637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    } else {
883637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        // Normal stop, no need to continue
884637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        return false;
885637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    }
886637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    break;
887c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                case BALLISTIC:
888637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    mStartTime += mDuration;
889c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    startSpringback(mFinal, mStart, 0);
890637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    break;
891c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                case CUBIC:
892c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    return false;
893637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
894637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
895637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            update();
896637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            return true;
897637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
898637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
899637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        /*
900637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         * Update the current position and velocity for current time. Returns
901637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         * true if update has been done and false if animation duration has been
902637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         * reached.
903637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell         */
904637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        boolean update() {
905637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final long time = AnimationUtils.currentAnimationTimeMillis();
906637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            final long currentTime = time - mStartTime;
907637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
908637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            if (currentTime > mDuration) {
909637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                return false;
910637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
911637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
912637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            double distance = 0.0;
913637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            switch (mState) {
914c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                case SPLINE: {
915637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    final float t = (float) currentTime / mSplineDuration;
916637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    final int index = (int) (NB_SAMPLES * t);
917637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    float distanceCoef = 1.f;
918637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    float velocityCoef = 0.f;
919637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    if (index < NB_SAMPLES) {
920637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        final float t_inf = (float) index / NB_SAMPLES;
921637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        final float t_sup = (float) (index + 1) / NB_SAMPLES;
922637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        final float d_inf = SPLINE_POSITION[index];
923637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        final float d_sup = SPLINE_POSITION[index + 1];
924637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        velocityCoef = (d_sup - d_inf) / (t_sup - t_inf);
925637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                        distanceCoef = d_inf + (t - t_inf) * velocityCoef;
926637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    }
927637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
928637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    distance = distanceCoef * mSplineDistance;
929c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    mCurrVelocity = velocityCoef * mSplineDistance / mSplineDuration * 1000.0f;
930637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    break;
931637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
932637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
933c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                case BALLISTIC: {
934637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    final float t = currentTime / 1000.0f;
935637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    mCurrVelocity = mVelocity + mDeceleration * t;
936637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    distance = mVelocity * t + mDeceleration * t * t / 2.0f;
937637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    break;
938637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
939637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
940c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                case CUBIC: {
941c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    final float t = (float) (currentTime) / mDuration;
942c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    final float t2 = t * t;
943c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    final float sign = Math.signum(mVelocity);
944c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    distance = sign * mOver * (3.0f * t2 - 2.0f * t * t2);
945c35a829b10946fe88b53fd3354b55218ecaff00eGilles Debunne                    mCurrVelocity = sign * mOver * 6.0f * (- t + t2);
946637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                    break;
947637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell                }
948637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            }
949637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell
950637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            mCurrentPosition = mStart + (int) Math.round(distance);
95134961cc2e5cd483b15dfd1ff1eb33b01b849e0dcGilles Debunne
952637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell            return true;
953637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell        }
954637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell    }
955637d337b58d8eec6de19230a5dd5ca5581c0478dAdam Powell}
956