1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "MotionEvent-JNI"
#include "JNIHelp.h"
#include <android_runtime/AndroidRuntime.h>
#include <utils/Log.h>
#include <ui/Input.h>
#include "android_view_MotionEvent.h"
#include "android_util_Binder.h"
#include "android/graphics/Matrix.h"
#include "SkMatrix.h"
namespace android {
// ----------------------------------------------------------------------------
static struct {
jclass clazz;
jmethodID obtain;
jmethodID recycle;
jfieldID mNativePtr;
} gMotionEventClassInfo;
static struct {
jclass clazz;
jfieldID mPackedAxisBits;
jfieldID mPackedAxisValues;
jfieldID x;
jfieldID y;
jfieldID pressure;
jfieldID size;
jfieldID touchMajor;
jfieldID touchMinor;
jfieldID toolMajor;
jfieldID toolMinor;
jfieldID orientation;
} gPointerCoordsClassInfo;
// ----------------------------------------------------------------------------
static MotionEvent* android_view_MotionEvent_getNativePtr(JNIEnv* env, jobject eventObj) {
return reinterpret_cast<MotionEvent*>(
env->GetIntField(eventObj, gMotionEventClassInfo.mNativePtr));
}
static void android_view_MotionEvent_setNativePtr(JNIEnv* env, jobject eventObj,
MotionEvent* event) {
env->SetIntField(eventObj, gMotionEventClassInfo.mNativePtr,
reinterpret_cast<int>(event));
}
jobject android_view_MotionEvent_fromNative(JNIEnv* env, const MotionEvent* event) {
jobject eventObj = env->CallStaticObjectMethod(gMotionEventClassInfo.clazz,
gMotionEventClassInfo.obtain);
if (env->ExceptionCheck()) {
LOGE("An exception occurred while obtaining a motion event.");
LOGE_EX(env);
env->ExceptionClear();
return NULL;
}
MotionEvent* destEvent = android_view_MotionEvent_getNativePtr(env, eventObj);
if (!destEvent) {
destEvent = new MotionEvent();
android_view_MotionEvent_setNativePtr(env, eventObj, destEvent);
}
destEvent->copyFrom(event, true);
return eventObj;
}
status_t android_view_MotionEvent_toNative(JNIEnv* env, jobject eventObj,
MotionEvent* event) {
MotionEvent* srcEvent = android_view_MotionEvent_getNativePtr(env, eventObj);
if (!srcEvent) {
LOGE("MotionEvent was finalized");
return BAD_VALUE;
}
event->copyFrom(srcEvent, true);
return OK;
}
status_t android_view_MotionEvent_recycle(JNIEnv* env, jobject eventObj) {
env->CallVoidMethod(eventObj, gMotionEventClassInfo.recycle);
if (env->ExceptionCheck()) {
LOGW("An exception occurred while recycling a motion event.");
LOGW_EX(env);
env->ExceptionClear();
return UNKNOWN_ERROR;
}
return OK;
}
// ----------------------------------------------------------------------------
static const jint HISTORY_CURRENT = -0x80000000;
static bool validatePointerCount(JNIEnv* env, jint pointerCount) {
if (pointerCount < 1) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerCount must be at least 1");
return false;
}
return true;
}
static bool validatePointerIdsArray(JNIEnv* env, jintArray pointerIdsArray,
size_t pointerCount) {
if (!pointerIdsArray) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerIds array must not be null");
return false;
}
size_t length = size_t(env->GetArrayLength(pointerIdsArray));
if (length < pointerCount) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerIds array must be large enough to hold all pointers");
return false;
}
return true;
}
static bool validatePointerCoordsObjArray(JNIEnv* env, jobjectArray pointerCoordsObjArray,
size_t pointerCount) {
if (!pointerCoordsObjArray) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerCoords array must not be null");
return false;
}
size_t length = size_t(env->GetArrayLength(pointerCoordsObjArray));
if (length < pointerCount) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerCoords array must be large enough to hold all pointers");
return false;
}
return true;
}
static bool validatePointerIndex(JNIEnv* env, jint pointerIndex, size_t pointerCount) {
if (pointerIndex < 0 || size_t(pointerIndex) >= pointerCount) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerIndex out of range");
return false;
}
return true;
}
static bool validateHistoryPos(JNIEnv* env, jint historyPos, size_t historySize) {
if (historyPos < 0 || size_t(historyPos) >= historySize) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"historyPos out of range");
return false;
}
return true;
}
static bool validatePointerCoords(JNIEnv* env, jobject pointerCoordsObj) {
if (!pointerCoordsObj) {
jniThrowException(env, "java/lang/IllegalArgumentException",
"pointerCoords must not be null");
return false;
}
return true;
}
static void pointerCoordsToNative(JNIEnv* env, jobject pointerCoordsObj,
float xOffset, float yOffset, PointerCoords* outRawPointerCoords) {
outRawPointerCoords->clear();
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_X,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.x) - xOffset);
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_Y,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.y) - yOffset);
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_PRESSURE,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.pressure));
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_SIZE,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.size));
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.touchMajor));
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.touchMinor));
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.toolMajor));
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_TOOL_MINOR,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.toolMinor));
outRawPointerCoords->setAxisValue(AINPUT_MOTION_AXIS_ORIENTATION,
env->GetFloatField(pointerCoordsObj, gPointerCoordsClassInfo.orientation));
uint32_t bits = env->GetIntField(pointerCoordsObj,
gPointerCoordsClassInfo.mPackedAxisBits);
if (bits) {
jfloatArray valuesArray = jfloatArray(env->GetObjectField(pointerCoordsObj,
gPointerCoordsClassInfo.mPackedAxisValues));
if (valuesArray) {
jfloat* values = static_cast<jfloat*>(
env->GetPrimitiveArrayCritical(valuesArray, NULL));
uint32_t index = 0;
do {
uint32_t axis = __builtin_ctz(bits);
uint32_t axisBit = 1 << axis;
bits &= ~axisBit;
outRawPointerCoords->setAxisValue(axis, values[index++]);
} while (bits);
env->ReleasePrimitiveArrayCritical(valuesArray, values, JNI_ABORT);
env->DeleteLocalRef(valuesArray);
}
}
}
static jfloatArray obtainPackedAxisValuesArray(JNIEnv* env, uint32_t minSize,
jobject outPointerCoordsObj) {
jfloatArray outValuesArray = jfloatArray(env->GetObjectField(outPointerCoordsObj,
gPointerCoordsClassInfo.mPackedAxisValues));
if (outValuesArray) {
uint32_t size = env->GetArrayLength(outValuesArray);
if (minSize <= size) {
return outValuesArray;
}
env->DeleteLocalRef(outValuesArray);
}
uint32_t size = 8;
while (size < minSize) {
size *= 2;
}
outValuesArray = env->NewFloatArray(size);
env->SetObjectField(outPointerCoordsObj,
gPointerCoordsClassInfo.mPackedAxisValues, outValuesArray);
return outValuesArray;
}
static void pointerCoordsFromNative(JNIEnv* env, const PointerCoords* rawPointerCoords,
float xOffset, float yOffset, jobject outPointerCoordsObj) {
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.x,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_X) + xOffset);
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.y,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_Y) + yOffset);
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.pressure,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_PRESSURE));
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.size,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_SIZE));
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.touchMajor,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MAJOR));
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.touchMinor,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_TOUCH_MINOR));
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.toolMajor,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_TOOL_MAJOR));
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.toolMinor,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_TOOL_MINOR));
env->SetFloatField(outPointerCoordsObj, gPointerCoordsClassInfo.orientation,
rawPointerCoords->getAxisValue(AINPUT_MOTION_AXIS_ORIENTATION));
const uint32_t unpackedAxisBits = 0
| (1 << AINPUT_MOTION_AXIS_X)
| (1 << AINPUT_MOTION_AXIS_Y)
| (1 << AINPUT_MOTION_AXIS_PRESSURE)
| (1 << AINPUT_MOTION_AXIS_SIZE)
| (1 << AINPUT_MOTION_AXIS_TOUCH_MAJOR)
| (1 << AINPUT_MOTION_AXIS_TOUCH_MINOR)
| (1 << AINPUT_MOTION_AXIS_TOOL_MAJOR)
| (1 << AINPUT_MOTION_AXIS_TOOL_MINOR)
| (1 << AINPUT_MOTION_AXIS_ORIENTATION);
uint32_t outBits = 0;
uint32_t remainingBits = rawPointerCoords->bits & ~unpackedAxisBits;
if (remainingBits) {
uint32_t packedAxesCount = __builtin_popcount(remainingBits);
jfloatArray outValuesArray = obtainPackedAxisValuesArray(env, packedAxesCount,
outPointerCoordsObj);
if (!outValuesArray) {
return; // OOM
}
jfloat* outValues = static_cast<jfloat*>(env->GetPrimitiveArrayCritical(
outValuesArray, NULL));
const float* values = rawPointerCoords->values;
uint32_t index = 0;
do {
uint32_t axis = __builtin_ctz(remainingBits);
uint32_t axisBit = 1 << axis;
remainingBits &= ~axisBit;
outBits |= axisBit;
outValues[index++] = rawPointerCoords->getAxisValue(axis);
} while (remainingBits);
env->ReleasePrimitiveArrayCritical(outValuesArray, outValues, 0);
env->DeleteLocalRef(outValuesArray);
}
env->SetIntField(outPointerCoordsObj, gPointerCoordsClassInfo.mPackedAxisBits, outBits);
}
// ----------------------------------------------------------------------------
static jint android_view_MotionEvent_nativeInitialize(JNIEnv* env, jclass clazz,
jint nativePtr,
jint deviceId, jint source, jint action, jint flags, jint edgeFlags, jint metaState,
jfloat xOffset, jfloat yOffset, jfloat xPrecision, jfloat yPrecision,
jlong downTimeNanos, jlong eventTimeNanos,
jint pointerCount, jintArray pointerIdsArray, jobjectArray pointerCoordsObjArray) {
if (!validatePointerCount(env, pointerCount)
|| !validatePointerIdsArray(env, pointerIdsArray, pointerCount)
|| !validatePointerCoordsObjArray(env, pointerCoordsObjArray, pointerCount)) {
return 0;
}
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
if (!event) {
event = new MotionEvent();
}
PointerCoords rawPointerCoords[pointerCount];
for (jint i = 0; i < pointerCount; i++) {
jobject pointerCoordsObj = env->GetObjectArrayElement(pointerCoordsObjArray, i);
if (!pointerCoordsObj) {
jniThrowNullPointerException(env, "pointerCoords");
if (!nativePtr) {
delete event;
}
return 0;
}
pointerCoordsToNative(env, pointerCoordsObj, xOffset, yOffset, &rawPointerCoords[i]);
env->DeleteLocalRef(pointerCoordsObj);
}
int* pointerIds = static_cast<int*>(env->GetPrimitiveArrayCritical(pointerIdsArray, NULL));
event->initialize(deviceId, source, action, flags, edgeFlags, metaState,
xOffset, yOffset, xPrecision, yPrecision,
downTimeNanos, eventTimeNanos, pointerCount, pointerIds, rawPointerCoords);
env->ReleasePrimitiveArrayCritical(pointerIdsArray, pointerIds, JNI_ABORT);
return reinterpret_cast<jint>(event);
}
static jint android_view_MotionEvent_nativeCopy(JNIEnv* env, jclass clazz,
jint destNativePtr, jint sourceNativePtr, jboolean keepHistory) {
MotionEvent* destEvent = reinterpret_cast<MotionEvent*>(destNativePtr);
if (!destEvent) {
destEvent = new MotionEvent();
}
MotionEvent* sourceEvent = reinterpret_cast<MotionEvent*>(sourceNativePtr);
destEvent->copyFrom(sourceEvent, keepHistory);
return reinterpret_cast<jint>(destEvent);
}
static void android_view_MotionEvent_nativeDispose(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
delete event;
}
static void android_view_MotionEvent_nativeAddBatch(JNIEnv* env, jclass clazz,
jint nativePtr, jlong eventTimeNanos, jobjectArray pointerCoordsObjArray,
jint metaState) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount();
if (!validatePointerCoordsObjArray(env, pointerCoordsObjArray, pointerCount)) {
return;
}
PointerCoords rawPointerCoords[pointerCount];
for (size_t i = 0; i < pointerCount; i++) {
jobject pointerCoordsObj = env->GetObjectArrayElement(pointerCoordsObjArray, i);
if (!pointerCoordsObj) {
jniThrowNullPointerException(env, "pointerCoords");
return;
}
pointerCoordsToNative(env, pointerCoordsObj,
event->getXOffset(), event->getYOffset(), &rawPointerCoords[i]);
env->DeleteLocalRef(pointerCoordsObj);
}
event->addSample(eventTimeNanos, rawPointerCoords);
event->setMetaState(event->getMetaState() | metaState);
}
static jint android_view_MotionEvent_nativeGetDeviceId(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getDeviceId();
}
static jint android_view_MotionEvent_nativeGetSource(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getSource();
}
static void android_view_MotionEvent_nativeSetSource(JNIEnv* env, jclass clazz,
jint nativePtr, jint source) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
event->setSource(source);
}
static jint android_view_MotionEvent_nativeGetAction(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getAction();
}
static void android_view_MotionEvent_nativeSetAction(JNIEnv* env, jclass clazz,
jint nativePtr, jint action) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
event->setAction(action);
}
static jint android_view_MotionEvent_nativeGetFlags(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getFlags();
}
static jint android_view_MotionEvent_nativeGetEdgeFlags(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getEdgeFlags();
}
static void android_view_MotionEvent_nativeSetEdgeFlags(JNIEnv* env, jclass clazz,
jint nativePtr, jint edgeFlags) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
event->setEdgeFlags(edgeFlags);
}
static jint android_view_MotionEvent_nativeGetMetaState(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getMetaState();
}
static void android_view_MotionEvent_nativeOffsetLocation(JNIEnv* env, jclass clazz,
jint nativePtr, jfloat deltaX, jfloat deltaY) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->offsetLocation(deltaX, deltaY);
}
static jfloat android_view_MotionEvent_nativeGetXPrecision(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getXPrecision();
}
static jfloat android_view_MotionEvent_nativeGetYPrecision(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getYPrecision();
}
static jlong android_view_MotionEvent_nativeGetDownTimeNanos(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return event->getDownTime();
}
static jint android_view_MotionEvent_nativeGetPointerCount(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return jint(event->getPointerCount());
}
static jint android_view_MotionEvent_nativeGetPointerId(JNIEnv* env, jclass clazz,
jint nativePtr, jint pointerIndex) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount();
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return -1;
}
return event->getPointerId(pointerIndex);
}
static jint android_view_MotionEvent_nativeFindPointerIndex(JNIEnv* env, jclass clazz,
jint nativePtr, jint pointerId) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount();
for (size_t i = 0; i < pointerCount; i++) {
if (event->getPointerId(i) == pointerId) {
return i;
}
}
return -1;
}
static jint android_view_MotionEvent_nativeGetHistorySize(JNIEnv* env, jclass clazz,
jint nativePtr) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
return jint(event->getHistorySize());
}
static jlong android_view_MotionEvent_nativeGetEventTimeNanos(JNIEnv* env, jclass clazz,
jint nativePtr, jint historyPos) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
if (historyPos == HISTORY_CURRENT) {
return event->getEventTime();
} else {
size_t historySize = event->getHistorySize();
if (!validateHistoryPos(env, historyPos, historySize)) {
return 0;
}
return event->getHistoricalEventTime(historyPos);
}
}
static jfloat android_view_MotionEvent_nativeGetRawAxisValue(JNIEnv* env, jclass clazz,
jint nativePtr, jint axis, jint pointerIndex, jint historyPos) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount();
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return 0;
}
if (historyPos == HISTORY_CURRENT) {
return event->getRawAxisValue(axis, pointerIndex);
} else {
size_t historySize = event->getHistorySize();
if (!validateHistoryPos(env, historyPos, historySize)) {
return 0;
}
return event->getHistoricalRawAxisValue(axis, pointerIndex, historyPos);
}
}
static jfloat android_view_MotionEvent_nativeGetAxisValue(JNIEnv* env, jclass clazz,
jint nativePtr, jint axis, jint pointerIndex, jint historyPos) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount();
if (!validatePointerIndex(env, pointerIndex, pointerCount)) {
return 0;
}
if (historyPos == HISTORY_CURRENT) {
return event->getAxisValue(axis, pointerIndex);
} else {
size_t historySize = event->getHistorySize();
if (!validateHistoryPos(env, historyPos, historySize)) {
return 0;
}
return event->getHistoricalAxisValue(axis, pointerIndex, historyPos);
}
}
static void android_view_MotionEvent_nativeGetPointerCoords(JNIEnv* env, jclass clazz,
jint nativePtr, jint pointerIndex, jint historyPos, jobject outPointerCoordsObj) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
size_t pointerCount = event->getPointerCount();
if (!validatePointerIndex(env, pointerIndex, pointerCount)
|| !validatePointerCoords(env, outPointerCoordsObj)) {
return;
}
const PointerCoords* rawPointerCoords;
if (historyPos == HISTORY_CURRENT) {
rawPointerCoords = event->getRawPointerCoords(pointerIndex);
} else {
size_t historySize = event->getHistorySize();
if (!validateHistoryPos(env, historyPos, historySize)) {
return;
}
rawPointerCoords = event->getHistoricalRawPointerCoords(pointerIndex, historyPos);
}
pointerCoordsFromNative(env, rawPointerCoords, event->getXOffset(), event->getYOffset(),
outPointerCoordsObj);
}
static void android_view_MotionEvent_nativeScale(JNIEnv* env, jclass clazz,
jint nativePtr, jfloat scale) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
event->scale(scale);
}
static void android_view_MotionEvent_nativeTransform(JNIEnv* env, jclass clazz,
jint nativePtr, jobject matrixObj) {
SkMatrix* matrix = android_graphics_Matrix_getSkMatrix(env, matrixObj);
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
event->transform(matrix);
}
static jint android_view_MotionEvent_nativeReadFromParcel(JNIEnv* env, jclass clazz,
jint nativePtr, jobject parcelObj) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
if (!event) {
event = new MotionEvent();
}
Parcel* parcel = parcelForJavaObject(env, parcelObj);
status_t status = event->readFromParcel(parcel);
if (!status) {
if (!nativePtr) {
delete event;
}
jniThrowRuntimeException(env, "Failed to read MotionEvent parcel.");
return 0;
}
return reinterpret_cast<jint>(event);
}
static void android_view_MotionEvent_nativeWriteToParcel(JNIEnv* env, jclass clazz,
jint nativePtr, jobject parcelObj) {
MotionEvent* event = reinterpret_cast<MotionEvent*>(nativePtr);
Parcel* parcel = parcelForJavaObject(env, parcelObj);
status_t status = event->writeToParcel(parcel);
if (!status) {
jniThrowRuntimeException(env, "Failed to write MotionEvent parcel.");
}
}
// ----------------------------------------------------------------------------
static JNINativeMethod gMotionEventMethods[] = {
/* name, signature, funcPtr */
{ "nativeInitialize",
"(IIIIIIIFFFFJJI[I[Landroid/view/MotionEvent$PointerCoords;)I",
(void*)android_view_MotionEvent_nativeInitialize },
{ "nativeCopy",
"(IIZ)I",
(void*)android_view_MotionEvent_nativeCopy },
{ "nativeDispose",
"(I)V",
(void*)android_view_MotionEvent_nativeDispose },
{ "nativeAddBatch",
"(IJ[Landroid/view/MotionEvent$PointerCoords;I)V",
(void*)android_view_MotionEvent_nativeAddBatch },
{ "nativeGetDeviceId",
"(I)I",
(void*)android_view_MotionEvent_nativeGetDeviceId },
{ "nativeGetSource",
"(I)I",
(void*)android_view_MotionEvent_nativeGetSource },
{ "nativeSetSource",
"(II)I",
(void*)android_view_MotionEvent_nativeSetSource },
{ "nativeGetAction",
"(I)I",
(void*)android_view_MotionEvent_nativeGetAction },
{ "nativeSetAction",
"(II)V",
(void*)android_view_MotionEvent_nativeSetAction },
{ "nativeGetFlags",
"(I)I",
(void*)android_view_MotionEvent_nativeGetFlags },
{ "nativeGetEdgeFlags",
"(I)I",
(void*)android_view_MotionEvent_nativeGetEdgeFlags },
{ "nativeSetEdgeFlags",
"(II)V",
(void*)android_view_MotionEvent_nativeSetEdgeFlags },
{ "nativeGetMetaState",
"(I)I",
(void*)android_view_MotionEvent_nativeGetMetaState },
{ "nativeOffsetLocation",
"(IFF)V",
(void*)android_view_MotionEvent_nativeOffsetLocation },
{ "nativeGetXPrecision",
"(I)F",
(void*)android_view_MotionEvent_nativeGetXPrecision },
{ "nativeGetYPrecision",
"(I)F",
(void*)android_view_MotionEvent_nativeGetYPrecision },
{ "nativeGetDownTimeNanos",
"(I)J",
(void*)android_view_MotionEvent_nativeGetDownTimeNanos },
{ "nativeGetPointerCount",
"(I)I",
(void*)android_view_MotionEvent_nativeGetPointerCount },
{ "nativeGetPointerId",
"(II)I",
(void*)android_view_MotionEvent_nativeGetPointerId },
{ "nativeFindPointerIndex",
"(II)I",
(void*)android_view_MotionEvent_nativeFindPointerIndex },
{ "nativeGetHistorySize",
"(I)I",
(void*)android_view_MotionEvent_nativeGetHistorySize },
{ "nativeGetEventTimeNanos",
"(II)J",
(void*)android_view_MotionEvent_nativeGetEventTimeNanos },
{ "nativeGetRawAxisValue",
"(IIII)F",
(void*)android_view_MotionEvent_nativeGetRawAxisValue },
{ "nativeGetAxisValue",
"(IIII)F",
(void*)android_view_MotionEvent_nativeGetAxisValue },
{ "nativeGetPointerCoords",
"(IIILandroid/view/MotionEvent$PointerCoords;)V",
(void*)android_view_MotionEvent_nativeGetPointerCoords },
{ "nativeScale",
"(IF)V",
(void*)android_view_MotionEvent_nativeScale },
{ "nativeTransform",
"(ILandroid/graphics/Matrix;)V",
(void*)android_view_MotionEvent_nativeTransform },
{ "nativeReadFromParcel",
"(ILandroid/os/Parcel;)I",
(void*)android_view_MotionEvent_nativeReadFromParcel },
{ "nativeWriteToParcel",
"(ILandroid/os/Parcel;)V",
(void*)android_view_MotionEvent_nativeWriteToParcel },
};
#define FIND_CLASS(var, className) \
var = env->FindClass(className); \
LOG_FATAL_IF(! var, "Unable to find class " className); \
var = jclass(env->NewGlobalRef(var));
#define GET_STATIC_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
var = env->GetStaticMethodID(clazz, methodName, fieldDescriptor); \
LOG_FATAL_IF(! var, "Unable to find static method" methodName);
#define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
LOG_FATAL_IF(! var, "Unable to find method" methodName);
#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \
var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \
LOG_FATAL_IF(! var, "Unable to find field " fieldName);
int register_android_view_MotionEvent(JNIEnv* env) {
int res = jniRegisterNativeMethods(env, "android/view/MotionEvent",
gMotionEventMethods, NELEM(gMotionEventMethods));
LOG_FATAL_IF(res < 0, "Unable to register native methods.");
FIND_CLASS(gMotionEventClassInfo.clazz, "android/view/MotionEvent");
GET_STATIC_METHOD_ID(gMotionEventClassInfo.obtain, gMotionEventClassInfo.clazz,
"obtain", "()Landroid/view/MotionEvent;");
GET_METHOD_ID(gMotionEventClassInfo.recycle, gMotionEventClassInfo.clazz,
"recycle", "()V");
GET_FIELD_ID(gMotionEventClassInfo.mNativePtr, gMotionEventClassInfo.clazz,
"mNativePtr", "I");
FIND_CLASS(gPointerCoordsClassInfo.clazz, "android/view/MotionEvent$PointerCoords");
GET_FIELD_ID(gPointerCoordsClassInfo.mPackedAxisBits, gPointerCoordsClassInfo.clazz,
"mPackedAxisBits", "I");
GET_FIELD_ID(gPointerCoordsClassInfo.mPackedAxisValues, gPointerCoordsClassInfo.clazz,
"mPackedAxisValues", "[F");
GET_FIELD_ID(gPointerCoordsClassInfo.x, gPointerCoordsClassInfo.clazz,
"x", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.y, gPointerCoordsClassInfo.clazz,
"y", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.pressure, gPointerCoordsClassInfo.clazz,
"pressure", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.size, gPointerCoordsClassInfo.clazz,
"size", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.touchMajor, gPointerCoordsClassInfo.clazz,
"touchMajor", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.touchMinor, gPointerCoordsClassInfo.clazz,
"touchMinor", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.toolMajor, gPointerCoordsClassInfo.clazz,
"toolMajor", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.toolMinor, gPointerCoordsClassInfo.clazz,
"toolMinor", "F");
GET_FIELD_ID(gPointerCoordsClassInfo.orientation, gPointerCoordsClassInfo.clazz,
"orientation", "F");
return 0;
}
} // namespace android
|