@mapsindoors/react-native-maps-indoors-mapbox 2.8.0 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +84 -1
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/com/mapsindoorsrn/MapsIndoorsPackage.java +10 -0
  4. package/android/src/main/java/com/mapsindoorsrn/core/BaseMapCacheProviders.java +94 -0
  5. package/android/src/main/java/com/mapsindoorsrn/core/DirectionsRendererModule.java +140 -49
  6. package/android/src/main/java/com/mapsindoorsrn/core/DirectionsServiceModule.java +16 -7
  7. package/android/src/main/java/com/mapsindoorsrn/core/MPDisplayRuleModule.java +1 -1
  8. package/android/src/main/java/com/mapsindoorsrn/core/MapControlModule.java +115 -20
  9. package/android/src/main/java/com/mapsindoorsrn/core/MapsIndoorsModule.java +200 -11
  10. package/android/src/main/java/com/mapsindoorsrn/core/RnGson.java +110 -0
  11. package/android/src/main/java/com/mapsindoorsrn/core/UtilsModule.java +1 -1
  12. package/android/src/main/java/com/mapsindoorsrn/core/models/DirectionsRendererOptionsModel.java +498 -0
  13. package/index.ts +25 -0
  14. package/ios/.swift-format +79 -0
  15. package/ios/core/DirectionsRendererBridge.m +11 -0
  16. package/ios/core/DirectionsRendererModule.swift +162 -1
  17. package/ios/core/DirectionsRendererOptions.swift +352 -0
  18. package/ios/core/MapsIndoorsBridge.m +14 -1
  19. package/ios/core/MapsIndoorsData.swift +1 -0
  20. package/ios/core/MapsIndoorsModule.swift +180 -21
  21. package/ios/core/UtilsModule.swift +57 -45
  22. package/package.json +1 -1
  23. package/react-native-maps-indoors-mapbox.podspec +2 -2
  24. package/specs/NativeDirectionsRendererModule.ts +3 -0
  25. package/specs/NativeMapsIndoorsModule.ts +6 -1
  26. package/src/core/EventNames.ts +3 -0
  27. package/src/core/MPDirectionsRenderer.ts +76 -1
  28. package/src/core/MPDirectionsRendererOptions.ts +287 -0
  29. package/src/core/MPError.ts +48 -1
  30. package/src/core/MPLanguageTag.ts +222 -0
  31. package/src/core/MPLegBoundaryIcons.ts +130 -0
  32. package/src/core/MPRouteAnimationType.ts +24 -0
  33. package/src/core/MPRouteArrowStyle.ts +28 -0
  34. package/src/core/MPRouteMarkerDisplayRule.ts +140 -0
  35. package/src/core/MPRouteStampType.ts +24 -0
  36. package/src/core/MPSolution.ts +33 -2
  37. package/src/core/MPStrokeStyle.ts +22 -0
  38. package/src/core/MapsIndoors.ts +155 -11
  39. package/src/core/OnBaseMapCacheProgressListener.ts +20 -0
  40. package/src/core/__tests__/MPBounds.test.ts +109 -0
  41. package/src/core/__tests__/MPFilter.test.ts +76 -0
  42. package/src/core/__tests__/MPLanguageTag.test.ts +168 -0
  43. package/src/core/__tests__/MPPoint.test.ts +82 -0
  44. package/src/core/__tests__/MPSolution.test.ts +107 -0
  45. package/src/core/__tests__/MapsIndoors.language.test.ts +180 -0
@@ -1,6 +1,7 @@
1
1
  package com.mapsindoorsrn.core;
2
2
 
3
3
  import android.graphics.Typeface;
4
+ import android.util.Log;
4
5
  import android.view.View;
5
6
 
6
7
  import androidx.annotation.NonNull;
@@ -55,7 +56,7 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
55
56
  public static final String NAME = "MapControlModule";
56
57
 
57
58
  private RCMapView mMapView;
58
- private final Gson gson = new Gson();
59
+ private final Gson gson = RnGson.create();
59
60
  private MapControl mMapControl;
60
61
  private OnMapControlReadyListener mcListener;
61
62
  private final ReactApplicationContext mCtx;
@@ -71,8 +72,26 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
71
72
  return NAME;
72
73
  }
73
74
 
