summaryrefslogtreecommitdiff
path: root/android/cpp/JNI.cpp
blob: 44ecea0b4ac9145629354595c1727c37b475a460 (plain)
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#include <cstdint>
#include <cinttypes>

#include <string>
#include <locale>
#include <codecvt>
#include <array>
#include <vector>

#include <jni.h>

#include <android/native_window_jni.h>

#include "log.h"

#include "NativeMapView.hpp"

#pragma clang diagnostic ignored "-Wunused-parameter"

namespace mbgl {
namespace android {

jmethodID on_map_changed_id = nullptr;

jclass lon_lat_class = nullptr;
jmethodID lon_lat_constructor_id = nullptr;
jfieldID lon_lat_lon_id = nullptr;
jfieldID lon_lat_lat_id = nullptr;

jclass lon_lat_zoom_class = nullptr;
jmethodID lon_lat_zoom_constructor_id = nullptr;
jfieldID lon_lat_zoom_lon_id = nullptr;
jfieldID lon_lat_zoom_lat_id = nullptr;
jfieldID lon_lat_zoom_zoom_id = nullptr;

jclass runtime_exception_class = nullptr;

jmethodID set_to_array_id = nullptr;

jclass tree_set_class = nullptr;
jmethodID tree_set_constructor_id = nullptr;
jmethodID tree_set_add_id = nullptr;

bool throw_error(JNIEnv* env, const char* msg) {
    if (env->ThrowNew(runtime_exception_class, msg) < 0) {
        env->ExceptionDescribe();
        return false;
    }

    return true;
}

std::string std_string_from_jstring(JNIEnv* env, jstring jstr) {
    std::string str;
    jsize len = env->GetStringLength(jstr);
    if (len < 0) {
        env->ExceptionDescribe();
        return str;
    }
    const jchar* chars = env->GetStringChars(jstr, nullptr);
    if (chars == nullptr) {
        env->ExceptionDescribe();
        return str;
    }
    std::u16string ustr(reinterpret_cast<const char16_t*>(chars), len);
    env->ReleaseStringChars(jstr, chars);
    chars = nullptr;
    str = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t>().to_bytes(ustr);
    return str;
}

jstring std_string_to_jstring(JNIEnv* env, std::string str) {
    std::u16string ustr = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t>().from_bytes(str);

    jstring jstr = env->NewString(reinterpret_cast<const jchar*>(ustr.c_str()), ustr.size());
    if (jstr == nullptr) {
        env->ExceptionDescribe();
        return nullptr;
    }

    return jstr;
}


// TODO: change from Java TreeSet to ArrayList
std::vector<std::string> std_vector_string_from_jobject(JNIEnv* env, jobject jset) {
    std::vector<std::string> vector;

    jobjectArray array = reinterpret_cast<jobjectArray>(env->CallObjectMethod(jset, set_to_array_id));
    if (env->ExceptionCheck() || (array == nullptr)) {
        env->ExceptionDescribe();
        return vector;
    }

    jsize len = env->GetArrayLength(array);
    if (len < 0) {
        env->ExceptionDescribe();
        return vector;
    }

    for (jsize i = 0; i < len; i++) {
        jstring jstr = reinterpret_cast<jstring>(env->GetObjectArrayElement(array, i));
        if (jstr == nullptr) {
            env->ExceptionDescribe();
            return vector;
        }

        vector.push_back(std_string_from_jstring(env, jstr));
    }

    return vector;
}

// TODO: change from Java TreeSet to ArrayList
jobject std_vector_string_to_jobject(JNIEnv* env, std::vector<std::string> vector)  {
    jobject jset = env->NewObject(tree_set_class, tree_set_constructor_id);
    if (jset == nullptr) {
        env->ExceptionDescribe();
        return nullptr;
    }

    for (std::string str : vector) {
        env->CallBooleanMethod(jset, tree_set_add_id, std_string_to_jstring(env, str));
        if (env->ExceptionCheck()) {
            env->ExceptionDescribe();
            return nullptr;
        }
    }

    return jset;
}

} // namespace android
} // namespace mbgl

