blob: 0245d38144cbb4b100503e5bca3cb652390b663e [file] [log] [blame]
Jeff Brown46b9ac02010-04-22 18:58:52 -07001/*
2 * Copyright (C) 2010 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 "MotionEvent-JNI"
18
19#include "JNIHelp.h"
20
Jeff Brown48a3d982013-07-15 17:31:32 -070021#include <SkMatrix.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070022#include <android_runtime/AndroidRuntime.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070023#include <android_runtime/Log.h>
Jeff Brown46b9ac02010-04-22 18:58:52 -070024#include <utils/Log.h>
Jeff Brown9d3b1a42013-07-01 19:07:15 -070025#include <input/Input.h>
Michael Wright337d9d22014-04-22 15:03:48 -070026#include <ScopedUtfChars.h>
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080027#include "android_os_Parcel.h"
Jeff Brown46b9ac02010-04-22 18:58:52 -070028#include "android_view_MotionEvent.h"
Jeff Brown91c69ab2011-02-14 17:03:18 -080029#include "android_util_Binder.h"
Jeff Brown20e987b2010-08-23 12:01:02 -070030#include "android/graphics/Matrix.h"
31
Andreas Gampe987f79f2014-11-18 17:29:46 -080032#include "core_jni_helpers.h"
33
Jeff Brown46b9ac02010-04-22 18:58:52 -070034namespace android {
35
36// ----------------------------------------------------------------------------
37
38static struct {
39 jclass clazz;
40
41 jmethodID obtain;
42 jmethodID recycle;
43
Jeff Brown91c69ab2011-02-14 17:03:18 -080044 jfieldID mNativePtr;
Jeff Brown46b9ac02010-04-22 18:58:52 -070045} gMotionEventClassInfo;
46
Jeff Brown91c69ab2011-02-14 17:03:18 -080047static struct {
Jeff Brown91c69ab2011-02-14 17:03:18 -080048 jfieldID mPackedAxisBits;
49 jfieldID mPackedAxisValues;
50 jfieldID x;
51 jfieldID y;
52 jfieldID pressure;
53 jfieldID size;
54 jfieldID touchMajor;
55 jfieldID touchMinor;
56 jfieldID toolMajor;
57 jfieldID toolMinor;
58 jfieldID orientation;
59} gPointerCoordsClassInfo;
60
Jeff Brownfe9f8ab2011-05-06 18:20:01 -070061static struct {
62 jfieldID id;
63 jfieldID toolType;
64} gPointerPropertiesClassInfo;
65
Jeff Brown46b9ac02010-04-22 18:58:52 -070066// ----------------------------------------------------------------------------
67
Jeff Brown2ed24622011-03-14 19:39:54 -070068MotionEvent* android_view_MotionEvent_getNativePtr(JNIEnv* env, jobject eventObj) {
69 if (!eventObj) {
70 return NULL;
71 }
Jeff Brown91c69ab2011-02-14 17:03:18 -080072 return reinterpret_cast<MotionEvent*>(
Ashok Bhat99a1ef22014-01-08 14:45:08 +000073 env->GetLongField(eventObj, gMotionEventClassInfo.mNativePtr));
Jeff Brown91c69ab2011-02-14 17:03:18 -080074}
Jeff Brown46b9ac02010-04-22 18:58:52 -070075
Jeff Brown91c69ab2011-02-14 17:03:18 -080076static void android_view_MotionEvent_setNativePtr(JNIEnv* env, jobject eventObj,
77 MotionEvent* event) {
Ashok Bhat99a1ef22014-01-08 14:45:08 +000078 env->SetLongField(eventObj, gMotionEventClassInfo.mNativePtr,
79 reinterpret_cast<jlong>(event));
Jeff Brown91c69ab2011-02-14 17:03:18 -080080}
81
Jeff Brown2ed24622011-03-14 19:39:54 -070082jobject android_view_MotionEvent_obtainAsCopy(JNIEnv* env, const MotionEvent* event) {
Jeff Brown46b9ac02010-04-22 18:58:52 -070083 jobject eventObj = env->CallStaticObjectMethod(gMotionEventClassInfo.clazz,
Jeff Brown91c69ab2011-02-14 17:03:18 -080084 gMotionEventClassInfo.obtain);
Jeff Brown2ed24622011-03-14 19:39:54 -070085 if (env->ExceptionCheck() || !eventObj) {
Steve Block3762c312012-01-06 19:20:56 +000086 ALOGE("An exception occurred while obtaining a motion event.");
Jeff Brown46b9ac02010-04-22 18:58:52 -070087 LOGE_EX(env);
88 env->ExceptionClear();
89 return NULL;
90 }
91
Jeff Brown91c69ab2011-02-14 17:03:18 -080092 MotionEvent* destEvent = android_view_MotionEvent_getNativePtr(env, eventObj);
93 if (!destEvent) {
94 destEvent = new MotionEvent();
95 android_view_MotionEvent_setNativePtr(env, eventObj, destEvent);
Jeff Brown46b9ac02010-04-22 18:58:52 -070096 }
97
Jeff Brown91c69ab2011-02-14 17:03:18 -080098 destEvent->copyFrom(event, true);
Jeff Brown46b9ac02010-04-22 18:58:52 -070099 return eventObj;
100}
101
Jeff Brown1f245102010-11-18 20:53:46 -0800102status_t android_view_MotionEvent_recycle(JNIEnv* env, jobject eventObj) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700103 env->CallVoidMethod(eventObj, gMotionEventClassInfo.recycle);
104 if (env->ExceptionCheck()) {
Steve Block8564c8d2012-01-05 23:22:43 +0000105 ALOGW("An exception occurred while recycling a motion event.");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700106 LOGW_EX(env);
107 env->ExceptionClear();
Jeff Brown1f245102010-11-18 20:53:46 -0800108 return UNKNOWN_ERROR;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700109 }
Jeff Brown1f245102010-11-18 20:53:46 -0800110 return OK;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700111}
112
Jeff Brown91c69ab2011-02-14 17:03:18 -0800113// ----------------------------------------------------------------------------
Jeff Brown20e987b2010-08-23 12:01:02 -0700114
Jeff Brown91c69ab2011-02-14 17:03:18 -0800115static const jint HISTORY_CURRENT = -0x80000000;
116
117static bool validatePointerCount(JNIEnv* env, jint pointerCount) {
118 if (pointerCount < 1) {
119 jniThrowException(env, "java/lang/IllegalArgumentException",
120 "pointerCount must be at least 1");
121 return false;
Jeff Brown20e987b2010-08-23 12:01:02 -0700122 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800123 return true;
Jeff Brown20e987b2010-08-23 12:01:02 -0700124}
125
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700126static bool validatePointerPropertiesArray(JNIEnv* env, jobjectArray pointerPropertiesObjArray,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800127 size_t pointerCount) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700128 if (!pointerPropertiesObjArray) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800129 jniThrowException(env, "java/lang/IllegalArgumentException",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700130 "pointerProperties array must not be null");
Jeff Brown91c69ab2011-02-14 17:03:18 -0800131 return false;
132 }
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700133 size_t length = size_t(env->GetArrayLength(pointerPropertiesObjArray));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800134 if (length < pointerCount) {
135 jniThrowException(env, "java/lang/IllegalArgumentException",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700136 "pointerProperties array must be large enough to hold all pointers");
Jeff Brown91c69ab2011-02-14 17:03:18 -0800137 return false;
138 }
139 return true;
140}
Jeff Brown20e987b2010-08-23 12:01:02 -0700141
Jeff Brown91c69ab2011-02-14 17:03:18 -0800142static bool validatePointerCoordsObjArray(JNIEnv* env, jobjectArray pointerCoordsObjArray,
143 size_t pointerCount) {
144 if (!pointerCoordsObjArray) {
145 jniThrowException(env, "java/lang/IllegalArgumentException",
146 "pointerCoords array must not be null");
147 return false;
148 }
149 size_t length = size_t(env->GetArrayLength(pointerCoordsObjArray));
150 if (length < pointerCount) {
151 jniThrowException(env, "java/lang/IllegalArgumentException",
152 "pointerCoords array must be large enough to hold all pointers");
153 return false;
154 }
155 return true;
156}
Jeff Brown20e987b2010-08-23 12:01:02 -0700157
Jeff Brown91c69ab2011-02-14 17:03:18 -0800158static bool validatePointerIndex(JNIEnv* env, jint pointerIndex, size_t pointerCount) {
159 if (pointerIndex < 0 || size_t(pointerIndex) >= pointerCount) {
160 jniThrowException(env, "java/lang/IllegalArgumentException",
161 "pointerIndex out of range");
162 return false;
163 }
164 return true;
165}
Jeff Brown20e987b2010-08-23 12:01:02 -0700166
Jeff Brown91c69ab2011-02-14 17:03:18 -0800167static bool validateHistoryPos(JNIEnv* env, jint historyPos, size_t historySize) {
168 if (historyPos < 0 || size_t(historyPos) >= historySize) {
169 jniThrowException(env, "java/lang/IllegalArgumentException",
170 "historyPos out of range");
171 return false;
172 }
173 return true;
174}
Jeff Brown20e987b2010-08-23 12:01:02 -0700175
Jeff Brown91c69ab2011-02-14 17:03:18 -0800176static bool validatePointerCoords(JNIEnv* env, jobject pointerCoordsObj) {
177 if (!pointerCoordsObj) {
178 jniThrowException(env, "java/lang/IllegalArgumentException",
179 "pointerCoords must not be null");
180 return false;
181 }
182 return true;
183}
184
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700185static bool validatePointerProperties(JNIEnv* env, jobject pointerPropertiesObj) {
186 if (!pointerPropertiesObj) {
187 jniThrowException(env, "java/lang/IllegalArgumentException",
188 "pointerProperties must not be null");
189 return false;
190 }
191 return true;
192}
193
Jeff Brown91c69ab2011-02-14 17:03:18 -0800194static void pointerCoordsToNative(JNIEnv* env, jobject pointerCoordsObj,
195 float xOffset, float yOffset, PointerCoords* outRawPointerCoords) {
196 outRawPointerCoords->clear();
Jeff Brownebbd5d12011-02-17 13:01:34 -0800197 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_X,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800198 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.x) - xOffset);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800199 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_Y,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800200 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.y) - yOffset);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800201 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_PRESSURE,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800202 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.pressure));
Jeff Brownebbd5d12011-02-17 13:01:34 -0800203 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_SIZE,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800204 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.size));
Jeff Brownebbd5d12011-02-17 13:01:34 -0800205 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800206 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.touchMajor));
Jeff Brownebbd5d12011-02-17 13:01:34 -0800207 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800208 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.touchMinor));
Jeff Brownebbd5d12011-02-17 13:01:34 -0800209 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800210 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.toolMajor));
Jeff Brownebbd5d12011-02-17 13:01:34 -0800211 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800212 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.toolMinor));
Jeff Brownebbd5d12011-02-17 13:01:34 -0800213 outRawPointerCoords->setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800214 env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.orientation));
215
Michael Wrightcbf14862014-05-19 15:42:01 -0700216 BitSet64 bits =
217 BitSet64(env->GetLongField(pointerCoordsObj, gPointerCoordsClassInfo.mPackedAxisBits));
218 if (!bits.isEmpty()) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800219 jfloatArray valuesArray = jfloatArray(env->GetObjectField(pointerCoordsObj,
220 gPointerCoordsClassInfo.mPackedAxisValues));
221 if (valuesArray) {
222 jfloat* values = static_cast<jfloat*>(
223 env->GetPrimitiveArrayCritical(valuesArray, NULL));
224
225 uint32_t index = 0;
226 do {
Michael Wrightcbf14862014-05-19 15:42:01 -0700227 uint32_t axis = bits.clearFirstMarkedBit();
Jeff Brown91c69ab2011-02-14 17:03:18 -0800228 outRawPointerCoords->setAxisValue(axis, values[index++]);
Michael Wrightcbf14862014-05-19 15:42:01 -0700229 } while (!bits.isEmpty());
Jeff Brown91c69ab2011-02-14 17:03:18 -0800230
231 env->ReleasePrimitiveArrayCritical(valuesArray, values, JNI_ABORT);
232 env->DeleteLocalRef(valuesArray);
233 }
234 }
235}
236
237static jfloatArray obtainPackedAxisValuesArray(JNIEnv* env, uint32_t minSize,
238 jobject outPointerCoordsObj) {
239 jfloatArray outValuesArray = jfloatArray(env->GetObjectField(outPointerCoordsObj,
240 gPointerCoordsClassInfo.mPackedAxisValues));
241 if (outValuesArray) {
242 uint32_t size = env->GetArrayLength(outValuesArray);
243 if (minSize <= size) {
244 return outValuesArray;
245 }
246 env->DeleteLocalRef(outValuesArray);
247 }
248 uint32_t size = 8;
249 while (size < minSize) {
250 size *= 2;
251 }
252 outValuesArray = env->NewFloatArray(size);
253 env->SetObjectField(outPointerCoordsObj,
254 gPointerCoordsClassInfo.mPackedAxisValues, outValuesArray);
255 return outValuesArray;
256}
257
258static void pointerCoordsFromNative(JNIEnv* env, const PointerCoords* rawPointerCoords,
259 float xOffset, float yOffset, jobject outPointerCoordsObj) {
260 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.x,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800261 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_X) + xOffset);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800262 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.y,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800263 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_Y) + yOffset);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800264 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.pressure,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800265 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800266 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.size,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800267 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_SIZE));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800268 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.touchMajor,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800269 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800270 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.touchMinor,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800271 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800272 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.toolMajor,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800273 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800274 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.toolMinor,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800275 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800276 env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.orientation,
Jeff Brownebbd5d12011-02-17 13:01:34 -0800277 rawPointerCoords->getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800278
Jeff Brown6f2fba42011-02-19 01:08:02 -0800279 uint64_t outBits = 0;
Michael Wrightcbf14862014-05-19 15:42:01 -0700280 BitSet64 bits = BitSet64(rawPointerCoords->bits);
281 bits.clearBit(AMOTION_EVENT_AXIS_X);
282 bits.clearBit(AMOTION_EVENT_AXIS_Y);
283 bits.clearBit(AMOTION_EVENT_AXIS_PRESSURE);
284 bits.clearBit(AMOTION_EVENT_AXIS_SIZE);
285 bits.clearBit(AMOTION_EVENT_AXIS_TOUCH_MAJOR);
286 bits.clearBit(AMOTION_EVENT_AXIS_TOUCH_MINOR);
287 bits.clearBit(AMOTION_EVENT_AXIS_TOOL_MAJOR);
288 bits.clearBit(AMOTION_EVENT_AXIS_TOOL_MINOR);
289 bits.clearBit(AMOTION_EVENT_AXIS_ORIENTATION);
290 if (!bits.isEmpty()) {
291 uint32_t packedAxesCount = bits.count();
Jeff Brown91c69ab2011-02-14 17:03:18 -0800292 jfloatArray outValuesArray = obtainPackedAxisValuesArray(env, packedAxesCount,
293 outPointerCoordsObj);
294 if (!outValuesArray) {
295 return; // OOM
Jeff Brown20e987b2010-08-23 12:01:02 -0700296 }
297
Jeff Brown91c69ab2011-02-14 17:03:18 -0800298 jfloat* outValues = static_cast<jfloat*>(env->GetPrimitiveArrayCritical(
299 outValuesArray, NULL));
Jeff Brown20e987b2010-08-23 12:01:02 -0700300
Jeff Brown91c69ab2011-02-14 17:03:18 -0800301 uint32_t index = 0;
302 do {
Michael Wrightcbf14862014-05-19 15:42:01 -0700303 uint32_t axis = bits.clearFirstMarkedBit();
304 outBits |= BitSet64::valueForBit(axis);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800305 outValues[index++] = rawPointerCoords->getAxisValue(axis);
Michael Wrightcbf14862014-05-19 15:42:01 -0700306 } while (!bits.isEmpty());
Jeff Brown91c69ab2011-02-14 17:03:18 -0800307
308 env->ReleasePrimitiveArrayCritical(outValuesArray, outValues, 0);
309 env->DeleteLocalRef(outValuesArray);
310 }
Jeff Brown6f2fba42011-02-19 01:08:02 -0800311 env->SetLongField(outPointerCoordsObj, gPointerCoordsClassInfo.mPackedAxisBits, outBits);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800312}
313
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700314static void pointerPropertiesToNative(JNIEnv* env, jobject pointerPropertiesObj,
315 PointerProperties* outPointerProperties) {
316 outPointerProperties->clear();
317 outPointerProperties->id = env->GetIntField(pointerPropertiesObj,
318 gPointerPropertiesClassInfo.id);
319 outPointerProperties->toolType = env->GetIntField(pointerPropertiesObj,
320 gPointerPropertiesClassInfo.toolType);
321}
322
323static void pointerPropertiesFromNative(JNIEnv* env, const PointerProperties* pointerProperties,
324 jobject outPointerPropertiesObj) {
325 env->SetIntField(outPointerPropertiesObj, gPointerPropertiesClassInfo.id,
326 pointerProperties->id);
327 env->SetIntField(outPointerPropertiesObj, gPointerPropertiesClassInfo.toolType,
328 pointerProperties->toolType);
329}
330
Jeff Brown91c69ab2011-02-14 17:03:18 -0800331
332// ----------------------------------------------------------------------------
333
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000334static jlong android_view_MotionEvent_nativeInitialize(JNIEnv* env, jclass clazz,
335 jlong nativePtr,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700336 jint deviceId, jint source, jint action, jint flags, jint edgeFlags,
337 jint metaState, jint buttonState,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800338 jfloat xOffset, jfloat yOffset, jfloat xPrecision, jfloat yPrecision,
339 jlong downTimeNanos, jlong eventTimeNanos,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700340 jint pointerCount, jobjectArray pointerPropertiesObjArray,
341 jobjectArray pointerCoordsObjArray) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800342 if (!validatePointerCount(env, pointerCount)
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700343 || !validatePointerPropertiesArray(env, pointerPropertiesObjArray, pointerCount)
Jeff Brown91c69ab2011-02-14 17:03:18 -0800344 || !validatePointerCoordsObjArray(env, pointerCoordsObjArray, pointerCount)) {
345 return 0;
Jeff Brown20e987b2010-08-23 12:01:02 -0700346 }
347
Jeff Brown91c69ab2011-02-14 17:03:18 -0800348 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
349 if (!event) {
350 event = new MotionEvent();
351 }
Jeff Brown20e987b2010-08-23 12:01:02 -0700352
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700353 PointerProperties pointerProperties[pointerCount];
Jeff Brown91c69ab2011-02-14 17:03:18 -0800354 PointerCoords rawPointerCoords[pointerCount];
Jeff Brown20e987b2010-08-23 12:01:02 -0700355
Jeff Brown91c69ab2011-02-14 17:03:18 -0800356 for (jint i = 0; i < pointerCount; i++) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700357 jobject pointerPropertiesObj = env->GetObjectArrayElement(pointerPropertiesObjArray, i);
358 if (!pointerPropertiesObj) {
359 goto Error;
360 }
361 pointerPropertiesToNative(env, pointerPropertiesObj, &pointerProperties[i]);
362 env->DeleteLocalRef(pointerPropertiesObj);
363
Jeff Brown91c69ab2011-02-14 17:03:18 -0800364 jobject pointerCoordsObj = env->GetObjectArrayElement(pointerCoordsObjArray, i);
365 if (!pointerCoordsObj) {
366 jniThrowNullPointerException(env, "pointerCoords");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700367 goto Error;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800368 }
369 pointerCoordsToNative(env, pointerCoordsObj, xOffset, yOffset, &rawPointerCoords[i]);
370 env->DeleteLocalRef(pointerCoordsObj);
371 }
372
Michael Wright7a629cc2015-06-10 14:57:33 +0100373 event->initialize(deviceId, source, action, 0, flags, edgeFlags, metaState, buttonState,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800374 xOffset, yOffset, xPrecision, yPrecision,
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700375 downTimeNanos, eventTimeNanos, pointerCount, pointerProperties, rawPointerCoords);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800376
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000377 return reinterpret_cast<jlong>(event);
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700378
379Error:
380 if (!nativePtr) {
381 delete event;
382 }
383 return 0;
Jeff Brown91c69ab2011-02-14 17:03:18 -0800384}
385
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000386static jlong android_view_MotionEvent_nativeCopy(JNIEnv* env, jclass clazz,
387 jlong destNativePtr, jlong sourceNativePtr, jboolean keepHistory) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800388 MotionEvent* destEvent = reinterpret_cast<MotionEvent*>(destNativePtr);
389 if (!destEvent) {
390 destEvent = new MotionEvent();
391 }
392 MotionEvent* sourceEvent = reinterpret_cast<MotionEvent*>(sourceNativePtr);
393 destEvent->copyFrom(sourceEvent, keepHistory);
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000394 return reinterpret_cast<jlong>(destEvent);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800395}
396
397static void android_view_MotionEvent_nativeDispose(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000398 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800399 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
400 delete event;
401}
402
403static void android_view_MotionEvent_nativeAddBatch(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000404 jlong nativePtr, jlong eventTimeNanos, jobjectArray pointerCoordsObjArray,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800405 jint metaState) {
406 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
407 size_t pointerCount = event->getPointerCount();
408 if (!validatePointerCoordsObjArray(env, pointerCoordsObjArray, pointerCount)) {
409 return;
410 }
411
412 PointerCoords rawPointerCoords[pointerCount];
413
414 for (size_t i = 0; i < pointerCount; i++) {
415 jobject pointerCoordsObj = env->GetObjectArrayElement(pointerCoordsObjArray, i);
416 if (!pointerCoordsObj) {
417 jniThrowNullPointerException(env, "pointerCoords");
418 return;
419 }
420 pointerCoordsToNative(env, pointerCoordsObj,
421 event->getXOffset(), event->getYOffset(), &rawPointerCoords[i]);
422 env->DeleteLocalRef(pointerCoordsObj);
423 }
424
425 event->addSample(eventTimeNanos, rawPointerCoords);
426 event->setMetaState(event->getMetaState() | metaState);
427}
428
429static jint android_view_MotionEvent_nativeGetDeviceId(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000430 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800431 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
432 return event->getDeviceId();
433}
434
435static jint android_view_MotionEvent_nativeGetSource(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000436 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800437 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
438 return event->getSource();
439}
440
441static void android_view_MotionEvent_nativeSetSource(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000442 jlong nativePtr, jint source) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800443 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
444 event->setSource(source);
445}
446
447static jint android_view_MotionEvent_nativeGetAction(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000448 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800449 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
450 return event->getAction();
451}
452
453static void android_view_MotionEvent_nativeSetAction(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000454 jlong nativePtr, jint action) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800455 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
456 event->setAction(action);
457}
458
Michael Wright5bd69e62015-05-14 14:48:08 +0100459static int android_view_MotionEvent_nativeGetActionButton(JNIEnv* env, jclass clazz,
460 jlong nativePtr) {
461 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
462 return event->getActionButton();
463}
464
Michael Wright6b819b42015-06-17 21:06:03 +0100465static void android_view_MotionEvent_nativeSetActionButton(JNIEnv* env, jclass clazz,
466 jlong nativePtr, jint button) {
467 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
468 event->setActionButton(button);
469}
470
Jeff Brown56194eb2011-03-02 19:23:13 -0800471static jboolean android_view_MotionEvent_nativeIsTouchEvent(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000472 jlong nativePtr) {
Jeff Brown56194eb2011-03-02 19:23:13 -0800473 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
474 return event->isTouchEvent();
475}
476
Jeff Brown91c69ab2011-02-14 17:03:18 -0800477static jint android_view_MotionEvent_nativeGetFlags(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000478 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800479 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
480 return event->getFlags();
481}
482
Jeff Brown21bc5c92011-02-28 18:27:14 -0800483static void android_view_MotionEvent_nativeSetFlags(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000484 jlong nativePtr, jint flags) {
Jeff Brown21bc5c92011-02-28 18:27:14 -0800485 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
486 event->setFlags(flags);
487}
488
Jeff Brown91c69ab2011-02-14 17:03:18 -0800489static jint android_view_MotionEvent_nativeGetEdgeFlags(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000490 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800491 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
492 return event->getEdgeFlags();
493}
494
495static void android_view_MotionEvent_nativeSetEdgeFlags(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000496 jlong nativePtr, jint edgeFlags) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800497 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
498 event->setEdgeFlags(edgeFlags);
499}
500
501static jint android_view_MotionEvent_nativeGetMetaState(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000502 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800503 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
504 return event->getMetaState();
505}
506
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700507static jint android_view_MotionEvent_nativeGetButtonState(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000508 jlong nativePtr) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700509 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
510 return event->getButtonState();
511}
512
Michael Wright5bd69e62015-05-14 14:48:08 +0100513static void android_view_MotionEvent_nativeSetButtonState(JNIEnv* env, jclass clazz,
514 jlong nativePtr, jint buttonState) {
515 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
516 event->setButtonState(buttonState);
517}
518
Jeff Brown91c69ab2011-02-14 17:03:18 -0800519static void android_view_MotionEvent_nativeOffsetLocation(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000520 jlong nativePtr, jfloat deltaX, jfloat deltaY) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800521 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
522 return event->offsetLocation(deltaX, deltaY);
523}
524
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700525static jfloat android_view_MotionEvent_nativeGetXOffset(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000526 jlong nativePtr) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700527 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
528 return event->getXOffset();
529}
530
531static jfloat android_view_MotionEvent_nativeGetYOffset(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000532 jlong nativePtr) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700533 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
534 return event->getYOffset();
535}
536
Jeff Brown91c69ab2011-02-14 17:03:18 -0800537static jfloat android_view_MotionEvent_nativeGetXPrecision(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000538 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800539 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
540 return event->getXPrecision();
541}
542
543static jfloat android_view_MotionEvent_nativeGetYPrecision(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000544 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800545 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
546 return event->getYPrecision();
547}
548
549static jlong android_view_MotionEvent_nativeGetDownTimeNanos(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000550 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800551 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
552 return event->getDownTime();
553}
554
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700555static void android_view_MotionEvent_nativeSetDownTimeNanos(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000556 jlong nativePtr, jlong downTimeNanos) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700557 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
558 event->setDownTime(downTimeNanos);
559}
560
Jeff Brown91c69ab2011-02-14 17:03:18 -0800561static jint android_view_MotionEvent_nativeGetPointerCount(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000562 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800563 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
564 return jint(event->getPointerCount());
565}
566
567static jint android_view_MotionEvent_nativeGetPointerId(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000568 jlong nativePtr, jint pointerIndex) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800569 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
570 size_t pointerCount = event->getPointerCount();
571 if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
572 return -1;
573 }
574 return event->getPointerId(pointerIndex);
575}
576
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700577static jint android_view_MotionEvent_nativeGetToolType(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000578 jlong nativePtr, jint pointerIndex) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700579 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
580 size_t pointerCount = event->getPointerCount();
581 if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
582 return -1;
583 }
584 return event->getToolType(pointerIndex);
585}
586
Jeff Brown91c69ab2011-02-14 17:03:18 -0800587static jint android_view_MotionEvent_nativeFindPointerIndex(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000588 jlong nativePtr, jint pointerId) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800589 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
Jeff Brown2ed24622011-03-14 19:39:54 -0700590 return jint(event->findPointerIndex(pointerId));
Jeff Brown91c69ab2011-02-14 17:03:18 -0800591}
592
593static jint android_view_MotionEvent_nativeGetHistorySize(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000594 jlong nativePtr) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800595 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
596 return jint(event->getHistorySize());
597}
598
599static jlong android_view_MotionEvent_nativeGetEventTimeNanos(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000600 jlong nativePtr, jint historyPos) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800601 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
602 if (historyPos == HISTORY_CURRENT) {
603 return event->getEventTime();
604 } else {
605 size_t historySize = event->getHistorySize();
606 if (!validateHistoryPos(env, historyPos, historySize)) {
607 return 0;
608 }
609 return event->getHistoricalEventTime(historyPos);
610 }
611}
612
613static jfloat android_view_MotionEvent_nativeGetRawAxisValue(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000614 jlong nativePtr, jint axis, jint pointerIndex, jint historyPos) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800615 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
616 size_t pointerCount = event->getPointerCount();
617 if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
618 return 0;
619 }
620
621 if (historyPos == HISTORY_CURRENT) {
622 return event->getRawAxisValue(axis, pointerIndex);
623 } else {
624 size_t historySize = event->getHistorySize();
625 if (!validateHistoryPos(env, historyPos, historySize)) {
626 return 0;
627 }
628 return event->getHistoricalRawAxisValue(axis, pointerIndex, historyPos);
629 }
630}
631
632static jfloat android_view_MotionEvent_nativeGetAxisValue(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000633 jlong nativePtr, jint axis, jint pointerIndex, jint historyPos) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800634 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
635 size_t pointerCount = event->getPointerCount();
636 if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
637 return 0;
638 }
639
640 if (historyPos == HISTORY_CURRENT) {
641 return event->getAxisValue(axis, pointerIndex);
642 } else {
643 size_t historySize = event->getHistorySize();
644 if (!validateHistoryPos(env, historyPos, historySize)) {
645 return 0;
646 }
647 return event->getHistoricalAxisValue(axis, pointerIndex, historyPos);
648 }
649}
650
651static void android_view_MotionEvent_nativeGetPointerCoords(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000652 jlong nativePtr, jint pointerIndex, jint historyPos, jobject outPointerCoordsObj) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800653 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
654 size_t pointerCount = event->getPointerCount();
655 if (!validatePointerIndex(env, pointerIndex, pointerCount)
656 || !validatePointerCoords(env, outPointerCoordsObj)) {
657 return;
658 }
659
660 const PointerCoords* rawPointerCoords;
661 if (historyPos == HISTORY_CURRENT) {
662 rawPointerCoords = event->getRawPointerCoords(pointerIndex);
663 } else {
664 size_t historySize = event->getHistorySize();
665 if (!validateHistoryPos(env, historyPos, historySize)) {
666 return;
667 }
668 rawPointerCoords = event->getHistoricalRawPointerCoords(pointerIndex, historyPos);
669 }
670 pointerCoordsFromNative(env, rawPointerCoords, event->getXOffset(), event->getYOffset(),
671 outPointerCoordsObj);
672}
673
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700674static void android_view_MotionEvent_nativeGetPointerProperties(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000675 jlong nativePtr, jint pointerIndex, jobject outPointerPropertiesObj) {
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700676 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
677 size_t pointerCount = event->getPointerCount();
678 if (!validatePointerIndex(env, pointerIndex, pointerCount)
679 || !validatePointerProperties(env, outPointerPropertiesObj)) {
680 return;
681 }
682
683 const PointerProperties* pointerProperties = event->getPointerProperties(pointerIndex);
684 pointerPropertiesFromNative(env, pointerProperties, outPointerPropertiesObj);
685}
686
Jeff Brown91c69ab2011-02-14 17:03:18 -0800687static void android_view_MotionEvent_nativeScale(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000688 jlong nativePtr, jfloat scale) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800689 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
690 event->scale(scale);
691}
692
693static void android_view_MotionEvent_nativeTransform(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000694 jlong nativePtr, jobject matrixObj) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800695 SkMatrix* matrix = android_graphics_Matrix_getSkMatrix(env, matrixObj);
696 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
Jeff Brown11093282013-07-15 18:05:59 -0700697
698 float m[9];
699 m[0] = SkScalarToFloat(matrix->get(SkMatrix::kMScaleX));
700 m[1] = SkScalarToFloat(matrix->get(SkMatrix::kMSkewX));
701 m[2] = SkScalarToFloat(matrix->get(SkMatrix::kMTransX));
702 m[3] = SkScalarToFloat(matrix->get(SkMatrix::kMSkewY));
703 m[4] = SkScalarToFloat(matrix->get(SkMatrix::kMScaleY));
704 m[5] = SkScalarToFloat(matrix->get(SkMatrix::kMTransY));
705 m[6] = SkScalarToFloat(matrix->get(SkMatrix::kMPersp0));
706 m[7] = SkScalarToFloat(matrix->get(SkMatrix::kMPersp1));
707 m[8] = SkScalarToFloat(matrix->get(SkMatrix::kMPersp2));
708 event->transform(m);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800709}
710
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000711static jlong android_view_MotionEvent_nativeReadFromParcel(JNIEnv* env, jclass clazz,
712 jlong nativePtr, jobject parcelObj) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800713 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
714 if (!event) {
715 event = new MotionEvent();
716 }
717
718 Parcel* parcel = parcelForJavaObject(env, parcelObj);
719
720 status_t status = event->readFromParcel(parcel);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800721 if (status) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800722 if (!nativePtr) {
723 delete event;
724 }
725 jniThrowRuntimeException(env, "Failed to read MotionEvent parcel.");
726 return 0;
727 }
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000728 return reinterpret_cast<jlong>(event);
Jeff Brown91c69ab2011-02-14 17:03:18 -0800729}
730
731static void android_view_MotionEvent_nativeWriteToParcel(JNIEnv* env, jclass clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000732 jlong nativePtr, jobject parcelObj) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800733 MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
734 Parcel* parcel = parcelForJavaObject(env, parcelObj);
735
736 status_t status = event->writeToParcel(parcel);
Jeff Brownebbd5d12011-02-17 13:01:34 -0800737 if (status) {
Jeff Brown91c69ab2011-02-14 17:03:18 -0800738 jniThrowRuntimeException(env, "Failed to write MotionEvent parcel.");
739 }
Jeff Brown20e987b2010-08-23 12:01:02 -0700740}
741
Michael Wright337d9d22014-04-22 15:03:48 -0700742static jstring android_view_MotionEvent_nativeAxisToString(JNIEnv* env, jclass clazz,
743 jint axis) {
744 return env->NewStringUTF(MotionEvent::getLabel(static_cast<int32_t>(axis)));
745}
746
747static jint android_view_MotionEvent_nativeAxisFromString(JNIEnv* env, jclass clazz,
748 jstring label) {
749 ScopedUtfChars axisLabel(env, label);
750 return static_cast<jint>(MotionEvent::getAxisFromLabel(axisLabel.c_str()));
751}
752
Jeff Brown46b9ac02010-04-22 18:58:52 -0700753// ----------------------------------------------------------------------------
754
Daniel Micay76f6a862015-09-19 17:31:01 -0400755static const JNINativeMethod gMotionEventMethods[] = {
Jeff Brown20e987b2010-08-23 12:01:02 -0700756 /* name, signature, funcPtr */
Jeff Brown91c69ab2011-02-14 17:03:18 -0800757 { "nativeInitialize",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000758 "(JIIIIIIIFFFFJJI[Landroid/view/MotionEvent$PointerProperties;"
759 "[Landroid/view/MotionEvent$PointerCoords;)J",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800760 (void*)android_view_MotionEvent_nativeInitialize },
761 { "nativeCopy",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000762 "(JJZ)J",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800763 (void*)android_view_MotionEvent_nativeCopy },
764 { "nativeDispose",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000765 "(J)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800766 (void*)android_view_MotionEvent_nativeDispose },
767 { "nativeAddBatch",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000768 "(JJ[Landroid/view/MotionEvent$PointerCoords;I)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800769 (void*)android_view_MotionEvent_nativeAddBatch },
Chris Craik526cf822015-10-16 17:42:57 +0000770 { "nativeGetDeviceId",
Chris Craik0fc10652015-10-16 11:39:50 -0700771 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800772 (void*)android_view_MotionEvent_nativeGetDeviceId },
Chris Craik526cf822015-10-16 17:42:57 +0000773 { "nativeGetSource",
Chris Craik0fc10652015-10-16 11:39:50 -0700774 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800775 (void*)android_view_MotionEvent_nativeGetSource },
Chris Craik526cf822015-10-16 17:42:57 +0000776 { "nativeSetSource",
Chris Craik0fc10652015-10-16 11:39:50 -0700777 "!(JI)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800778 (void*)android_view_MotionEvent_nativeSetSource },
Chris Craik526cf822015-10-16 17:42:57 +0000779 { "nativeGetAction",
Chris Craik0fc10652015-10-16 11:39:50 -0700780 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800781 (void*)android_view_MotionEvent_nativeGetAction },
Chris Craik526cf822015-10-16 17:42:57 +0000782 { "nativeSetAction",
Chris Craik0fc10652015-10-16 11:39:50 -0700783 "!(JI)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800784 (void*)android_view_MotionEvent_nativeSetAction },
Chris Craik526cf822015-10-16 17:42:57 +0000785 { "nativeGetActionButton",
Chris Craik0fc10652015-10-16 11:39:50 -0700786 "!(J)I",
Michael Wright5bd69e62015-05-14 14:48:08 +0100787 (void*)android_view_MotionEvent_nativeGetActionButton},
Chris Craik526cf822015-10-16 17:42:57 +0000788 { "nativeSetActionButton",
Chris Craik0fc10652015-10-16 11:39:50 -0700789 "!(JI)V",
Michael Wright6b819b42015-06-17 21:06:03 +0100790 (void*)android_view_MotionEvent_nativeSetActionButton},
Chris Craik526cf822015-10-16 17:42:57 +0000791 { "nativeIsTouchEvent",
Chris Craik0fc10652015-10-16 11:39:50 -0700792 "!(J)Z",
Jeff Brown56194eb2011-03-02 19:23:13 -0800793 (void*)android_view_MotionEvent_nativeIsTouchEvent },
Chris Craik526cf822015-10-16 17:42:57 +0000794 { "nativeGetFlags",
Chris Craik0fc10652015-10-16 11:39:50 -0700795 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800796 (void*)android_view_MotionEvent_nativeGetFlags },
Chris Craik526cf822015-10-16 17:42:57 +0000797 { "nativeSetFlags",
Chris Craik0fc10652015-10-16 11:39:50 -0700798 "!(JI)V",
Jeff Brown21bc5c92011-02-28 18:27:14 -0800799 (void*)android_view_MotionEvent_nativeSetFlags },
Chris Craik526cf822015-10-16 17:42:57 +0000800 { "nativeGetEdgeFlags",
Chris Craik0fc10652015-10-16 11:39:50 -0700801 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800802 (void*)android_view_MotionEvent_nativeGetEdgeFlags },
Chris Craik526cf822015-10-16 17:42:57 +0000803 { "nativeSetEdgeFlags",
Chris Craik0fc10652015-10-16 11:39:50 -0700804 "!(JI)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800805 (void*)android_view_MotionEvent_nativeSetEdgeFlags },
Chris Craik526cf822015-10-16 17:42:57 +0000806 { "nativeGetMetaState",
Chris Craik0fc10652015-10-16 11:39:50 -0700807 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800808 (void*)android_view_MotionEvent_nativeGetMetaState },
Chris Craik526cf822015-10-16 17:42:57 +0000809 { "nativeGetButtonState",
Chris Craik0fc10652015-10-16 11:39:50 -0700810 "!(J)I",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700811 (void*)android_view_MotionEvent_nativeGetButtonState },
Chris Craik526cf822015-10-16 17:42:57 +0000812 { "nativeSetButtonState",
Chris Craik0fc10652015-10-16 11:39:50 -0700813 "!(JI)V",
Michael Wright5bd69e62015-05-14 14:48:08 +0100814 (void*)android_view_MotionEvent_nativeSetButtonState },
Chris Craik526cf822015-10-16 17:42:57 +0000815 { "nativeOffsetLocation",
Chris Craik0fc10652015-10-16 11:39:50 -0700816 "!(JFF)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800817 (void*)android_view_MotionEvent_nativeOffsetLocation },
Chris Craik526cf822015-10-16 17:42:57 +0000818 { "nativeGetXOffset",
Chris Craik0fc10652015-10-16 11:39:50 -0700819 "!(J)F",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700820 (void*)android_view_MotionEvent_nativeGetXOffset },
Chris Craik526cf822015-10-16 17:42:57 +0000821 { "nativeGetYOffset",
Chris Craik0fc10652015-10-16 11:39:50 -0700822 "!(J)F",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700823 (void*)android_view_MotionEvent_nativeGetYOffset },
Chris Craik526cf822015-10-16 17:42:57 +0000824 { "nativeGetXPrecision",
Chris Craik0fc10652015-10-16 11:39:50 -0700825 "!(J)F",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800826 (void*)android_view_MotionEvent_nativeGetXPrecision },
Chris Craik526cf822015-10-16 17:42:57 +0000827 { "nativeGetYPrecision",
Chris Craik0fc10652015-10-16 11:39:50 -0700828 "!(J)F",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800829 (void*)android_view_MotionEvent_nativeGetYPrecision },
Chris Craik526cf822015-10-16 17:42:57 +0000830 { "nativeGetDownTimeNanos",
Chris Craik0fc10652015-10-16 11:39:50 -0700831 "!(J)J",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800832 (void*)android_view_MotionEvent_nativeGetDownTimeNanos },
Chris Craik526cf822015-10-16 17:42:57 +0000833 { "nativeSetDownTimeNanos",
Chris Craik0fc10652015-10-16 11:39:50 -0700834 "!(JJ)V",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700835 (void*)android_view_MotionEvent_nativeSetDownTimeNanos },
Chris Craik526cf822015-10-16 17:42:57 +0000836 { "nativeGetPointerCount",
Chris Craik0fc10652015-10-16 11:39:50 -0700837 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800838 (void*)android_view_MotionEvent_nativeGetPointerCount },
Chris Craik526cf822015-10-16 17:42:57 +0000839 { "nativeGetPointerId",
Chris Craik0fc10652015-10-16 11:39:50 -0700840 "!(JI)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800841 (void*)android_view_MotionEvent_nativeGetPointerId },
Chris Craik526cf822015-10-16 17:42:57 +0000842 { "nativeGetToolType",
Chris Craik0fc10652015-10-16 11:39:50 -0700843 "!(JI)I",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700844 (void*)android_view_MotionEvent_nativeGetToolType },
Chris Craik526cf822015-10-16 17:42:57 +0000845 { "nativeFindPointerIndex",
Chris Craik0fc10652015-10-16 11:39:50 -0700846 "!(JI)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800847 (void*)android_view_MotionEvent_nativeFindPointerIndex },
Chris Craik526cf822015-10-16 17:42:57 +0000848 { "nativeGetHistorySize",
Chris Craik0fc10652015-10-16 11:39:50 -0700849 "!(J)I",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800850 (void*)android_view_MotionEvent_nativeGetHistorySize },
Chris Craik526cf822015-10-16 17:42:57 +0000851 { "nativeGetEventTimeNanos",
Chris Craik0fc10652015-10-16 11:39:50 -0700852 "!(JI)J",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800853 (void*)android_view_MotionEvent_nativeGetEventTimeNanos },
Chris Craik526cf822015-10-16 17:42:57 +0000854 { "nativeGetRawAxisValue",
Chris Craik0fc10652015-10-16 11:39:50 -0700855 "!(JIII)F",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800856 (void*)android_view_MotionEvent_nativeGetRawAxisValue },
Chris Craik526cf822015-10-16 17:42:57 +0000857 { "nativeGetAxisValue",
Chris Craik0fc10652015-10-16 11:39:50 -0700858 "!(JIII)F",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800859 (void*)android_view_MotionEvent_nativeGetAxisValue },
860 { "nativeGetPointerCoords",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000861 "(JIILandroid/view/MotionEvent$PointerCoords;)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800862 (void*)android_view_MotionEvent_nativeGetPointerCoords },
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700863 { "nativeGetPointerProperties",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000864 "(JILandroid/view/MotionEvent$PointerProperties;)V",
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700865 (void*)android_view_MotionEvent_nativeGetPointerProperties },
Chris Craik526cf822015-10-16 17:42:57 +0000866 { "nativeScale",
Chris Craik0fc10652015-10-16 11:39:50 -0700867 "!(JF)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800868 (void*)android_view_MotionEvent_nativeScale },
Jeff Brown20e987b2010-08-23 12:01:02 -0700869 { "nativeTransform",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000870 "(JLandroid/graphics/Matrix;)V",
Jeff Brown20e987b2010-08-23 12:01:02 -0700871 (void*)android_view_MotionEvent_nativeTransform },
Jeff Brown91c69ab2011-02-14 17:03:18 -0800872 { "nativeReadFromParcel",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000873 "(JLandroid/os/Parcel;)J",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800874 (void*)android_view_MotionEvent_nativeReadFromParcel },
875 { "nativeWriteToParcel",
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000876 "(JLandroid/os/Parcel;)V",
Jeff Brown91c69ab2011-02-14 17:03:18 -0800877 (void*)android_view_MotionEvent_nativeWriteToParcel },
Michael Wright337d9d22014-04-22 15:03:48 -0700878 { "nativeAxisToString", "(I)Ljava/lang/String;",
879 (void*)android_view_MotionEvent_nativeAxisToString },
880 { "nativeAxisFromString", "(Ljava/lang/String;)I",
881 (void*)android_view_MotionEvent_nativeAxisFromString },
Jeff Brown20e987b2010-08-23 12:01:02 -0700882};
883
Jeff Brown46b9ac02010-04-22 18:58:52 -0700884int register_android_view_MotionEvent(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800885 int res = RegisterMethodsOrDie(env, "android/view/MotionEvent", gMotionEventMethods,
886 NELEM(gMotionEventMethods));
Jeff Brown20e987b2010-08-23 12:01:02 -0700887
Andreas Gampe987f79f2014-11-18 17:29:46 -0800888 gMotionEventClassInfo.clazz = FindClassOrDie(env, "android/view/MotionEvent");
889 gMotionEventClassInfo.clazz = MakeGlobalRefOrDie(env, gMotionEventClassInfo.clazz);
Jeff Brown46b9ac02010-04-22 18:58:52 -0700890
Andreas Gampe987f79f2014-11-18 17:29:46 -0800891 gMotionEventClassInfo.obtain = GetStaticMethodIDOrDie(env, gMotionEventClassInfo.clazz,
Jeff Brown91c69ab2011-02-14 17:03:18 -0800892 "obtain", "()Landroid/view/MotionEvent;");
Andreas Gampe987f79f2014-11-18 17:29:46 -0800893 gMotionEventClassInfo.recycle = GetMethodIDOrDie(env, gMotionEventClassInfo.clazz,
Jeff Brown46b9ac02010-04-22 18:58:52 -0700894 "recycle", "()V");
Andreas Gampe987f79f2014-11-18 17:29:46 -0800895 gMotionEventClassInfo.mNativePtr = GetFieldIDOrDie(env, gMotionEventClassInfo.clazz,
Ashok Bhat99a1ef22014-01-08 14:45:08 +0000896 "mNativePtr", "J");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700897
Andreas Gampe987f79f2014-11-18 17:29:46 -0800898 jclass clazz = FindClassOrDie(env, "android/view/MotionEvent$PointerCoords");
Jeff Brown91c69ab2011-02-14 17:03:18 -0800899
Andreas Gampe987f79f2014-11-18 17:29:46 -0800900 gPointerCoordsClassInfo.mPackedAxisBits = GetFieldIDOrDie(env, clazz, "mPackedAxisBits", "J");
901 gPointerCoordsClassInfo.mPackedAxisValues = GetFieldIDOrDie(env, clazz, "mPackedAxisValues",
902 "[F");
903 gPointerCoordsClassInfo.x = GetFieldIDOrDie(env, clazz, "x", "F");
904 gPointerCoordsClassInfo.y = GetFieldIDOrDie(env, clazz, "y", "F");
905 gPointerCoordsClassInfo.pressure = GetFieldIDOrDie(env, clazz, "pressure", "F");
906 gPointerCoordsClassInfo.size = GetFieldIDOrDie(env, clazz, "size", "F");
907 gPointerCoordsClassInfo.touchMajor = GetFieldIDOrDie(env, clazz, "touchMajor", "F");
908 gPointerCoordsClassInfo.touchMinor = GetFieldIDOrDie(env, clazz, "touchMinor", "F");
909 gPointerCoordsClassInfo.toolMajor = GetFieldIDOrDie(env, clazz, "toolMajor", "F");
910 gPointerCoordsClassInfo.toolMinor = GetFieldIDOrDie(env, clazz, "toolMinor", "F");
911 gPointerCoordsClassInfo.orientation = GetFieldIDOrDie(env, clazz, "orientation", "F");
Jeff Brown46b9ac02010-04-22 18:58:52 -0700912
Andreas Gampe987f79f2014-11-18 17:29:46 -0800913 clazz = FindClassOrDie(env, "android/view/MotionEvent$PointerProperties");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700914
Andreas Gampe987f79f2014-11-18 17:29:46 -0800915 gPointerPropertiesClassInfo.id = GetFieldIDOrDie(env, clazz, "id", "I");
916 gPointerPropertiesClassInfo.toolType = GetFieldIDOrDie(env, clazz, "toolType", "I");
Jeff Brownfe9f8ab2011-05-06 18:20:01 -0700917
Andreas Gampe987f79f2014-11-18 17:29:46 -0800918 return res;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700919}
920
921} // namespace android