75
+ /**
76
+ * Rejects with the SDK's error, serialised.
77
+ *
78
+ * Most callers are SDK callbacks, off React Native's stack, where an unhandled throw takes the
79
+ * host app down rather than failing the call. {@code MPError} carries {@code MIError.tag},
80
+ * which is a bare {@code Object}, so this is the same reflection hazard the rest of the module
81
+ * guards - and the one path where a throw would land when the call has already failed
82
+ * (MS-3983). A failure to serialise the error still has to reach the caller, so it falls back
83
+ * to the Gson-free payload rather than being dropped.
84
+ */
74
85
  private void reject(Promise promise, MIError error) {
75
- promise.reject("MapControlError", gson.toJson(MPError.fromMIError(error)));
86
+ final String errorString;
87
+ try {
88
+ errorString = gson.toJson(MPError.fromMIError(error));
89
+ } catch (Exception | StackOverflowError t) {
90
+ RnGson.rejectSerialisationFailure(promise, "MapControlError",
91
+ "Could not serialise error " + (error != null ? error.code : "null") + ": " + t);
92
+ return;
93
+ }
94
+ promise.reject("MapControlError", errorString);
76
95
  }
77
96
 
78
97
  public void setOnMapControlReadyListener(OnMapControlReadyListener listener) {
@@ -389,8 +408,21 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
389
408
  }
390
409
 
391
410
  OnLiveLocationUpdateListener listener = location -> {
411
+ // Live updates arrive on the SDK's thread and there is no promise to fail, so a
412
+ // throw here would take the host app down. Drop the update and log it (MS-3983).
413
+ // StackOverflowError is named because it is the failure being guarded and it is an
414
+ // Error, not an Exception; OutOfMemoryError and the linkage Errors are left to
415
+ // propagate, since nothing here can recover from them.
416
+ final String locationString;
417
+ try {
418
+ locationString = gson.toJson(location);
419
+ } catch (Exception | StackOverflowError t) {
420
+ Log.e(NAME, "Dropping a live location update that could not be serialised", t);
421
+ return;
422
+ }
423
+
392
424
  WritableMap params = Arguments.createMap();
393
- params.putString("location", gson.toJson(location));
425
+ params.putString("location", locationString);
394
426
  emit(Listener.ON_LIVE_LOCATION_UPDATE, params);
395
427
  };
396
428
 
@@ -432,7 +464,18 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
432
464
 
433
465
  @ReactMethod
434
466
  public void getCurrentCameraPosition(final Promise promise) {
435
- mCtx.runOnUiQueueThread(() -> promise.resolve(gson.toJson(mMapView.getCurrentCameraPosition())));
467
+ mCtx.runOnUiQueueThread(() -> {
468
+ // Runs later on the UI queue, off React Native's stack (MS-3983).
469
+ final String cameraPositionString;
470
+ try {
471
+ cameraPositionString = gson.toJson(mMapView.getCurrentCameraPosition());
472
+ } catch (Exception | StackOverflowError t) {
473
+ RnGson.rejectSerialisationFailure(promise, "MapControlError",
474
+ "Could not serialise the camera position: " + t);
475
+ return;
476
+ }
477
+ promise.resolve(cameraPositionString);
478
+ });
436
479
  }
437
480
 
438
481
  @ReactMethod
@@ -464,9 +507,19 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
464
507
  public void setOnMapClickListener(boolean setup, @Nullable final Boolean consumeEvent) {
465
508
  if (setup) {
466
509
  mMapControl.setOnMapClickListener((latLng, locations) -> {
467
- WritableMap params = Arguments.createMap();
468
- params.putString("point", gson.toJson(new MPPoint(latLng)));
469
- params.putString("locations", gson.toJson(locations));
510
+ // Emitted from an SDK callback, off React Native's stack: a throw here takes the
511
+ // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
512
+ // The locations list is the payload that crashed in MS-3983, and this fires on every
513
+ // tap rather than once per search. Consumption is unaffected by a dropped event:
514
+ // the host app configured it, so report it either way.
515
+ final WritableMap params = Arguments.createMap();
516
+ try {
517
+ params.putString("point", gson.toJson(new MPPoint(latLng)));
518
+ params.putString("locations", gson.toJson(locations));
519
+ } catch (Exception | StackOverflowError t) {
520
+ Log.e(NAME, "Dropping a map click that could not be serialised", t);
521
+ return Boolean.TRUE.equals(consumeEvent);
522
+ }
470
523
  emit(Listener.ON_MAP_CLICK, params);
471
524
  return Boolean.TRUE.equals(consumeEvent);
472
525
  });
@@ -479,8 +532,15 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
479
532
  public void setOnLocationSelectedListener(boolean setup, @Nullable final Boolean consumeEvent) {
480
533
  if (setup) {
481
534
  mMapControl.setOnLocationSelectedListener(location -> {
482
- WritableMap params = Arguments.createMap();
483
- params.putString("location", gson.toJson(location));
535
+ // Emitted from an SDK callback, off React Native's stack: a throw here takes the
536
+ // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
537
+ final WritableMap params = Arguments.createMap();
538
+ try {
539
+ params.putString("location", gson.toJson(location));
540
+ } catch (Exception | StackOverflowError t) {
541
+ Log.e(NAME, "Dropping a location selection that could not be serialised", t);
542
+ return Boolean.TRUE.equals(consumeEvent);
543
+ }
484
544
  emit(Listener.ON_LOCATION_SELECTED, params);
485
545
  return Boolean.TRUE.equals(consumeEvent);
486
546
  });
@@ -493,8 +553,15 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
493
553
  public void setOnCurrentVenueChangedListener(boolean setup) {
494
554
  if (setup) {
495
555
  mMapControl.setOnCurrentVenueChangedListener(venue -> {
496
- WritableMap params = Arguments.createMap();
497
- params.putString("venue", gson.toJson(venue));
556
+ // Emitted from an SDK callback, off React Native's stack: a throw here takes the
557
+ // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
558
+ final WritableMap params = Arguments.createMap();
559
+ try {
560
+ params.putString("venue", gson.toJson(venue));
561
+ } catch (Exception | StackOverflowError t) {
562
+ Log.e(NAME, "Dropping a venue change that could not be serialised", t);
563
+ return;
564
+ }
498
565
  emit(Listener.ON_VENUE_FOUND_AT_CAMERA_TARGET, params);
499
566
  });
500
567
  } else {
@@ -506,8 +573,15 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
506
573
  public void setOnCurrentBuildingChangedListener(boolean setup) {
507
574
  if (setup) {
508
575
  mMapControl.setOnCurrentBuildingChangedListener(building -> {
509
- WritableMap params = Arguments.createMap();
510
- params.putString("building", gson.toJson(building));
576
+ // Emitted from an SDK callback, off React Native's stack: a throw here takes the
577
+ // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
578
+ final WritableMap params = Arguments.createMap();
579
+ try {
580
+ params.putString("building", gson.toJson(building));
581
+ } catch (Exception | StackOverflowError t) {
582
+ Log.e(NAME, "Dropping a building change that could not be serialised", t);
583
+ return;
584
+ }
511
585
  emit(Listener.ON_BUILDING_FOUND_AT_CAMERA_TARGET, params);
512
586
  });
513
587
  } else {
@@ -640,9 +714,16 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
640
714
 
641
715
  @Override
642
716
  public void onFloorUpdate(@Nullable MPBuilding building, int floorIndex) {
643
- WritableMap params = Arguments.createMap();
644
- params.putString("floorIndex", gson.toJson(floorIndex));
645
- params.putString("building", gson.toJson(building));
717
+ // Called by the SDK, off React Native's stack: a throw here takes the host app down and
718
+ // there is no promise to fail. Drop the event and log it (MS-3983).
719
+ final WritableMap params = Arguments.createMap();
720
+ try {
721
+ params.putString("floorIndex", gson.toJson(floorIndex));
722
+ params.putString("building", gson.toJson(building));
723
+ } catch (Exception | StackOverflowError t) {
724
+ Log.e(NAME, "Dropping a floor update that could not be serialised", t);
725
+ return;
726
+ }
646
727
  emit(Listener.ON_FLOOR_UPDATE, params);
647
728
  }
648
729
 
@@ -688,10 +769,17 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
688
769
 
689
770
  @Override
690
771
  public void setList(@Nullable List<MPFloor> list) {
691
- WritableMap params = Arguments.createMap();
772
+ // Called by the SDK, off React Native's stack: a throw here takes the host app down and
773
+ // there is no promise to fail. Drop the event and log it (MS-3983).
774
+ final WritableMap params = Arguments.createMap();
692
775
  params.putString("method", "setList");
693
776
  if (list != null) {
694
- params.putString("list", gson.toJson(list));
777
+ try {
778
+ params.putString("list", gson.toJson(list));
779
+ } catch (Exception | StackOverflowError t) {
780
+ Log.e(NAME, "Dropping a floor list that could not be serialised", t);
781
+ return;
782
+ }
695
783
  }
696
784
 
697
785
  emit(Listener.FLOOR_SELECTOR, params);
@@ -708,9 +796,16 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
708
796
 
709
797
  @Override
710
798
  public void setSelectedFloor(@NonNull MPFloor floor) {
711
- WritableMap params = Arguments.createMap();
799
+ // Called by the SDK, off React Native's stack: a throw here takes the host app down and
800
+ // there is no promise to fail. Drop the event and log it (MS-3983).
801
+ final WritableMap params = Arguments.createMap();
712
802
  params.putString("method", "setSelectedFloor");
713
- params.putString("floor", gson.toJson(floor));
803
+ try {
804
+ params.putString("floor", gson.toJson(floor));
805
+ } catch (Exception | StackOverflowError t) {
806
+ Log.e(NAME, "Dropping a floor selection that could not be serialised", t);
807
+ return;
808
+ }
714
809
  emit(Listener.FLOOR_SELECTOR, params);
715
810
  }
716
811
 
@@ -1,14 +1,19 @@
1
1
  package com.mapsindoorsrn.core;
2
2
 
3
+ import androidx.annotation.Nullable;
4
+
3
5
  import com.facebook.react.bridge.Arguments;
4
6
  import com.facebook.react.bridge.Promise;
5
7
  import com.facebook.react.bridge.ReactApplicationContext;
6
8
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
7
9
  import com.facebook.react.bridge.ReactMethod;
8
10
  import com.facebook.react.bridge.ReadableArray;
11
+ import com.facebook.react.bridge.WritableMap;
9
12
  import com.facebook.react.module.annotations.ReactModule;
13
+ import com.facebook.react.modules.core.DeviceEventManagerModule;
10
14
  import com.google.gson.Gson;
11
15
  import com.google.gson.reflect.TypeToken;
16
+ import com.mapsindoors.core.MPBaseMapCacheRegionListener;
12
17
  import com.mapsindoors.core.MPBuilding;
13
18
  import com.mapsindoors.core.MPBuildingCollection;
14
19
  import com.mapsindoors.core.MPCategory;
@@ -29,6 +34,7 @@ import com.mapsindoors.core.MPVenue;
29
34
  import com.mapsindoors.core.MPVenueCollection;
30
35
  import com.mapsindoors.core.MapsIndoors;
31
36
  import com.mapsindoors.core.errors.MIError;
37
+ import com.mapsindoors.core.models.MPIMapProviderBaseMapCache;
32
38
  import com.mapsindoors.core.models.MPMapStyle;
33
39
  import com.mapsindoorsrn.core.models.Filter;
34
40
  import com.mapsindoorsrn.core.models.MPError;
@@ -45,13 +51,24 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
45
51
  public static final String NAME = "MapsIndoorsModule";
46
52
 
47
53
  private final ReactApplicationContext reactContext;
48
- private final Gson gson = new Gson();
54
+ private final Gson gson = RnGson.create();
49
55
 
50
56
  private PositionProvider positionProvider;
51
57
 
52
58
  public MapsIndoorsModule(ReactApplicationContext reactContext) {
53
59
  super(reactContext);
54
60
  this.reactContext = reactContext;
61
+
62
+ // Initialized here rather than lazily inside the caching methods, because MPTileProvider asks
63
+ // MPDataSetCacheManager whether a solution has offline tiles when the map builds its floor-tile
64
+ // overlay - and getInstance() throws outright when nothing has initialized it. sIsInitialized is
65
+ // static, so it is false again on every cold start: an app that cached tiles in an earlier run
66
+ // and then launches offline would build its tile overlay against the network URL and render no
67
+ // MapsIndoors tiles at all. Doing it in the constructor puts it before any map can exist, and
68
+ // costs a file-path setup - the datasets manifest is only read on first getInstance().
69
+ if (!MPDataSetCacheManager.isInitialized()) {
70
+ MPDataSetCacheManager.initialize(reactContext);
71
+ }
55
72
  }
56
73
 
57
74
  @Override
@@ -59,8 +76,26 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
59
76
  return NAME;
60
77
  }
61
78
 
79
+ /**
80
+ * Rejects with the SDK's error, serialised.
81
+ *
82
+ * Most callers are SDK callbacks, off React Native's stack, where an unhandled throw takes the
83
+ * host app down rather than failing the call. {@code MPError} carries {@code MIError.tag},
84
+ * which is a bare {@code Object}, so this is the same reflection hazard the rest of the module
85
+ * guards - and the one path where a throw would land when the call has already failed
86
+ * (MS-3983). A failure to serialise the error still has to reach the caller, so it falls back
87
+ * to the Gson-free payload rather than being dropped.
88
+ */
62
89
  private void reject(Promise promise, MIError error) {
63
- promise.reject("MapsIndoorsError", gson.toJson(MPError.fromMIError(error)));
90
+ final String errorString;
91
+ try {
92
+ errorString = gson.toJson(MPError.fromMIError(error));
93
+ } catch (Exception | StackOverflowError t) {
94
+ RnGson.rejectSerialisationFailure(promise, "MapsIndoorsError",
95
+ "Could not serialise error " + (error != null ? error.code : "null") + ": " + t);
96
+ return;
97
+ }
98
+ promise.reject("MapsIndoorsError", errorString);
64
99
  }
65
100
 
66
101
  @ReactMethod
@@ -152,7 +187,7 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
152
187
  String locationsString = gson.toJson(locations);
153
188
  promise.resolve(locationsString);
154
189
  } else {
155
- reject(promise, new MIError(MIError.UNKNOWN_ERROR, "Cannot fetch categories, try waiting until MapsIndoors is Ready"));
190
+ reject(promise, new MIError(MIError.UNKNOWN_ERROR, "Cannot fetch locations, try waiting until MapsIndoors is Ready"));
156
191
  }
157
192
  }
158
193
 
@@ -180,7 +215,12 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
180
215
 
181
216
  @ReactMethod
182
217
  public void getDefaultLanguage(final Promise promise) {
183
- promise.resolve(MapsIndoors.getDefaultLanguage());
218
+ String defaultLanguage = MapsIndoors.getDefaultLanguage();
219
+ if (defaultLanguage != null) {
220
+ promise.resolve(defaultLanguage);
221
+ } else {
222
+ reject(promise, new MIError(MIError.UNKNOWN_ERROR, "No default language is available, try waiting until MapsIndoors is Ready"));
223
+ }
184
224
  }
185
225
 
186
226
  @ReactMethod
@@ -220,8 +260,11 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
220
260
  @ReactMethod
221
261
  public void getSolution(final Promise promise) {
222
262
  MPSolution solution = MapsIndoors.getSolution();
223
- String solutionString = solution != null ? gson.toJson(solution): null;
224
- promise.resolve(solutionString);
263
+ if (solution != null) {
264
+ promise.resolve(gson.toJson(solution));
265
+ } else {
266
+ reject(promise, new MIError(MIError.UNKNOWN_ERROR, "No solution is available, try waiting until MapsIndoors is Ready"));
267
+ }
225
268
  }
226
269
 
227
270
  @ReactMethod
@@ -230,12 +273,29 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
230
273
  MPFilter mpFilter = gson.fromJson(filter, Filter.class).toMPFilter();
231
274
 
232
275
  MapsIndoors.getLocationsAsync(mpQuery, mpFilter, (list, miError) -> {
233
- if (miError == null) {
234
- String locationsString = gson.toJson(list);
235
- promise.resolve(locationsString);
236
- } else {
276
+ if (miError != null) {
237
277
  reject(promise, miError);
278
+ return;
279
+ }
280
+
281
+ // Serialising here runs on the SDK's thread, outside React Native's try/catch, so an
282
+ // unhandled throw takes the host app down instead of failing this call (MS-3983).
283
+ // StackOverflowError is named because it is the failure being guarded and it is an
284
+ // Error, not an Exception; OutOfMemoryError and the linkage Errors are left to
285
+ // propagate, since nothing here can recover from them.
286
+ //
287
+ // Only the serialisation goes inside the guard. A throw from resolve - an already
288
+ // settled promise, a torn-down bridge - would otherwise be answered by rejecting the
289
+ // same promise from the catch, which throws again on this thread, uncaught.
290
+ final String locationsString;
291
+ try {
292
+ locationsString = gson.toJson(list);
293
+ } catch (Exception | StackOverflowError t) {
294
+ RnGson.rejectSerialisationFailure(promise, "MapsIndoorsError",
295
+ "Could not serialise the locations result: " + t);
296
+ return;
238
297
  }
298
+ promise.resolve(locationsString);
239
299
  });
240
300
  }
241
301
 
@@ -347,7 +407,19 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
347
407
  @ReactMethod
348
408
  public void reverseGeoCode(String pointString, final Promise promise) {
349
409
  MPPoint point = gson.fromJson(pointString, MPPoint.class);
350
- MapsIndoors.reverseGeoCode(point, mpGeoCodeResult -> promise.resolve(gson.toJson(mpGeoCodeResult)));
410
+ MapsIndoors.reverseGeoCode(point, mpGeoCodeResult -> {
411
+ // Not on React Native's stack either, and the same shape and Error handling - see
412
+ // getLocationsAsync.
413
+ final String geoCodeString;
414
+ try {
415
+ geoCodeString = gson.toJson(mpGeoCodeResult);
416
+ } catch (Exception | StackOverflowError t) {
417
+ RnGson.rejectSerialisationFailure(promise, "MapsIndoorsError",
418
+ "Could not serialise the reverse-geocode result: " + t);
419
+ return;
420
+ }
421
+ promise.resolve(geoCodeString);
422
+ });
351
423
  }
352
424
 
353
425
  @ReactMethod
@@ -410,4 +482,121 @@ public class MapsIndoorsModule extends ReactContextBaseJavaModule {
410
482
  }));
411
483
  MPDataSetCacheManager.getInstance().synchronizeDataSets(Collections.singletonList(cache));
412
484
  }
485
+
486
+ @ReactMethod
487
+ public void isBaseMapCachingSupported(Promise promise) {
488
+ final MPIMapProviderBaseMapCache provider = BaseMapCacheProviders.ensureRegistered(reactContext);
489
+
490
+ // Resolved rather than rejected when nothing is registered: "can this app cache base-map tiles"
491
+ // has a true answer either way, and false is the useful one for a caller deciding whether to
492
+ // offer offline base maps at all.
493
+ promise.resolve(provider != null && provider.isBaseMapCachingSupported());
494
+ }
495
+
496
+ @ReactMethod
497
+ public void setBaseMapTilesEnabled(boolean enabled, String apiKey, Promise promise) {
498
+ // No ensureRegistered() here, unlike the two methods either side of it: flagging a dataset needs
499
+ // no provider, and synchronizeBaseMapTiles registers one before it needs it. The manager itself
500
+ // is already initialized by this module's constructor.
501
+ final MPDataSetCacheManager manager = MPDataSetCacheManager.getInstance();
502
+ final MPDataSetCache cache = manager.getDataSetByID(apiKey);
503
+
504
+ if (cache == null) {
505
+ // Managed from here on, with the same FULL scope cacheData() uses: there is nothing to flag
506
+ // otherwise, and base-map caching needs the dataset's venues on disk to know what to cache
507
+ // around.
508
+ if (manager.addDataSetWithCachingScope(apiKey, MPDataSetCacheScope.FULL, enabled) == null) {
509
+ // Reported rather than resolved: the flag is not set, so a later synchronizeBaseMapTiles
510
+ // would fail as BASEMAP_CACHE_NOT_REGISTERED or simply cache nothing, displaced from the
511
+ // cause. iOS rejects on the same call.
512
+ reject(promise, new MIError(MIError.UNKNOWN_ERROR, "Unable to manage dataset '" + apiKey + "'"));
513
+ return;
514
+ }
515
+ } else {
516
+ cache.setBaseMapTilesEnabled(enabled);
517
+ }
518
+
519
+ promise.resolve(null);
520
+ }
521
+
522
+ @ReactMethod
523
+ public void synchronizeBaseMapTiles(@Nullable ReadableArray apiKeys, Promise promise) {
524
+ BaseMapCacheProviders.ensureRegistered(reactContext);
525
+
526
+ final MPDataSetCacheManager manager = MPDataSetCacheManager.getInstance();
527
+ final List<MPDataSetCache> caches = new ArrayList<>();
528
+
529
+ if (apiKeys != null) {
530
+ for (int i = 0; i < apiKeys.size(); i++) {
531
+ final String apiKey = apiKeys.getString(i);
532
+ final MPDataSetCache cache = manager.getDataSetByID(apiKey);
533
+
534
+ if (cache == null) {
535
+ // Rejected rather than skipped: an explicit list is an explicit instruction, and
536
+ // silently caching nothing for a key the caller named is the harder failure to spot.
537
+ reject(promise, new MIError(MIError.BASEMAP_CACHE_NOT_REGISTERED,
538
+ "No dataset is managed for '" + apiKey + "', so base-map tiles cannot be cached for it"));
539
+ return;
540
+ }
541
+
542
+ caches.add(cache);
543
+ }
544
+ }
545
+
546
+ final MPBaseMapCacheRegionListener listener = new MPBaseMapCacheRegionListener() {
547
+ @Override
548
+ public void onProgress(double fraction) {
549
+ // Guarded, unlike every other emitter in this module: those fire from map or UI
550
+ // callbacks, so a live React context is a given. This one fires from a background
551
+ // download owned by MPDataSetCacheManager - a static singleton - so it outlives a
552
+ // context teardown, and a Metro reload part-way through a multi-minute sync would
553
+ // otherwise call getJSModule against a bridge that is gone, once per tick.
554
+ if (!reactContext.hasActiveReactInstance()) {
555
+ return;
556
+ }
557
+
558
+ final WritableMap params = Arguments.createMap();
559
+ params.putDouble("progress", fraction);
560
+ // Emitted unconditionally, where iOS gates on whether JavaScript is listening. Both are
561
+ // right for their platform: RCTDeviceEventEmitter with nothing listening is harmless,
562
+ // whereas iOS's RCTEventEmitter logs a warning per event.
563
+ //
564
+ // "onBaseMapCacheProgress" is core's EventNames.onBaseMapCacheProgress, also spelled out
565
+ // in iOS's MapsIndoorsData.Event. A typo here delivers no progress rather than failing.
566
+ reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
567
+ .emit("onBaseMapCacheProgress", params);
568
+ }
569
+
570
+ @Override
571
+ public void onComplete(@Nullable MIError error) {
572
+ if (error != null) {
573
+ reject(promise, error);
574
+ } else {
575
+ promise.resolve(null);
576
+ }
577
+ }
578
+ };
579
+
580
+ if (apiKeys == null) {
581
+ manager.synchronizeBaseMapTiles(listener);
582
+ } else {
583
+ manager.synchronizeBaseMapTiles(caches, listener);
584
+ }
585
+ }
586
+
587
+ /**
588
+ * Required by React Native's NativeEventEmitter, which MapsIndoors.synchronizeBaseMapTiles()
589
+ * subscribes to for progress. The events themselves are emitted through RCTDeviceEventEmitter, so
590
+ * there is nothing to do here.
591
+ */
592
+ @ReactMethod
593
+ public void addListener(String eventName) {
594
+ }
595
+
596
+ /**
597
+ * Counterpart to {@link #addListener(String)}. See its note.
598
+ */
599
+ @ReactMethod
600
+ public void removeListeners(Integer count) {
601
+ }
413
602
  }
@@ -0,0 +1,110 @@
1
+ package com.mapsindoorsrn.core;
2
+
3
+ import android.graphics.Bitmap;
4
+
5
+ import com.facebook.react.bridge.Promise;
6
+ import com.google.gson.ExclusionStrategy;
7
+ import com.google.gson.FieldAttributes;
8
+ import com.google.gson.Gson;
9
+ import com.google.gson.GsonBuilder;
10
+ import com.mapsindoors.core.errors.MIError;
11
+
12
+ import org.json.JSONException;
13
+ import org.json.JSONObject;
14
+
15
+ import java.lang.ref.Reference;
16
+ import java.nio.Buffer;
17
+
18
+ /**
19
+ * The Gson instance every React module uses to put SDK model objects on the bridge.
20
+ *
21
+ * A bare {@code new Gson()} serialises by reflection, and reflection does not stop at the SDK's
22
+ * public API. {@code MPLocation} holds a non-transient {@code MPIcon} whose {@code IconLayer}
23
+ * holds an {@code android.graphics.Bitmap}; a Bitmap is native-backed, and the buffer behind it
24
+ * carries a {@code sun.misc.Cleaner}. Every Cleaner alive in the process is a node in one
25
+ * doubly-linked list joined by {@code next}/{@code prev}, so reflecting into one walks all of
26
+ * them. The serialised depth then tracks how many native buffers the *host app* has alive rather
27
+ * than anything about the map data - which is why this reproduces on a physical device with a
28
+ * real app around it and not on an emulator (MS-3983).
29
+ *
30
+ * Excluding these types is safe for the wire contract: the JavaScript models never read a
31
+ * serialised bitmap. {@code MPLocation.imageUrl} comes from {@code properties.imageUrl}, a string.
32
+ */
33
+ final class RnGson {
34
+
35
+ private RnGson() {
36
+ }
37
+
38
+ static Gson create() {
39
+ return new GsonBuilder()
40
+ .addSerializationExclusionStrategy(new RuntimeInternalsExclusion())
41
+ .create();
42
+ }
43
+
44
+ /**
45
+ * Rejects {@code promise} with a payload built without Gson.
46
+ *
47
+ * The ordinary reject paths serialise an {@code MPError}, whose {@code tag} is a bare
48
+ * {@code Object} carrying whatever the SDK put in {@code MIError.tag}. Recovering from a
49
+ * serialisation failure by reflecting into another untyped field risks a second throw, which
50
+ * would escape the catch and take the host app down - the outcome the guards exist to prevent.
51
+ * {@code JSONObject} does no reflection, so this path cannot fail the same way.
52
+ *
53
+ * <p>The shape matches {@code MPError}'s own {@code @SerializedName} values, so {@code
54
+ * MPError.parse} on the TypeScript side reads it like any other error.</p>
55
+ *
56
+ * @param code the module's own reject code, so a serialisation failure is reported under the
57
+ * same code as that module's other rejections
58
+ */
59
+ static void rejectSerialisationFailure(Promise promise, String code, String message) {
60
+ JSONObject error = new JSONObject();
61
+ try {
62
+ error.put("code", MIError.UNKNOWN_ERROR);
63
+ error.put("message", message);
64
+ } catch (JSONException ignored) {
65
+ // Only thrown for a NaN or infinite value, neither of which can occur here.
66
+ }
67
+ promise.reject(code, error.toString());
68
+ }
69
+
70
+ /**
71
+ * Cuts the paths out of the SDK's models and into the runtime.
72
+ *
73
+ * {@link Reference} is what actually stops MS-3983: {@code sun.misc.Cleaner} extends
74
+ * {@code PhantomReference}, so this clause is what breaks the cleaner chain. The rest close
75
+ * the neighbouring doors - a Bitmap or Buffer field is the usual way in, and a Thread or
76
+ * ClassLoader reference reaches most of the runtime in a few hops.
77
+ */
78
+ private static final class RuntimeInternalsExclusion implements ExclusionStrategy {
79
+
80
+ @Override
81
+ public boolean shouldSkipField(FieldAttributes f) {
82
+ return isRuntimeInternal(f.getDeclaredClass());
83
+ }
84
+
85
+ @Override
86
+ public boolean shouldSkipClass(Class<?> clazz) {
87
+ return isRuntimeInternal(clazz);
88
+ }
89
+
90
+ private static boolean isRuntimeInternal(Class<?> raw) {
91
+ if (Reference.class.isAssignableFrom(raw)
92
+ || Bitmap.class.isAssignableFrom(raw)
93
+ || Buffer.class.isAssignableFrom(raw)
94
+ || Thread.class.isAssignableFrom(raw)
95
+ || ClassLoader.class.isAssignableFrom(raw)
96
+ || Class.class == raw) {
97
+ return true;
98
+ }
99
+
100
+ final String name = raw.getName();
101
+
102
+ return name.startsWith("sun.")
103
+ || name.startsWith("jdk.")
104
+ || name.startsWith("dalvik.")
105
+ || name.startsWith("libcore.")
106
+ || name.startsWith("java.lang.invoke.")
107
+ || name.startsWith("java.lang.ref.");
108
+ }
109
+ }
110
+ }
@@ -30,7 +30,7 @@ import java.util.Locale;
30
30
  public class UtilsModule extends ReactContextBaseJavaModule {
31
31
  public static final String NAME = "UtilsModule";
32
32
 
33
- private final Gson gson = new Gson();
33
+ private final Gson gson = RnGson.create();
34
34
 
35
35
  public UtilsModule(ReactApplicationContext reactApplicationContext) {
36
36
  super(reactApplicationContext);