namespace {

using namespace mbgl::android;

// TODO: wrap C++ exceptions?
// TODO: wrap other sorts of exceptions? eg coffee catch

jlong JNICALL nativeCreate(JNIEnv* env, jobject obj, jstring style_url, jstring api_key) {
    LOG_VERBOSE("nativeCreate");
    NativeMapView* native_map_view = new NativeMapView(env, obj, std_string_from_jstring(env, style_url), std_string_from_jstring(env, api_key));
    if (native_map_view == nullptr) {
        throw_error(env, "Unable to create NativeMapView.");
        return 0;
    }
    jlong map_view_ptr = reinterpret_cast<jlong>(native_map_view);
    return map_view_ptr;
}

void JNICALL nativeDestroy(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeDestroy");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    delete native_map_view;
    native_map_view = nullptr;
}

void JNICALL nativeInitializeDisplay(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeInitializeDisplay");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    if (!native_map_view->initializeDisplay()) {
        throw_error(env, "Unable to initialize GL display.");
    }
}

void JNICALL nativeTerminateDisplay(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeTerminateDisplay");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->terminateDisplay();
}

void JNICALL nativeInitializeContext(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeInitializeContext");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    if (!native_map_view->initializeContext()) {
        throw_error(env, "Unable to initialize GL context.");
    }
}

void JNICALL nativeTerminateContext(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeTerminateContext");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->terminateContext();
}

void JNICALL nativeCreateSurface(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jobject surface) {
    LOG_VERBOSE("nativeCreateSurface");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    if (!native_map_view->createSurface(ANativeWindow_fromSurface(env, surface))) {
        throw_error(env, "Unable to create GL surface.");
    }
}

void JNICALL nativeDestroySurface(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeDestroySurface");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->destroySurface();
}

void JNICALL nativeStart(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStart");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->start();
}

void JNICALL nativeStop(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStop");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->stop();
}

void JNICALL nativeRerender(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeRerender");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->rerender();
}

void JNICALL nativeUpdate(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeUpdate");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->update();
}

void JNICALL nativeCleanup(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeCleanup");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->cleanup();
}

void JNICALL nativeResize(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jint width, jint height, jfloat ratio) {
    LOG_VERBOSE("nativeResize");
    LOG_ASSERT(native_map_view_ptr != 0);
    LOG_ASSERT(width >= 0);
    LOG_ASSERT(height >= 0);
    LOG_ASSERT(width <= UINT16_MAX);
    LOG_ASSERT(height <= UINT16_MAX);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->resize(width, height, ratio);
}

void JNICALL nativeResize(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jint width, jint height, jfloat ratio, jint fb_width, jint fb_height) {
    LOG_VERBOSE("nativeResize");
    LOG_ASSERT(native_map_view_ptr != 0);
    LOG_ASSERT(width >= 0);
    LOG_ASSERT(height >= 0);
    LOG_ASSERT(width <= UINT16_MAX);
    LOG_ASSERT(height <= UINT16_MAX);
    LOG_ASSERT(fb_width >= 0);
    LOG_ASSERT(fb_height >= 0);
    LOG_ASSERT(fb_width <= UINT16_MAX);
    LOG_ASSERT(fb_height <= UINT16_MAX);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->resize(width, height, ratio, fb_width, fb_height);
}

void JNICALL nativeSetAppliedClasses(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jobject applied_classes) {
    LOG_VERBOSE("nativeSetAppliedClasses");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setAppliedClasses(std_vector_string_from_jobject(env, applied_classes));
}

jobject JNICALL nativeGetAppliedClasses(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetAppliedClasses");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return std_vector_string_to_jobject(env, native_map_view->getMap()->getAppliedClasses());
}

void JNICALL nativeSetDefaultTransitionDuration(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jlong duration_milliseconds) {
    LOG_VERBOSE("nativeSetDefaultTransitionDuration");
    LOG_ASSERT(native_map_view_ptr != 0);
    LOG_ASSERT(duration_milliseconds >= 0);
    //LOG_ASSERT(duration_milliseconds <= UINT64_MAX);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setDefaultTransitionDuration(duration_milliseconds);
}

void JNICALL nativeSetStyleJSON(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jstring new_style_json) {
    LOG_VERBOSE("nativeSetStyleJSON");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setStyleJSON(std_string_from_jstring(env, new_style_json));
}

jstring JNICALL nativeGetStyleJSON(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetStyleJSON");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return std_string_to_jstring(env, native_map_view->getMap()->getStyleJSON());
}

