Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | #define LOG_TAG "VelocityTracker-JNI" |
| 18 | |
| 19 | #include "JNIHelp.h" |
| 20 | |
| 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | #include <utils/Log.h> |
Jeff Brown | 9d3b1a4 | 2013-07-01 19:07:15 -0700 | [diff] [blame] | 23 | #include <input/Input.h> |
| 24 | #include <input/VelocityTracker.h> |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 25 | #include "android_view_MotionEvent.h" |
| 26 | |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 27 | #include <ScopedUtfChars.h> |
| 28 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // Special constant to request the velocity of the active pointer. |
| 33 | static const int ACTIVE_POINTER_ID = -1; |
| 34 | |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 35 | static struct { |
| 36 | jfieldID xCoeff; |
| 37 | jfieldID yCoeff; |
| 38 | jfieldID degree; |
| 39 | jfieldID confidence; |
| 40 | } gEstimatorClassInfo; |
| 41 | |
| 42 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 43 | // --- VelocityTrackerState --- |
| 44 | |
| 45 | class VelocityTrackerState { |
| 46 | public: |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 47 | VelocityTrackerState(const char* strategy); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 48 | |
| 49 | void clear(); |
| 50 | void addMovement(const MotionEvent* event); |
| 51 | void computeCurrentVelocity(int32_t units, float maxVelocity); |
| 52 | void getVelocity(int32_t id, float* outVx, float* outVy); |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 53 | bool getEstimator(int32_t id, VelocityTracker::Estimator* outEstimator); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 54 | |
| 55 | private: |
| 56 | struct Velocity { |
| 57 | float vx, vy; |
| 58 | }; |
| 59 | |
| 60 | VelocityTracker mVelocityTracker; |
| 61 | int32_t mActivePointerId; |
| 62 | BitSet32 mCalculatedIdBits; |
| 63 | Velocity mCalculatedVelocity[MAX_POINTERS]; |
| 64 | }; |
| 65 | |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 66 | VelocityTrackerState::VelocityTrackerState(const char* strategy) : |
| 67 | mVelocityTracker(strategy), mActivePointerId(-1) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void VelocityTrackerState::clear() { |
| 71 | mVelocityTracker.clear(); |
| 72 | mActivePointerId = -1; |
| 73 | mCalculatedIdBits.clear(); |
| 74 | } |
| 75 | |
| 76 | void VelocityTrackerState::addMovement(const MotionEvent* event) { |
| 77 | mVelocityTracker.addMovement(event); |
| 78 | } |
| 79 | |
| 80 | void VelocityTrackerState::computeCurrentVelocity(int32_t units, float maxVelocity) { |
| 81 | BitSet32 idBits(mVelocityTracker.getCurrentPointerIdBits()); |
| 82 | mCalculatedIdBits = idBits; |
| 83 | |
| 84 | for (uint32_t index = 0; !idBits.isEmpty(); index++) { |
Jeff Brown | be1aa82 | 2011-07-27 16:04:54 -0700 | [diff] [blame] | 85 | uint32_t id = idBits.clearFirstMarkedBit(); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 86 | |
| 87 | float vx, vy; |
| 88 | mVelocityTracker.getVelocity(id, &vx, &vy); |
| 89 | |
| 90 | vx = vx * units / 1000; |
| 91 | vy = vy * units / 1000; |
| 92 | |
| 93 | if (vx > maxVelocity) { |
| 94 | vx = maxVelocity; |
| 95 | } else if (vx < -maxVelocity) { |
| 96 | vx = -maxVelocity; |
| 97 | } |
| 98 | if (vy > maxVelocity) { |
| 99 | vy = maxVelocity; |
| 100 | } else if (vy < -maxVelocity) { |
| 101 | vy = -maxVelocity; |
| 102 | } |
| 103 | |
| 104 | Velocity& velocity = mCalculatedVelocity[index]; |
| 105 | velocity.vx = vx; |
| 106 | velocity.vy = vy; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void VelocityTrackerState::getVelocity(int32_t id, float* outVx, float* outVy) { |
| 111 | if (id == ACTIVE_POINTER_ID) { |
| 112 | id = mVelocityTracker.getActivePointerId(); |
| 113 | } |
| 114 | |
| 115 | float vx, vy; |
| 116 | if (id >= 0 && id <= MAX_POINTER_ID && mCalculatedIdBits.hasBit(id)) { |
| 117 | uint32_t index = mCalculatedIdBits.getIndexOfBit(id); |
| 118 | const Velocity& velocity = mCalculatedVelocity[index]; |
| 119 | vx = velocity.vx; |
| 120 | vy = velocity.vy; |
| 121 | } else { |
| 122 | vx = 0; |
| 123 | vy = 0; |
| 124 | } |
| 125 | |
| 126 | if (outVx) { |
| 127 | *outVx = vx; |
| 128 | } |
| 129 | if (outVy) { |
| 130 | *outVy = vy; |
| 131 | } |
| 132 | } |
| 133 | |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 134 | bool VelocityTrackerState::getEstimator(int32_t id, VelocityTracker::Estimator* outEstimator) { |
| 135 | return mVelocityTracker.getEstimator(id, outEstimator); |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 138 | |
| 139 | // --- JNI Methods --- |
| 140 | |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 141 | static jlong android_view_VelocityTracker_nativeInitialize(JNIEnv* env, jclass clazz, |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 142 | jstring strategyStr) { |
| 143 | if (strategyStr) { |
| 144 | ScopedUtfChars strategy(env, strategyStr); |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 145 | return reinterpret_cast<jlong>(new VelocityTrackerState(strategy.c_str())); |
Jeff Brown | 9eb7d86 | 2012-06-01 12:39:25 -0700 | [diff] [blame] | 146 | } |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 147 | return reinterpret_cast<jlong>(new VelocityTrackerState(NULL)); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 150 | static void android_view_VelocityTracker_nativeDispose(JNIEnv* env, jclass clazz, jlong ptr) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 151 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 152 | delete state; |
| 153 | } |
| 154 | |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 155 | static void android_view_VelocityTracker_nativeClear(JNIEnv* env, jclass clazz, jlong ptr) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 156 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 157 | state->clear(); |
| 158 | } |
| 159 | |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 160 | static void android_view_VelocityTracker_nativeAddMovement(JNIEnv* env, jclass clazz, jlong ptr, |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 161 | jobject eventObj) { |
| 162 | const MotionEvent* event = android_view_MotionEvent_getNativePtr(env, eventObj); |
| 163 | if (!event) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 164 | ALOGW("nativeAddMovement failed because MotionEvent was finalized."); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | |
| 168 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 169 | state->addMovement(event); |
| 170 | } |
| 171 | |
| 172 | static void android_view_VelocityTracker_nativeComputeCurrentVelocity(JNIEnv* env, jclass clazz, |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 173 | jlong ptr, jint units, jfloat maxVelocity) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 174 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 175 | state->computeCurrentVelocity(units, maxVelocity); |
| 176 | } |
| 177 | |
| 178 | static jfloat android_view_VelocityTracker_nativeGetXVelocity(JNIEnv* env, jclass clazz, |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 179 | jlong ptr, jint id) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 180 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 181 | float vx; |
| 182 | state->getVelocity(id, &vx, NULL); |
| 183 | return vx; |
| 184 | } |
| 185 | |
| 186 | static jfloat android_view_VelocityTracker_nativeGetYVelocity(JNIEnv* env, jclass clazz, |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 187 | jlong ptr, jint id) { |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 188 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 189 | float vy; |
| 190 | state->getVelocity(id, NULL, &vy); |
| 191 | return vy; |
| 192 | } |
| 193 | |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 194 | static jboolean android_view_VelocityTracker_nativeGetEstimator(JNIEnv* env, jclass clazz, |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 195 | jlong ptr, jint id, jobject outEstimatorObj) { |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 196 | VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr); |
| 197 | VelocityTracker::Estimator estimator; |
Jeff Brown | 85bd0d6 | 2012-05-13 15:30:42 -0700 | [diff] [blame] | 198 | bool result = state->getEstimator(id, &estimator); |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 199 | |
| 200 | jfloatArray xCoeffObj = jfloatArray(env->GetObjectField(outEstimatorObj, |
| 201 | gEstimatorClassInfo.xCoeff)); |
| 202 | jfloatArray yCoeffObj = jfloatArray(env->GetObjectField(outEstimatorObj, |
| 203 | gEstimatorClassInfo.yCoeff)); |
| 204 | |
| 205 | env->SetFloatArrayRegion(xCoeffObj, 0, VelocityTracker::Estimator::MAX_DEGREE + 1, |
| 206 | estimator.xCoeff); |
| 207 | env->SetFloatArrayRegion(yCoeffObj, 0, VelocityTracker::Estimator::MAX_DEGREE + 1, |
| 208 | estimator.yCoeff); |
| 209 | env->SetIntField(outEstimatorObj, gEstimatorClassInfo.degree, estimator.degree); |
| 210 | env->SetFloatField(outEstimatorObj, gEstimatorClassInfo.confidence, estimator.confidence); |
| 211 | return result; |
| 212 | } |
| 213 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 214 | |
| 215 | // --- JNI Registration --- |
| 216 | |
| 217 | static JNINativeMethod gVelocityTrackerMethods[] = { |
| 218 | /* name, signature, funcPtr */ |
| 219 | { "nativeInitialize", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 220 | "(Ljava/lang/String;)J", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 221 | (void*)android_view_VelocityTracker_nativeInitialize }, |
| 222 | { "nativeDispose", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 223 | "(J)V", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 224 | (void*)android_view_VelocityTracker_nativeDispose }, |
| 225 | { "nativeClear", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 226 | "(J)V", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 227 | (void*)android_view_VelocityTracker_nativeClear }, |
| 228 | { "nativeAddMovement", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 229 | "(JLandroid/view/MotionEvent;)V", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 230 | (void*)android_view_VelocityTracker_nativeAddMovement }, |
| 231 | { "nativeComputeCurrentVelocity", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 232 | "(JIF)V", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 233 | (void*)android_view_VelocityTracker_nativeComputeCurrentVelocity }, |
| 234 | { "nativeGetXVelocity", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 235 | "(JI)F", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 236 | (void*)android_view_VelocityTracker_nativeGetXVelocity }, |
| 237 | { "nativeGetYVelocity", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 238 | "(JI)F", |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 239 | (void*)android_view_VelocityTracker_nativeGetYVelocity }, |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 240 | { "nativeGetEstimator", |
Ashok Bhat | 96804bc | 2014-01-08 15:45:39 +0000 | [diff] [blame] | 241 | "(JILandroid/view/VelocityTracker$Estimator;)Z", |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 242 | (void*)android_view_VelocityTracker_nativeGetEstimator }, |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 243 | }; |
| 244 | |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 245 | #define FIND_CLASS(var, className) \ |
| 246 | var = env->FindClass(className); \ |
| 247 | LOG_FATAL_IF(! var, "Unable to find class " className); |
| 248 | |
| 249 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 250 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 251 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 252 | |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 253 | int register_android_view_VelocityTracker(JNIEnv* env) { |
| 254 | int res = jniRegisterNativeMethods(env, "android/view/VelocityTracker", |
| 255 | gVelocityTrackerMethods, NELEM(gVelocityTrackerMethods)); |
| 256 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
Jeff Brown | b59ab9f | 2011-09-14 10:53:18 -0700 | [diff] [blame] | 257 | |
| 258 | jclass clazz; |
| 259 | FIND_CLASS(clazz, "android/view/VelocityTracker$Estimator"); |
| 260 | |
| 261 | GET_FIELD_ID(gEstimatorClassInfo.xCoeff, clazz, |
| 262 | "xCoeff", "[F"); |
| 263 | GET_FIELD_ID(gEstimatorClassInfo.yCoeff, clazz, |
| 264 | "yCoeff", "[F"); |
| 265 | GET_FIELD_ID(gEstimatorClassInfo.degree, clazz, |
| 266 | "degree", "I"); |
| 267 | GET_FIELD_ID(gEstimatorClassInfo.confidence, clazz, |
| 268 | "confidence", "F"); |
Jeff Brown | 2ed2462 | 2011-03-14 19:39:54 -0700 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | } // namespace android |