blob: 7154f2bdee42904d29ad905108926876480a872b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Mathew Inwoode5ad5982018-08-17 15:07:52 +010019import android.annotation.UnsupportedAppUsage;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080020import android.util.Pools.SynchronizedPool;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22/**
23 * Helper for tracking the velocity of touch events, for implementing
Jeff Brown2ed24622011-03-14 19:39:54 -070024 * flinging and other such gestures.
25 *
26 * Use {@link #obtain} to retrieve a new instance of the class when you are going
27 * to begin tracking. Put the motion events you receive into it with
28 * {@link #addMovement(MotionEvent)}. When you want to determine the velocity call
29 * {@link #computeCurrentVelocity(int)} and then call {@link #getXVelocity(int)}
Gilles Debunne185b8252012-01-17 11:12:27 -080030 * and {@link #getYVelocity(int)} to retrieve the velocity for each pointer id.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080032public final class VelocityTracker {
33 private static final SynchronizedPool<VelocityTracker> sPool =
34 new SynchronizedPool<VelocityTracker>(2);
Romain Guyd928d682009-03-31 17:52:16 -070035
Jeff Brown2ed24622011-03-14 19:39:54 -070036 private static final int ACTIVE_POINTER_ID = -1;
37
Ashok Bhat96804bc2014-01-08 15:45:39 +000038 private long mPtr;
Jeff Brown9eb7d862012-06-01 12:39:25 -070039 private final String mStrategy;
40
Ashok Bhat96804bc2014-01-08 15:45:39 +000041 private static native long nativeInitialize(String strategy);
42 private static native void nativeDispose(long ptr);
43 private static native void nativeClear(long ptr);
44 private static native void nativeAddMovement(long ptr, MotionEvent event);
45 private static native void nativeComputeCurrentVelocity(long ptr, int units, float maxVelocity);
46 private static native float nativeGetXVelocity(long ptr, int id);
47 private static native float nativeGetYVelocity(long ptr, int id);
48 private static native boolean nativeGetEstimator(long ptr, int id, Estimator outEstimator);
Jeff Brown2ed24622011-03-14 19:39:54 -070049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050 /**
51 * Retrieve a new VelocityTracker object to watch the velocity of a
52 * motion. Be sure to call {@link #recycle} when done. You should
53 * generally only maintain an active object while tracking a movement,
54 * so that the VelocityTracker can be re-used elsewhere.
Romain Guyd928d682009-03-31 17:52:16 -070055 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 * @return Returns a new VelocityTracker.
57 */
58 static public VelocityTracker obtain() {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080059 VelocityTracker instance = sPool.acquire();
60 return (instance != null) ? instance : new VelocityTracker(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 }
Romain Guyd928d682009-03-31 17:52:16 -070062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 /**
Jeff Brown9eb7d862012-06-01 12:39:25 -070064 * Obtains a velocity tracker with the specified strategy.
65 * For testing and comparison purposes only.
66 *
67 * @param strategy The strategy, or null to use the default.
68 * @return The velocity tracker.
69 *
70 * @hide
71 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +010072 @UnsupportedAppUsage
Jeff Brown9eb7d862012-06-01 12:39:25 -070073 public static VelocityTracker obtain(String strategy) {
74 if (strategy == null) {
75 return obtain();
76 }
77 return new VelocityTracker(strategy);
78 }
79
80 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * Return a VelocityTracker object back to be re-used by others. You must
82 * not touch the object after calling this function.
83 */
84 public void recycle() {
Jeff Brown9eb7d862012-06-01 12:39:25 -070085 if (mStrategy == null) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080086 clear();
Jeff Brown9eb7d862012-06-01 12:39:25 -070087 sPool.release(this);
88 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
Romain Guyd928d682009-03-31 17:52:16 -070090
Jeff Brown9eb7d862012-06-01 12:39:25 -070091 private VelocityTracker(String strategy) {
92 mPtr = nativeInitialize(strategy);
93 mStrategy = strategy;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 }
Jeff Brown2ed24622011-03-14 19:39:54 -070095
96 @Override
97 protected void finalize() throws Throwable {
98 try {
99 if (mPtr != 0) {
100 nativeDispose(mPtr);
101 mPtr = 0;
102 }
103 } finally {
104 super.finalize();
105 }
106 }
107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 /**
109 * Reset the velocity tracker back to its initial state.
110 */
111 public void clear() {
Jeff Brown2ed24622011-03-14 19:39:54 -0700112 nativeClear(mPtr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
114
115 /**
116 * Add a user's movement to the tracker. You should call this for the
117 * initial {@link MotionEvent#ACTION_DOWN}, the following
118 * {@link MotionEvent#ACTION_MOVE} events that you receive, and the
119 * final {@link MotionEvent#ACTION_UP}. You can, however, call this
120 * for whichever events you desire.
121 *
Jeff Brown2ed24622011-03-14 19:39:54 -0700122 * @param event The MotionEvent you received and would like to track.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
Jeff Brown2ed24622011-03-14 19:39:54 -0700124 public void addMovement(MotionEvent event) {
125 if (event == null) {
126 throw new IllegalArgumentException("event must not be null");
Adam Powell73d8fca2010-02-12 16:50:19 -0800127 }
Jeff Brown2ed24622011-03-14 19:39:54 -0700128 nativeAddMovement(mPtr, event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 }
Romain Guy4296fc42009-07-06 11:48:52 -0700130
131 /**
132 * Equivalent to invoking {@link #computeCurrentVelocity(int, float)} with a maximum
133 * velocity of Float.MAX_VALUE.
134 *
135 * @see #computeCurrentVelocity(int, float)
136 */
137 public void computeCurrentVelocity(int units) {
Jeff Brown2ed24622011-03-14 19:39:54 -0700138 nativeComputeCurrentVelocity(mPtr, units, Float.MAX_VALUE);
Romain Guy4296fc42009-07-06 11:48:52 -0700139 }
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
142 * Compute the current velocity based on the points that have been
143 * collected. Only call this when you actually want to retrieve velocity
144 * information, as it is relatively expensive. You can then retrieve
145 * the velocity with {@link #getXVelocity()} and
146 * {@link #getYVelocity()}.
147 *
148 * @param units The units you would like the velocity in. A value of 1
149 * provides pixels per millisecond, 1000 provides pixels per second, etc.
Romain Guy4296fc42009-07-06 11:48:52 -0700150 * @param maxVelocity The maximum velocity that can be computed by this method.
151 * This value must be declared in the same unit as the units parameter. This value
152 * must be positive.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 */
Romain Guy4296fc42009-07-06 11:48:52 -0700154 public void computeCurrentVelocity(int units, float maxVelocity) {
Jeff Brown2ed24622011-03-14 19:39:54 -0700155 nativeComputeCurrentVelocity(mPtr, units, maxVelocity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
157
158 /**
159 * Retrieve the last computed X velocity. You must first call
160 * {@link #computeCurrentVelocity(int)} before calling this function.
161 *
162 * @return The previously computed X velocity.
163 */
164 public float getXVelocity() {
Jeff Brown2ed24622011-03-14 19:39:54 -0700165 return nativeGetXVelocity(mPtr, ACTIVE_POINTER_ID);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167
168 /**
169 * Retrieve the last computed Y velocity. You must first call
170 * {@link #computeCurrentVelocity(int)} before calling this function.
171 *
172 * @return The previously computed Y velocity.
173 */
174 public float getYVelocity() {
Jeff Brown2ed24622011-03-14 19:39:54 -0700175 return nativeGetYVelocity(mPtr, ACTIVE_POINTER_ID);
Adam Powell8acdb202010-01-06 17:33:52 -0800176 }
177
178 /**
179 * Retrieve the last computed X velocity. You must first call
180 * {@link #computeCurrentVelocity(int)} before calling this function.
181 *
Adam Powell73d8fca2010-02-12 16:50:19 -0800182 * @param id Which pointer's velocity to return.
Adam Powell8acdb202010-01-06 17:33:52 -0800183 * @return The previously computed X velocity.
Adam Powell8acdb202010-01-06 17:33:52 -0800184 */
Adam Powell73d8fca2010-02-12 16:50:19 -0800185 public float getXVelocity(int id) {
Jeff Brown2ed24622011-03-14 19:39:54 -0700186 return nativeGetXVelocity(mPtr, id);
Adam Powell8acdb202010-01-06 17:33:52 -0800187 }
188
189 /**
190 * Retrieve the last computed Y velocity. You must first call
191 * {@link #computeCurrentVelocity(int)} before calling this function.
192 *
Adam Powell73d8fca2010-02-12 16:50:19 -0800193 * @param id Which pointer's velocity to return.
Adam Powell8acdb202010-01-06 17:33:52 -0800194 * @return The previously computed Y velocity.
Adam Powell8acdb202010-01-06 17:33:52 -0800195 */
Adam Powell73d8fca2010-02-12 16:50:19 -0800196 public float getYVelocity(int id) {
Jeff Brown2ed24622011-03-14 19:39:54 -0700197 return nativeGetYVelocity(mPtr, id);
Jeff Brown88cf2fc2010-08-09 18:50:35 -0700198 }
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700199
200 /**
201 * Get an estimator for the movements of a pointer using past movements of the
202 * pointer to predict future movements.
203 *
204 * It is not necessary to call {@link #computeCurrentVelocity(int)} before calling
205 * this method.
206 *
207 * @param id Which pointer's velocity to return.
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700208 * @param outEstimator The estimator to populate.
209 * @return True if an estimator was obtained, false if there is no information
210 * available about the pointer.
211 *
212 * @hide For internal use only. Not a final API.
213 */
Jeff Brown85bd0d62012-05-13 15:30:42 -0700214 public boolean getEstimator(int id, Estimator outEstimator) {
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700215 if (outEstimator == null) {
216 throw new IllegalArgumentException("outEstimator must not be null");
217 }
Jeff Brown85bd0d62012-05-13 15:30:42 -0700218 return nativeGetEstimator(mPtr, id, outEstimator);
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700219 }
220
221 /**
222 * An estimator for the movements of a pointer based on a polynomial model.
223 *
224 * The last recorded position of the pointer is at time zero seconds.
225 * Past estimated positions are at negative times and future estimated positions
226 * are at positive times.
227 *
228 * First coefficient is position (in pixels), second is velocity (in pixels per second),
229 * third is acceleration (in pixels per second squared).
230 *
231 * @hide For internal use only. Not a final API.
232 */
233 public static final class Estimator {
234 // Must match VelocityTracker::Estimator::MAX_DEGREE
Jeff Brown9eb7d862012-06-01 12:39:25 -0700235 private static final int MAX_DEGREE = 4;
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700236
237 /**
238 * Polynomial coefficients describing motion in X.
239 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100240 @UnsupportedAppUsage
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700241 public final float[] xCoeff = new float[MAX_DEGREE + 1];
242
243 /**
244 * Polynomial coefficients describing motion in Y.
245 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100246 @UnsupportedAppUsage
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700247 public final float[] yCoeff = new float[MAX_DEGREE + 1];
248
249 /**
250 * Polynomial degree, or zero if only position information is available.
251 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100252 @UnsupportedAppUsage
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700253 public int degree;
254
255 /**
256 * Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
257 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100258 @UnsupportedAppUsage
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700259 public float confidence;
260
261 /**
262 * Gets an estimate of the X position of the pointer at the specified time point.
263 * @param time The time point in seconds, 0 is the last recorded time.
264 * @return The estimated X coordinate.
265 */
266 public float estimateX(float time) {
267 return estimate(time, xCoeff);
268 }
269
270 /**
271 * Gets an estimate of the Y position of the pointer at the specified time point.
272 * @param time The time point in seconds, 0 is the last recorded time.
273 * @return The estimated Y coordinate.
274 */
275 public float estimateY(float time) {
276 return estimate(time, yCoeff);
277 }
278
Jeff Brown8a90e6e2012-05-11 12:24:35 -0700279 /**
280 * Gets the X coefficient with the specified index.
281 * @param index The index of the coefficient to return.
282 * @return The X coefficient, or 0 if the index is greater than the degree.
283 */
284 public float getXCoeff(int index) {
285 return index <= degree ? xCoeff[index] : 0;
286 }
287
288 /**
289 * Gets the Y coefficient with the specified index.
290 * @param index The index of the coefficient to return.
291 * @return The Y coefficient, or 0 if the index is greater than the degree.
292 */
293 public float getYCoeff(int index) {
294 return index <= degree ? yCoeff[index] : 0;
295 }
296
Jeff Brownb59ab9f2011-09-14 10:53:18 -0700297 private float estimate(float time, float[] c) {
298 float a = 0;
299 float scale = 1;
300 for (int i = 0; i <= degree; i++) {
301 a += c[i] * scale;
302 scale *= time;
303 }
304 return a;
305 }
306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307}