void JNICALL nativeCancelTransitions(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeCancelTransitions");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->cancelTransitions();
}

void JNICALL nativeMoveBy(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble dx, jdouble dy, jdouble duration) {
    LOG_VERBOSE("nativeMoveBy");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->moveBy(dx, dy, duration);
}

void JNICALL nativeSetLonLat(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jobject lon_lat, jdouble duration) {
    LOG_VERBOSE("nativeSetLonLat");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);

    double lon = env->GetDoubleField(lon_lat, lon_lat_lon_id);
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        return;
    }

    double lat = env->GetDoubleField(lon_lat, lon_lat_lat_id);
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        return;
    }

    // TODO remove when fixed
    LOG_DEBUG("lon lat %f %f", lon, lat);
    native_map_view->getMap()->setLonLat(lon, lat, duration);
}

jobject JNICALL nativeGetLonLat(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetLonLat");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    double lon, lat;
    native_map_view->getMap()->getLonLat(lon, lat);

    jobject ret = env->NewObject(lon_lat_class, lon_lat_constructor_id, lon, lat);
    if (ret == nullptr) {
        env->ExceptionDescribe();
        return nullptr;
    }

    return ret;
}

void JNICALL nativeStartPanning(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStartPanning");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->startPanning();
}

void JNICALL nativeStopPanning(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStopPanning");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->stopPanning();
}

void JNICALL nativeResetPosition(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeResetPosition");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->resetPosition();
}

void JNICALL nativeScaleBy(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble ds, jdouble cx, jdouble cy, jdouble duration) {
    LOG_VERBOSE("nativeScaleBy");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->scaleBy(ds, cx, cy, duration);
}

void JNICALL nativeSetScale(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble scale, jdouble cx, jdouble cy, jdouble duration) {
    LOG_VERBOSE("nativeSetScale");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setScale(scale, cx, cy, duration);
}

jdouble JNICALL nativeGetScale(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetScale");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->getScale();
}

void JNICALL nativeSetZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble zoom, jdouble duration) {
    LOG_VERBOSE("nativeSetZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setZoom(zoom, duration);
}

jdouble JNICALL nativeGetZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->getZoom();
}

void JNICALL nativeSetLonLatZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jobject lon_lat_zoom, jdouble duration) {
    LOG_VERBOSE("nativeSetLonLatZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);

    double lon = env->GetDoubleField(lon_lat_zoom, lon_lat_zoom_lon_id);
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        return;
    }

    double lat = env->GetDoubleField(lon_lat_zoom, lon_lat_zoom_lat_id);
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        return;
    }

    double zoom = env->GetDoubleField(lon_lat_zoom, lon_lat_zoom_zoom_id);
    if (env->ExceptionCheck()) {
        env->ExceptionDescribe();
        return;
    }

    native_map_view->getMap()->setLonLatZoom(lon, lat, zoom, duration);
}

jobject JNICALL nativeGetLonLatZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetLonLatZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    double lon, lat, zoom;
    native_map_view->getMap()->getLonLatZoom(lon, lat, zoom);

    jobject ret = env->NewObject(lon_lat_zoom_class, lon_lat_zoom_constructor_id, lon, lat, zoom);
    if (ret == nullptr) {
        env->ExceptionDescribe();
        return nullptr;
    }

    return ret;
}

void JNICALL nativeResetZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeResetZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->resetZoom();
}

void JNICALL nativeStartScaling(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStartScaling");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->startScaling();
}

void JNICALL nativeStopScaling(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStopScaling");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->stopScaling();
}

jdouble JNICALL nativeGetMinZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetMinZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->getMinZoom();
}

jdouble JNICALL nativeGetMaxZoom(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetMaxZoom");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->getMaxZoom();
}

void JNICALL nativeRotateBy(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble sx, jdouble sy, jdouble ex, jdouble ey, jdouble duration) {
    LOG_VERBOSE("nativeRotateBy");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->rotateBy(sx, sy, ex, ey, duration);
}

void JNICALL nativeSetBearing(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble degrees, jdouble duration) {
    LOG_VERBOSE("nativeSetBearing");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setBearing(degrees, duration);
}

void JNICALL nativeSetBearing(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jdouble degrees, jdouble cx, jdouble cy) {
    LOG_VERBOSE("nativeSetBearing");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setBearing(degrees, cx, cy);
}

jdouble JNICALL nativeGetBearing(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetBearing");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->getBearing();
}

void JNICALL nativeResetNorth(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeResetNorth");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->resetNorth();
}

void JNICALL nativeStartRotating(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStartRotating");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->startRotating();
}

void JNICALL nativeStopRotating(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeStopRotating");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->stopRotating();
}

jboolean JNICALL nativeCanRotate(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeCanRotate");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->canRotate();
}

void JNICALL nativeSetDebug(JNIEnv* env, jobject obj, jlong native_map_view_ptr, jboolean debug) {
    LOG_VERBOSE("nativeSetDebug");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->setDebug(debug);
}

void JNICALL nativeToggleDebug(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeToggleDebug");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    native_map_view->getMap()->toggleDebug();
}

jboolean JNICALL nativeGetDebug(JNIEnv* env, jobject obj, jlong native_map_view_ptr) {
    LOG_VERBOSE("nativeGetDebug");
    LOG_ASSERT(native_map_view_ptr != 0);
    NativeMapView* native_map_view = reinterpret_cast<NativeMapView*>(native_map_view_ptr);
    return native_map_view->getMap()->getDebug();
}

} // namespace

extern "C" {

extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
    LOG_VERBOSE("JNI_OnLoad");

    JNIEnv* env = nullptr;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        LOG_ERROR("GetEnv() failed");
        return JNI_ERR;
    }

    lon_lat_class = env->FindClass("com/mapbox/mapboxgl/lib/LonLat");
    if (lon_lat_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_constructor_id = env->GetMethodID(lon_lat_class, "<init>", "(DD)V");
    if (lon_lat_constructor_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_lon_id = env->GetFieldID(lon_lat_class, "lon", "D");
    if (lon_lat_lon_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_lat_id = env->GetFieldID(lon_lat_class, "lat", "D");
    if (lon_lat_lat_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_zoom_class = env->FindClass("com/mapbox/mapboxgl/lib/LonLatZoom");
    if (lon_lat_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_zoom_constructor_id = env->GetMethodID(lon_lat_zoom_class, "<init>", "(DDD)V");
    if (lon_lat_zoom_constructor_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_zoom_lon_id = env->GetFieldID(lon_lat_zoom_class, "lon", "D");
    if (lon_lat_zoom_lon_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_zoom_lat_id = env->GetFieldID(lon_lat_zoom_class, "lat", "D");
    if (lon_lat_zoom_lat_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_zoom_zoom_id = env->GetFieldID(lon_lat_zoom_class, "zoom", "D");
    if (lon_lat_zoom_zoom_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    jclass native_map_view_class = env->FindClass("com/mapbox/mapboxgl/lib/NativeMapView");
    if (native_map_view_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    on_map_changed_id = env->GetMethodID(native_map_view_class, "onMapChanged", "()V");
    if (on_map_changed_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    runtime_exception_class = env->FindClass("java/lang/RuntimeException");
    if (runtime_exception_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    jclass set_class = env->FindClass("java/util/Set");
    if (set_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    set_to_array_id = env->GetMethodID(set_class, "toArray", "()[Ljava/lang/Object;");
    if (set_to_array_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    tree_set_class = env->FindClass("java/util/TreeSet");
    if (tree_set_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    tree_set_constructor_id = env->GetMethodID(tree_set_class, "<init>", "()V");
    if (tree_set_constructor_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    tree_set_add_id = env->GetMethodID(tree_set_class, "add", "(Ljava/lang/Object;)Z");
    if (tree_set_add_id == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    // NOTE: if you get java.lang.UnsatisfiedLinkError you likely forgot to set the size of the array correctly (too large)
    std::array<JNINativeMethod, 50> methods = {{ // Can remove the extra brace in C++14
        { "nativeCreate", "(Ljava/lang/String;Ljava/lang/String;)J", reinterpret_cast<void*>(&nativeCreate) },
        { "nativeDestroy", "(J)V", reinterpret_cast<void*>(&nativeDestroy) },
        { "nativeInitializeDisplay", "(J)V", reinterpret_cast<void*>(&nativeInitializeDisplay) },
        { "nativeTerminateDisplay", "(J)V", reinterpret_cast<void*>(&nativeTerminateDisplay) },
        { "nativeInitializeContext", "(J)V", reinterpret_cast<void*>(&nativeInitializeContext) },
        { "nativeTerminateContext", "(J)V", reinterpret_cast<void*>(&nativeTerminateContext) },
        { "nativeCreateSurface", "(JLandroid/view/Surface;)V", reinterpret_cast<void*>(&nativeCreateSurface) },
        { "nativeDestroySurface", "(J)V", reinterpret_cast<void*>(&nativeDestroySurface) },
        { "nativeStart", "(J)V", reinterpret_cast<void*>(&nativeStart) },
        { "nativeStop", "(J)V", reinterpret_cast<void*>(&nativeStop) },
        { "nativeRerender", "(J)V", reinterpret_cast<void*>(&nativeRerender) },
        { "nativeUpdate", "(J)V", reinterpret_cast<void*>(&nativeUpdate) },
        { "nativeCleanup", "(J)V", reinterpret_cast<void*>(&nativeCleanup) },
        { "nativeResize", "(JIIF)V", reinterpret_cast<void*>(static_cast<void JNICALL(*)(JNIEnv*,jobject,jlong,jint,jint,jfloat)>(&nativeResize)) },
        { "nativeResize", "(JIIFII)V", reinterpret_cast<void*>(static_cast<void JNICALL(*)(JNIEnv*,jobject,jlong,jint,jint,jfloat,jint,jint)>(&nativeResize)) },
        { "nativeSetAppliedClasses", "(JLjava/util/Set;)V", reinterpret_cast<void*>(&nativeSetAppliedClasses) },
        { "nativeGetAppliedClasses", "(J)Ljava/util/Set;", reinterpret_cast<void*>(&nativeGetAppliedClasses) },
        { "nativeSetDefaultTransitionDuration", "(JJ)V", reinterpret_cast<void*>(&nativeSetDefaultTransitionDuration) },
        { "nativeSetStyleJSON", "(JLjava/lang/String;)V", reinterpret_cast<void*>(&nativeSetStyleJSON) },
        { "nativeGetStyleJSON", "(J)Ljava/lang/String;", reinterpret_cast<void*>(&nativeGetStyleJSON) },
        { "nativeCancelTransitions", "(J)V", reinterpret_cast<void*>(&nativeCancelTransitions) },
        { "nativeMoveBy", "(JDDD)V", reinterpret_cast<void*>(&nativeMoveBy) },
        { "nativeSetLonLat", "(JLcom/mapbox/mapboxgl/lib/LonLat;D)V", reinterpret_cast<void*>(&nativeSetLonLat) },
        { "nativeGetLonLat", "(J)Lcom/mapbox/mapboxgl/lib/LonLat;", reinterpret_cast<void*>(&nativeGetLonLat) },
        { "nativeStartPanning", "(J)V", reinterpret_cast<void*>(&nativeStartPanning) },
        { "nativeStopPanning", "(J)V", reinterpret_cast<void*>(&nativeStopPanning) },
        { "nativeResetPosition", "(J)V", reinterpret_cast<void*>(&nativeResetPosition) },
        { "nativeScaleBy", "(JDDDD)V", reinterpret_cast<void*>(&nativeScaleBy) },
        { "nativeSetScale", "(JDDDD)V", reinterpret_cast<void*>(&nativeSetScale) },
        { "nativeGetScale", "(J)D", reinterpret_cast<void*>(&nativeGetScale) },
        { "nativeSetZoom", "(JDD)V", reinterpret_cast<void*>(&nativeSetZoom) },
        { "nativeGetZoom", "(J)D", reinterpret_cast<void*>(&nativeGetZoom) },
        { "nativeSetLonLatZoom", "(JLcom/mapbox/mapboxgl/lib/LonLatZoom;D)V", reinterpret_cast<void*>(&nativeSetLonLatZoom) },
        { "nativeGetLonLatZoom", "(J)Lcom/mapbox/mapboxgl/lib/LonLatZoom;", reinterpret_cast<void*>(&nativeGetLonLatZoom) },
        { "nativeResetZoom", "(J)V", reinterpret_cast<void*>(&nativeResetZoom) },
        { "nativeStartPanning", "(J)V", reinterpret_cast<void*>(&nativeStartScaling) },
        { "nativeStopPanning", "(J)V", reinterpret_cast<void*>(&nativeStopScaling) },
        { "nativeGetMinZoom", "(J)D", reinterpret_cast<void*>(&nativeGetMinZoom) },
        { "nativeGetMaxZoom", "(J)D", reinterpret_cast<void*>(&nativeGetMaxZoom) },
        { "nativeRotateBy", "(JDDDDD)V", reinterpret_cast<void*>(&nativeRotateBy) },
        { "nativeSetBearing", "(JDD)V", reinterpret_cast<void*>(static_cast<void JNICALL(*)(JNIEnv*,jobject,jlong,jdouble,jdouble)>(&nativeSetBearing)) },
        { "nativeSetBearing", "(JDDD)V", reinterpret_cast<void*>(static_cast<void JNICALL(*)(JNIEnv*,jobject,jlong,jdouble,jdouble,jdouble)>(&nativeSetBearing)) },
        { "nativeGetBearing", "(J)D", reinterpret_cast<void*>(&nativeGetBearing) },
        { "nativeResetNorth", "(J)V", reinterpret_cast<void*>(&nativeResetNorth) },
        { "nativeStartRotating", "(J)V", reinterpret_cast<void*>(&nativeStartRotating) },
        { "nativeStopRotating", "(J)V", reinterpret_cast<void*>(&nativeStopRotating) },
        { "nativeCanRotate", "(J)Z", reinterpret_cast<void*>(&nativeCanRotate) },
        { "nativeSetDebug", "(JZ)V", reinterpret_cast<void*>(&nativeSetDebug) },
        { "nativeToggleDebug", "(J)V", reinterpret_cast<void*>(&nativeToggleDebug) },
        { "nativeGetDebug", "(J)Z", reinterpret_cast<void*>(&nativeGetDebug) }
    }};

    if (env->RegisterNatives(native_map_view_class, methods.data(), methods.size()) < 0) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_class = reinterpret_cast<jclass>(env->NewGlobalRef(lon_lat_class));
    if (lon_lat_class == nullptr) {
        env->ExceptionDescribe();
        return JNI_ERR;
    }

    lon_lat_zoom_class = reinterpret_cast<jclass>(env->NewGlobalRef(lon_lat_zoom_class));
    if (lon_lat_zoom_class == nullptr) {
        env->ExceptionDescribe();
        env->DeleteGlobalRef(lon_lat_class);
        return JNI_ERR;
    }

    runtime_exception_class = reinterpret_cast<jclass>(env->NewGlobalRef(runtime_exception_class));
    if (runtime_exception_class == nullptr) {
        env->ExceptionDescribe();
        env->DeleteGlobalRef(lon_lat_class);
        env->DeleteGlobalRef(lon_lat_zoom_class);
        return JNI_ERR;
    }

    tree_set_class = reinterpret_cast<jclass>(env->NewGlobalRef(tree_set_class));
    if (tree_set_class == nullptr) {
        env->ExceptionDescribe();
        env->DeleteGlobalRef(lon_lat_class);
        env->DeleteGlobalRef(lon_lat_zoom_class);
        env->DeleteGlobalRef(runtime_exception_class);
        return JNI_ERR;
    }

    return JNI_VERSION_1_6;
}

extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
    LOG_VERBOSE("JNI_OnUnload");

    JNIEnv* env = nullptr;
    if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
        LOG_ERROR("GetEnv() failed");
        return;
    }

    env->DeleteGlobalRef(lon_lat_class);
    lon_lat_class = nullptr;
    lon_lat_constructor_id = nullptr;
    lon_lat_lon_id = nullptr;
    lon_lat_lat_id = nullptr;

    env->DeleteGlobalRef(lon_lat_zoom_class);
    lon_lat_zoom_class = nullptr;
    lon_lat_zoom_constructor_id = nullptr;
    lon_lat_zoom_lon_id = nullptr;
    lon_lat_zoom_lat_id = nullptr;
    lon_lat_zoom_zoom_id = nullptr;

    on_map_changed_id = nullptr;

    env->DeleteGlobalRef(runtime_exception_class);
    runtime_exception_class = nullptr;

    set_to_array_id = nullptr;

    env->DeleteGlobalRef(tree_set_class);
    tree_set_class = nullptr;
    tree_set_constructor_id = nullptr;
    tree_set_add_id = nullptr;
}

} // extern "C"