@@ -29,6 +29,14 @@ namespace android {
2929// Special constant to request the velocity of the active pointer.
3030static const int ACTIVE_POINTER_ID = -1 ;
3131
32+ static struct {
33+ jfieldID xCoeff;
34+ jfieldID yCoeff;
35+ jfieldID degree;
36+ jfieldID confidence;
37+ } gEstimatorClassInfo ;
38+
39+
3240// --- VelocityTrackerState ---
3341
3442class VelocityTrackerState {
@@ -39,6 +47,8 @@ class VelocityTrackerState {
3947 void addMovement (const MotionEvent* event);
4048 void computeCurrentVelocity (int32_t units, float maxVelocity);
4149 void getVelocity (int32_t id, float * outVx, float * outVy);
50+ bool getEstimator (int32_t id, uint32_t degree, nsecs_t horizon,
51+ VelocityTracker::Estimator* outEstimator);
4252
4353private:
4454 struct Velocity {
@@ -118,6 +128,11 @@ void VelocityTrackerState::getVelocity(int32_t id, float* outVx, float* outVy) {
118128 }
119129}
120130
131+ bool VelocityTrackerState::getEstimator (int32_t id, uint32_t degree, nsecs_t horizon,
132+ VelocityTracker::Estimator* outEstimator) {
133+ return mVelocityTracker .getEstimator (id, degree, horizon, outEstimator);
134+ }
135+
121136
122137// --- JNI Methods ---
123138
@@ -169,6 +184,30 @@ static jfloat android_view_VelocityTracker_nativeGetYVelocity(JNIEnv* env, jclas
169184 return vy;
170185}
171186
187+ static jboolean android_view_VelocityTracker_nativeGetEstimator (JNIEnv* env, jclass clazz,
188+ jint ptr, jint id, jint degree, jint horizonMillis, jobject outEstimatorObj) {
189+ VelocityTrackerState* state = reinterpret_cast <VelocityTrackerState*>(ptr);
190+ VelocityTracker::Estimator estimator;
191+ bool result = state->getEstimator (id,
192+ degree < 0 ? VelocityTracker::DEFAULT_DEGREE : uint32_t (degree),
193+ horizonMillis < 0 ? VelocityTracker::DEFAULT_HORIZON :
194+ nsecs_t (horizonMillis) * 1000000L ,
195+ &estimator);
196+
197+ jfloatArray xCoeffObj = jfloatArray (env->GetObjectField (outEstimatorObj,
198+ gEstimatorClassInfo .xCoeff ));
199+ jfloatArray yCoeffObj = jfloatArray (env->GetObjectField (outEstimatorObj,
200+ gEstimatorClassInfo .yCoeff ));
201+
202+ env->SetFloatArrayRegion (xCoeffObj, 0 , VelocityTracker::Estimator::MAX_DEGREE + 1 ,
203+ estimator.xCoeff );
204+ env->SetFloatArrayRegion (yCoeffObj, 0 , VelocityTracker::Estimator::MAX_DEGREE + 1 ,
205+ estimator.yCoeff );
206+ env->SetIntField (outEstimatorObj, gEstimatorClassInfo .degree , estimator.degree );
207+ env->SetFloatField (outEstimatorObj, gEstimatorClassInfo .confidence , estimator.confidence );
208+ return result;
209+ }
210+
172211
173212// --- JNI Registration ---
174213
@@ -195,12 +234,35 @@ static JNINativeMethod gVelocityTrackerMethods[] = {
195234 { " nativeGetYVelocity" ,
196235 " (II)F" ,
197236 (void *)android_view_VelocityTracker_nativeGetYVelocity },
237+ { " nativeGetEstimator" ,
238+ " (IIIILandroid/view/VelocityTracker$Estimator;)Z" ,
239+ (void *)android_view_VelocityTracker_nativeGetEstimator },
198240};
199241
242+ #define FIND_CLASS (var, className ) \
243+ var = env->FindClass (className); \
244+ LOG_FATAL_IF (! var, " Unable to find class " className);
245+
246+ #define GET_FIELD_ID (var, clazz, fieldName, fieldDescriptor ) \
247+ var = env->GetFieldID (clazz, fieldName, fieldDescriptor); \
248+ LOG_FATAL_IF (! var, " Unable to find field " fieldName);
249+
200250int register_android_view_VelocityTracker (JNIEnv* env) {
201251 int res = jniRegisterNativeMethods (env, " android/view/VelocityTracker" ,
202252 gVelocityTrackerMethods , NELEM (gVelocityTrackerMethods ));
203253 LOG_FATAL_IF (res < 0 , " Unable to register native methods." );
254+
255+ jclass clazz;
256+ FIND_CLASS (clazz, " android/view/VelocityTracker$Estimator" );
257+
258+ GET_FIELD_ID (gEstimatorClassInfo .xCoeff , clazz,
259+ " xCoeff" , " [F" );
260+ GET_FIELD_ID (gEstimatorClassInfo .yCoeff , clazz,
261+ " yCoeff" , " [F" );
262+ GET_FIELD_ID (gEstimatorClassInfo .degree , clazz,
263+ " degree" , " I" );
264+ GET_FIELD_ID (gEstimatorClassInfo .confidence , clazz,
265+ " confidence" , " F" );
204266 return 0 ;
205267}
206268
0 commit comments