The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.view; |
| 18 | |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 19 | import android.util.Poolable; |
| 20 | import android.util.Pool; |
Romain Guy | 2e9bbce | 2009-04-01 10:40:10 -0700 | [diff] [blame] | 21 | import android.util.Pools; |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 22 | import android.util.PoolableManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Helper for tracking the velocity of touch events, for implementing |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 26 | * flinging and other such gestures. |
| 27 | * |
| 28 | * Use {@link #obtain} to retrieve a new instance of the class when you are going |
| 29 | * to begin tracking. Put the motion events you receive into it with |
| 30 | * {@link #addMovement(MotionEvent)}. When you want to determine the velocity call |
| 31 | * {@link #computeCurrentVelocity(int)} and then call {@link #getXVelocity(int)} |
| 32 | * and {@link #getXVelocity(int)} to retrieve the velocity for each pointer id. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | */ |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 34 | public final class VelocityTracker implements Poolable<VelocityTracker> { |
Romain Guy | 2e9bbce | 2009-04-01 10:40:10 -0700 | [diff] [blame] | 35 | private static final Pool<VelocityTracker> sPool = Pools.synchronizedPool( |
| 36 | Pools.finitePool(new PoolableManager<VelocityTracker>() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 37 | public VelocityTracker newInstance() { |
| 38 | return new VelocityTracker(); |
| 39 | } |
| 40 | |
| 41 | public void onAcquired(VelocityTracker element) { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public void onReleased(VelocityTracker element) { |
Jeff Brown | 88cf2fc | 2010-08-09 18:50:35 -0700 | [diff] [blame] | 45 | element.clear(); |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 46 | } |
| 47 | }, 2)); |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 48 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 49 | private static final int ACTIVE_POINTER_ID = -1; |
| 50 | |
| 51 | private int mPtr; |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 52 | private VelocityTracker mNext; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 53 | private boolean mIsPooled; |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 54 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 55 | private static native int nativeInitialize(); |
| 56 | private static native void nativeDispose(int ptr); |
| 57 | private static native void nativeClear(int ptr); |
| 58 | private static native void nativeAddMovement(int ptr, MotionEvent event); |
| 59 | private static native void nativeComputeCurrentVelocity(int ptr, int units, float maxVelocity); |
| 60 | private static native float nativeGetXVelocity(int ptr, int id); |
| 61 | private static native float nativeGetYVelocity(int ptr, int id); |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 62 | private static native boolean nativeGetEstimator(int ptr, int id, |
| 63 | int degree, int horizonMillis, Estimator outEstimator); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /** |
| 66 | * Retrieve a new VelocityTracker object to watch the velocity of a |
| 67 | * motion. Be sure to call {@link #recycle} when done. You should |
| 68 | * generally only maintain an active object while tracking a movement, |
| 69 | * so that the VelocityTracker can be re-used elsewhere. |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 70 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * @return Returns a new VelocityTracker. |
| 72 | */ |
| 73 | static public VelocityTracker obtain() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 74 | return sPool.acquire(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Return a VelocityTracker object back to be re-used by others. You must |
| 79 | * not touch the object after calling this function. |
| 80 | */ |
| 81 | public void recycle() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 82 | sPool.release(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 84 | |
| 85 | /** |
| 86 | * @hide |
| 87 | */ |
| 88 | public void setNextPoolable(VelocityTracker element) { |
| 89 | mNext = element; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @hide |
| 94 | */ |
| 95 | public VelocityTracker getNextPoolable() { |
| 96 | return mNext; |
| 97 | } |
| 98 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 99 | /** |
| 100 | * @hide |
| 101 | */ |
| 102 | public boolean isPooled() { |
| 103 | return mIsPooled; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @hide |
| 108 | */ |
| 109 | public void setPooled(boolean isPooled) { |
| 110 | mIsPooled = isPooled; |
| 111 | } |
| 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | private VelocityTracker() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 114 | mPtr = nativeInitialize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | } |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | protected void finalize() throws Throwable { |
| 119 | try { |
| 120 | if (mPtr != 0) { |
| 121 | nativeDispose(mPtr); |
| 122 | mPtr = 0; |
| 123 | } |
| 124 | } finally { |
| 125 | super.finalize(); |
| 126 | } |
| 127 | } |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | /** |
| 130 | * Reset the velocity tracker back to its initial state. |
| 131 | */ |
| 132 | public void clear() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 133 | nativeClear(mPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Add a user's movement to the tracker. You should call this for the |
| 138 | * initial {@link MotionEvent#ACTION_DOWN}, the following |
| 139 | * {@link MotionEvent#ACTION_MOVE} events that you receive, and the |
| 140 | * final {@link MotionEvent#ACTION_UP}. You can, however, call this |
| 141 | * for whichever events you desire. |
| 142 | * |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 143 | * @param event The MotionEvent you received and would like to track. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | */ |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 145 | public void addMovement(MotionEvent event) { |
| 146 | if (event == null) { |
| 147 | throw new IllegalArgumentException("event must not be null"); |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 148 | } |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 149 | nativeAddMovement(mPtr, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | } |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Equivalent to invoking {@link #computeCurrentVelocity(int, float)} with a maximum |
| 154 | * velocity of Float.MAX_VALUE. |
| 155 | * |
| 156 | * @see #computeCurrentVelocity(int, float) |
| 157 | */ |
| 158 | public void computeCurrentVelocity(int units) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 159 | nativeComputeCurrentVelocity(mPtr, units, Float.MAX_VALUE); |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 160 | } |
| 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Compute the current velocity based on the points that have been |
| 164 | * collected. Only call this when you actually want to retrieve velocity |
| 165 | * information, as it is relatively expensive. You can then retrieve |
| 166 | * the velocity with {@link #getXVelocity()} and |
| 167 | * {@link #getYVelocity()}. |
| 168 | * |
| 169 | * @param units The units you would like the velocity in. A value of 1 |
| 170 | * provides pixels per millisecond, 1000 provides pixels per second, etc. |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 171 | * @param maxVelocity The maximum velocity that can be computed by this method. |
| 172 | * This value must be declared in the same unit as the units parameter. This value |
| 173 | * must be positive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | */ |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 175 | public void computeCurrentVelocity(int units, float maxVelocity) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 176 | nativeComputeCurrentVelocity(mPtr, units, maxVelocity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Retrieve the last computed X velocity. You must first call |
| 181 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 182 | * |
| 183 | * @return The previously computed X velocity. |
| 184 | */ |
| 185 | public float getXVelocity() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 186 | return nativeGetXVelocity(mPtr, ACTIVE_POINTER_ID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Retrieve the last computed Y velocity. You must first call |
| 191 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 192 | * |
| 193 | * @return The previously computed Y velocity. |
| 194 | */ |
| 195 | public float getYVelocity() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 196 | return nativeGetYVelocity(mPtr, ACTIVE_POINTER_ID); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Retrieve the last computed X velocity. You must first call |
| 201 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 202 | * |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 203 | * @param id Which pointer's velocity to return. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 204 | * @return The previously computed X velocity. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 205 | */ |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 206 | public float getXVelocity(int id) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 207 | return nativeGetXVelocity(mPtr, id); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Retrieve the last computed Y velocity. You must first call |
| 212 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 213 | * |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 214 | * @param id Which pointer's velocity to return. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 215 | * @return The previously computed Y velocity. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 216 | */ |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 217 | public float getYVelocity(int id) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 218 | return nativeGetYVelocity(mPtr, id); |
Jeff Brown | 88cf2fc | 2010-08-09 18:50:35 -0700 | [diff] [blame] | 219 | } |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Get an estimator for the movements of a pointer using past movements of the |
| 223 | * pointer to predict future movements. |
| 224 | * |
| 225 | * It is not necessary to call {@link #computeCurrentVelocity(int)} before calling |
| 226 | * this method. |
| 227 | * |
| 228 | * @param id Which pointer's velocity to return. |
| 229 | * @param degree The desired polynomial degree. The actual estimator may have |
| 230 | * a lower degree than what is requested here. If -1, uses the default degree. |
| 231 | * @param horizonMillis The maximum age of the oldest sample to consider, in milliseconds. |
| 232 | * If -1, uses the default horizon. |
| 233 | * @param outEstimator The estimator to populate. |
| 234 | * @return True if an estimator was obtained, false if there is no information |
| 235 | * available about the pointer. |
| 236 | * |
| 237 | * @hide For internal use only. Not a final API. |
| 238 | */ |
| 239 | public boolean getEstimator(int id, int degree, int horizonMillis, Estimator outEstimator) { |
| 240 | if (outEstimator == null) { |
| 241 | throw new IllegalArgumentException("outEstimator must not be null"); |
| 242 | } |
| 243 | return nativeGetEstimator(mPtr, id, degree, horizonMillis, outEstimator); |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * An estimator for the movements of a pointer based on a polynomial model. |
| 248 | * |
| 249 | * The last recorded position of the pointer is at time zero seconds. |
| 250 | * Past estimated positions are at negative times and future estimated positions |
| 251 | * are at positive times. |
| 252 | * |
| 253 | * First coefficient is position (in pixels), second is velocity (in pixels per second), |
| 254 | * third is acceleration (in pixels per second squared). |
| 255 | * |
| 256 | * @hide For internal use only. Not a final API. |
| 257 | */ |
| 258 | public static final class Estimator { |
| 259 | // Must match VelocityTracker::Estimator::MAX_DEGREE |
| 260 | private static final int MAX_DEGREE = 2; |
| 261 | |
| 262 | /** |
| 263 | * Polynomial coefficients describing motion in X. |
| 264 | */ |
| 265 | public final float[] xCoeff = new float[MAX_DEGREE + 1]; |
| 266 | |
| 267 | /** |
| 268 | * Polynomial coefficients describing motion in Y. |
| 269 | */ |
| 270 | public final float[] yCoeff = new float[MAX_DEGREE + 1]; |
| 271 | |
| 272 | /** |
| 273 | * Polynomial degree, or zero if only position information is available. |
| 274 | */ |
| 275 | public int degree; |
| 276 | |
| 277 | /** |
| 278 | * Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit). |
| 279 | */ |
| 280 | public float confidence; |
| 281 | |
| 282 | /** |
| 283 | * Gets an estimate of the X position of the pointer at the specified time point. |
| 284 | * @param time The time point in seconds, 0 is the last recorded time. |
| 285 | * @return The estimated X coordinate. |
| 286 | */ |
| 287 | public float estimateX(float time) { |
| 288 | return estimate(time, xCoeff); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Gets an estimate of the Y position of the pointer at the specified time point. |
| 293 | * @param time The time point in seconds, 0 is the last recorded time. |
| 294 | * @return The estimated Y coordinate. |
| 295 | */ |
| 296 | public float estimateY(float time) { |
| 297 | return estimate(time, yCoeff); |
| 298 | } |
| 299 | |
| 300 | private float estimate(float time, float[] c) { |
| 301 | float a = 0; |
| 302 | float scale = 1; |
| 303 | for (int i = 0; i <= degree; i++) { |
| 304 | a += c[i] * scale; |
| 305 | scale *= time; |
| 306 | } |
| 307 | return a; |
| 308 | } |
| 309 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | } |