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 <android_runtime/AndroidRuntime.h>
20 #include <cutils/properties.h>
21 #include <input/Input.h>
22 #include <input/VelocityTracker.h>
23 #include <nativehelper/JNIHelp.h>
24 #include <nativehelper/ScopedUtfChars.h>
25 #include <utils/Log.h>
26 #include "android_view_MotionEvent.h"
27 #include "core_jni_helpers.h"
28 
29 namespace android {
30 
31 // Special constant to request the velocity of the active pointer.
32 static const int ACTIVE_POINTER_ID = -1;
33 
34 // --- VelocityTrackerState ---
35 
36 class VelocityTrackerState {
37 public:
38     explicit VelocityTrackerState(const VelocityTracker::Strategy strategy);
39 
40     void clear();
41     void addMovement(const MotionEvent* event);
42     // TODO(b/32830165): consider supporting an overload that supports computing velocity only for
43     // a subset of the supported axes.
44     void computeCurrentVelocity(int32_t units, float maxVelocity);
45     float getVelocity(int32_t axis, int32_t id);
46 
47 private:
48     VelocityTracker mVelocityTracker;
49     VelocityTracker::ComputedVelocity mComputedVelocity;
50 };
51 
VelocityTrackerState(const VelocityTracker::Strategy strategy)52 VelocityTrackerState::VelocityTrackerState(const VelocityTracker::Strategy strategy)
53       : mVelocityTracker(strategy) {}
54 
clear()55 void VelocityTrackerState::clear() {
56     mVelocityTracker.clear();
57 }
58 
addMovement(const MotionEvent * event)59 void VelocityTrackerState::addMovement(const MotionEvent* event) {
60     mVelocityTracker.addMovement(event);
61 }
62 
computeCurrentVelocity(int32_t units,float maxVelocity)63 void VelocityTrackerState::computeCurrentVelocity(int32_t units, float maxVelocity) {
64     mComputedVelocity = mVelocityTracker.getComputedVelocity(units, maxVelocity);
65 }
66 
getVelocity(int32_t axis,int32_t id)67 float VelocityTrackerState::getVelocity(int32_t axis, int32_t id) {
68     if (id == ACTIVE_POINTER_ID) {
69         id = mVelocityTracker.getActivePointerId();
70     }
71 
72     return mComputedVelocity.getVelocity(axis, id).value_or(0);
73 }
74 
75 // Return a strategy enum from integer value.
getStrategyFromInt(const int32_t strategy)76 inline static VelocityTracker::Strategy getStrategyFromInt(const int32_t strategy) {
77     if (strategy < static_cast<int32_t>(VelocityTracker::Strategy::MIN) ||
78         strategy > static_cast<int32_t>(VelocityTracker::Strategy::MAX)) {
79         return VelocityTracker::Strategy::DEFAULT;
80     }
81     return static_cast<VelocityTracker::Strategy>(strategy);
82 }
83 
84 // --- JNI Methods ---
85 
android_view_VelocityTracker_nativeInitialize(JNIEnv * env,jclass clazz,jint strategy)86 static jlong android_view_VelocityTracker_nativeInitialize(JNIEnv* env, jclass clazz,
87                                                            jint strategy) {
88     return reinterpret_cast<jlong>(new VelocityTrackerState(getStrategyFromInt(strategy)));
89 }
90 
android_view_VelocityTracker_nativeDispose(JNIEnv * env,jclass clazz,jlong ptr)91 static void android_view_VelocityTracker_nativeDispose(JNIEnv* env, jclass clazz, jlong ptr) {
92     VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
93     delete state;
94 }
95 
android_view_VelocityTracker_nativeClear(JNIEnv * env,jclass clazz,jlong ptr)96 static void android_view_VelocityTracker_nativeClear(JNIEnv* env, jclass clazz, jlong ptr) {
97     VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
98     state->clear();
99 }
100 
android_view_VelocityTracker_nativeAddMovement(JNIEnv * env,jclass clazz,jlong ptr,jobject eventObj)101 static void android_view_VelocityTracker_nativeAddMovement(JNIEnv* env, jclass clazz, jlong ptr,
102         jobject eventObj) {
103     const MotionEvent* event = android_view_MotionEvent_getNativePtr(env, eventObj);
104     if (!event) {
105         ALOGW("nativeAddMovement failed because MotionEvent was finalized.");
106         return;
107     }
108 
109     VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
110     state->addMovement(event);
111 }
112 
android_view_VelocityTracker_nativeComputeCurrentVelocity(JNIEnv * env,jclass clazz,jlong ptr,jint units,jfloat maxVelocity)113 static void android_view_VelocityTracker_nativeComputeCurrentVelocity(JNIEnv* env, jclass clazz,
114         jlong ptr, jint units, jfloat maxVelocity) {
115     VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
116     state->computeCurrentVelocity(units, maxVelocity);
117 }
118 
android_view_VelocityTracker_nativeGetVelocity(JNIEnv * env,jclass clazz,jlong ptr,jint axis,jint id)119 static jfloat android_view_VelocityTracker_nativeGetVelocity(JNIEnv* env, jclass clazz, jlong ptr,
120                                                              jint axis, jint id) {
121     VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
122     return state->getVelocity(axis, id);
123 }
124 
android_view_VelocityTracker_nativeIsAxisSupported(JNIEnv * env,jclass clazz,jint axis)125 static jboolean android_view_VelocityTracker_nativeIsAxisSupported(JNIEnv* env, jclass clazz,
126                                                                    jint axis) {
127     return VelocityTracker::isAxisSupported(axis);
128 }
129 
130 // --- JNI Registration ---
131 
132 static const JNINativeMethod gVelocityTrackerMethods[] = {
133         /* name, signature, funcPtr */
134         {"nativeInitialize", "(I)J", (void*)android_view_VelocityTracker_nativeInitialize},
135         {"nativeDispose", "(J)V", (void*)android_view_VelocityTracker_nativeDispose},
136         {"nativeClear", "(J)V", (void*)android_view_VelocityTracker_nativeClear},
137         {"nativeAddMovement", "(JLandroid/view/MotionEvent;)V",
138          (void*)android_view_VelocityTracker_nativeAddMovement},
139         {"nativeComputeCurrentVelocity", "(JIF)V",
140          (void*)android_view_VelocityTracker_nativeComputeCurrentVelocity},
141         {"nativeGetVelocity", "(JII)F", (void*)android_view_VelocityTracker_nativeGetVelocity},
142         {"nativeIsAxisSupported", "(I)Z",
143          (void*)android_view_VelocityTracker_nativeIsAxisSupported},
144 };
145 
register_android_view_VelocityTracker(JNIEnv * env)146 int register_android_view_VelocityTracker(JNIEnv* env) {
147     return RegisterMethodsOrDie(env, "android/view/VelocityTracker", gVelocityTrackerMethods,
148                                 NELEM(gVelocityTrackerMethods));
149 }
150 
151 } // namespace android
152