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; |
| 53 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 54 | private static native int nativeInitialize(); |
| 55 | private static native void nativeDispose(int ptr); |
| 56 | private static native void nativeClear(int ptr); |
| 57 | private static native void nativeAddMovement(int ptr, MotionEvent event); |
| 58 | private static native void nativeComputeCurrentVelocity(int ptr, int units, float maxVelocity); |
| 59 | private static native float nativeGetXVelocity(int ptr, int id); |
| 60 | private static native float nativeGetYVelocity(int ptr, int id); |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Retrieve a new VelocityTracker object to watch the velocity of a |
| 64 | * motion. Be sure to call {@link #recycle} when done. You should |
| 65 | * generally only maintain an active object while tracking a movement, |
| 66 | * so that the VelocityTracker can be re-used elsewhere. |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 67 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | * @return Returns a new VelocityTracker. |
| 69 | */ |
| 70 | static public VelocityTracker obtain() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 71 | return sPool.acquire(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Return a VelocityTracker object back to be re-used by others. You must |
| 76 | * not touch the object after calling this function. |
| 77 | */ |
| 78 | public void recycle() { |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 79 | sPool.release(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
Romain Guy | d928d68 | 2009-03-31 17:52:16 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * @hide |
| 84 | */ |
| 85 | public void setNextPoolable(VelocityTracker element) { |
| 86 | mNext = element; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @hide |
| 91 | */ |
| 92 | public VelocityTracker getNextPoolable() { |
| 93 | return mNext; |
| 94 | } |
| 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | private VelocityTracker() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 97 | mPtr = nativeInitialize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 99 | |
| 100 | @Override |
| 101 | protected void finalize() throws Throwable { |
| 102 | try { |
| 103 | if (mPtr != 0) { |
| 104 | nativeDispose(mPtr); |
| 105 | mPtr = 0; |
| 106 | } |
| 107 | } finally { |
| 108 | super.finalize(); |
| 109 | } |
| 110 | } |
| 111 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | /** |
| 113 | * Reset the velocity tracker back to its initial state. |
| 114 | */ |
| 115 | public void clear() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 116 | nativeClear(mPtr); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Add a user's movement to the tracker. You should call this for the |
| 121 | * initial {@link MotionEvent#ACTION_DOWN}, the following |
| 122 | * {@link MotionEvent#ACTION_MOVE} events that you receive, and the |
| 123 | * final {@link MotionEvent#ACTION_UP}. You can, however, call this |
| 124 | * for whichever events you desire. |
| 125 | * |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 126 | * @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] | 127 | */ |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 128 | public void addMovement(MotionEvent event) { |
| 129 | if (event == null) { |
| 130 | throw new IllegalArgumentException("event must not be null"); |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 131 | } |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 132 | nativeAddMovement(mPtr, event); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | } |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Equivalent to invoking {@link #computeCurrentVelocity(int, float)} with a maximum |
| 137 | * velocity of Float.MAX_VALUE. |
| 138 | * |
| 139 | * @see #computeCurrentVelocity(int, float) |
| 140 | */ |
| 141 | public void computeCurrentVelocity(int units) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 142 | nativeComputeCurrentVelocity(mPtr, units, Float.MAX_VALUE); |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 143 | } |
| 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | /** |
| 146 | * Compute the current velocity based on the points that have been |
| 147 | * collected. Only call this when you actually want to retrieve velocity |
| 148 | * information, as it is relatively expensive. You can then retrieve |
| 149 | * the velocity with {@link #getXVelocity()} and |
| 150 | * {@link #getYVelocity()}. |
| 151 | * |
| 152 | * @param units The units you would like the velocity in. A value of 1 |
| 153 | * provides pixels per millisecond, 1000 provides pixels per second, etc. |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 154 | * @param maxVelocity The maximum velocity that can be computed by this method. |
| 155 | * This value must be declared in the same unit as the units parameter. This value |
| 156 | * must be positive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | */ |
Romain Guy | 4296fc4 | 2009-07-06 11:48:52 -0700 | [diff] [blame] | 158 | public void computeCurrentVelocity(int units, float maxVelocity) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 159 | nativeComputeCurrentVelocity(mPtr, units, maxVelocity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Retrieve the last computed X velocity. You must first call |
| 164 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 165 | * |
| 166 | * @return The previously computed X velocity. |
| 167 | */ |
| 168 | public float getXVelocity() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 169 | return nativeGetXVelocity(mPtr, ACTIVE_POINTER_ID); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Retrieve the last computed Y velocity. You must first call |
| 174 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 175 | * |
| 176 | * @return The previously computed Y velocity. |
| 177 | */ |
| 178 | public float getYVelocity() { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 179 | return nativeGetYVelocity(mPtr, ACTIVE_POINTER_ID); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Retrieve the last computed X velocity. You must first call |
| 184 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 185 | * |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 186 | * @param id Which pointer's velocity to return. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 187 | * @return The previously computed X velocity. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 188 | */ |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 189 | public float getXVelocity(int id) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 190 | return nativeGetXVelocity(mPtr, id); |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Retrieve the last computed Y velocity. You must first call |
| 195 | * {@link #computeCurrentVelocity(int)} before calling this function. |
| 196 | * |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 197 | * @param id Which pointer's velocity to return. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 198 | * @return The previously computed Y velocity. |
Adam Powell | 8acdb20 | 2010-01-06 17:33:52 -0800 | [diff] [blame] | 199 | */ |
Adam Powell | 73d8fca | 2010-02-12 16:50:19 -0800 | [diff] [blame] | 200 | public float getYVelocity(int id) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 201 | return nativeGetYVelocity(mPtr, id); |
Jeff Brown | 88cf2fc | 2010-08-09 18:50:35 -0700 | [diff] [blame] | 202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } |