@mapsindoors/react-native-maps-indoors-mapbox 2.9.1 → 2.10.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.
@@ -55,10 +55,12 @@ import java.util.Map;
55
55
  public class MapControlModule extends ReactContextBaseJavaModule implements MPCameraEventListener, OnFloorUpdateListener, MPFloorSelectorInterface {
56
56
  public static final String NAME = "MapControlModule";
57
57
 
58
- private RCMapView mMapView;
58
+ // Volatile: written on the UI thread, read from React Native's module thread too.
59
+ private volatile RCMapView mMapView;
59
60
  private final Gson gson = RnGson.create();
60
- private MapControl mMapControl;
61
+ private volatile MapControl mMapControl;
61
62
  private OnMapControlReadyListener mcListener;
63
+ @Nullable private Runnable mOnMapControlDestroyed;
62
64
  private final ReactApplicationContext mCtx;
63
65
 
64
66
  public MapControlModule(@NonNull ReactApplicationContext reactContext) {
@@ -102,14 +104,94 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
102
104
  mCtx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(listener.method, data);
103
105
  }
104
106
 
107
+ public void setOnMapControlDestroyedListener(@Nullable Runnable listener) {
108
+ mOnMapControlDestroyed = listener;
109
+ }
110
+
105
111
  public void setView(RCMapView view) {
106
112
  mMapView = view;
107
113
  }
108
114
 
115
+ /**
116
+ * Releases {@code view} and its MapControl once React Native unmounts it. Call on the UI thread,
117
+ * before the map itself is destroyed.
118
+ *
119
+ * Left alive, the MapControl stays registered with the SDK, which keeps it and its map reachable
120
+ * and has it react to the next map's display rule changes (SPEX-2885). A no-op for any other view,
121
+ * as a new map may mount before the old one is dropped.
122
+ */
123
+ public void onViewDropped(RCMapView view) {
124
+ if (mMapView != view) {
125
+ return;
126
+ }
127
+ destroyMapControl();
128
+ mMapView = null;
129
+ }
130
+
131
+ private void destroyMapControl() {
132
+ MapControl mapControl = mMapControl;
133
+ if (mapControl == null) {
134
+ return;
135
+ }
136
+ mMapControl = null;
137
+ liveLocationUpdateListeners.clear();
138
+ // Set by the destroyed MapControl, which it would keep reachable.
139
+ onFloorSelectionChangedListener = null;
140
+ mapControl.onDestroy();
141
+ if (mOnMapControlDestroyed != null) {
142
+ mOnMapControlDestroyed.run();
143
+ }
144
+ }
145
+
146
+ /**
147
+ * The MapControl, or {@code null} once {@code promise} is rejected: there is none before
148
+ * {@code initMapControl}, nor after its view is dropped.
149
+ */
150
+ @Nullable
151
+ private MapControl requireMapControl(@Nullable Promise promise) {
152
+ MapControl mapControl = mMapControl;
153
+ if (mapControl == null && promise != null) {
154
+ promise.reject("MapControlError", "mapControl not available");
155
+ }
156
+ return mapControl;
157
+ }
158
+
159
+ /**
160
+ * Logs a call that has no promise to reject: without a MapControl, JS would otherwise believe the
161
+ * listener was set.
162
+ */
163
+ private void warnNoMapControl(String method) {
164
+ Log.w(NAME, method + ": mapControl not available, ignored");
165
+ }
166
+
167
+ private interface MapControlAction {
168
+ void run(@NonNull MapControl mapControl);
169
+ }
170
+
171
+ /**
172
+ * Runs {@code action} on the UI thread with the MapControl. Read there, where it is destroyed, so
173
+ * it cannot go between the check and the call. Without one, rejects {@code promise} if given.
174
+ */
175
+ private void runOnUiQueueThread(@Nullable Promise promise, MapControlAction action) {
176
+ mCtx.runOnUiQueueThread(() -> {
177
+ MapControl mapControl = requireMapControl(promise);
178
+ if (mapControl != null) {
179
+ action.run(mapControl);
180
+ }
181
+ });
182
+ }
183
+
109
184
  @ReactMethod
110
185
  public void initMapControl(ReadableMap config, final Promise promise) {
111
186
  getCurrentActivity().runOnUiThread(()-> {
112
- MPIMapConfig mpMapConfig = mMapView.parseConfig(config, getCurrentActivity());
187
+ RCMapView mapView = mMapView;
188
+ if (mapView == null) {
189
+ promise.reject("MapControlError", "MapView not available");
190
+ return;
191
+ }
192
+ // Replaced below: destroyed first so it unregisters from the SDK.
193
+ destroyMapControl();
194
+ MPIMapConfig mpMapConfig = mapView.parseConfig(config, getCurrentActivity());
113
195
  MapControl.create(mpMapConfig, (mapControl, miError) -> {
114
196
  mcListener.onMapControlReady(mapControl,miError);
115
197
  if (mapControl != null) {
@@ -130,8 +212,9 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
130
212
  locations.add(MapsIndoors.getLocationById(locationId));
131
213
  }
132
214
  MPFilterBehavior filterBehavior = gson.fromJson(filterBehaviorString, MPFilterBehavior.class);
133
- if (mMapControl != null) {
134
- mMapControl.setFilter(locations, filterBehavior);
215
+ MapControl mapControl = mMapControl;
216
+ if (mapControl != null) {
217
+ mapControl.setFilter(locations, filterBehavior);
135
218
  promise.resolve(true);
136
219
  }else {
137
220
  promise.resolve(false);
@@ -142,8 +225,9 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
142
225
  public void setFilter(String filterString, String filterBehaviorString, final Promise promise) {
143
226
  MPFilter filter = gson.fromJson(filterString, Filter.class).toMPFilter();
144
227
  MPFilterBehavior filterBehavior = gson.fromJson(filterBehaviorString, MPFilterBehavior.class);
145
- if (mMapControl != null && filter != null) {
146
- mMapControl.setFilter(filter, filterBehavior, null);
228
+ MapControl mapControl = mMapControl;
229
+ if (mapControl != null && filter != null) {
230
+ mapControl.setFilter(filter, filterBehavior, null);
147
231
  promise.resolve(true);
148
232
  }else {
149
233
  promise.resolve(false);
@@ -152,12 +236,13 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
152
236
 
153
237
  @ReactMethod
154
238
  public void clearFilter(final Promise promise) {
155
- if (mMapControl != null) {
156
- mCtx.runOnUiQueueThread(() -> {
157
- mMapControl.clearFilter();
158
- promise.resolve(null);
159
- });
160
- }
239
+ mCtx.runOnUiQueueThread(() -> {
240
+ MapControl mapControl = mMapControl;
241
+ if (mapControl != null) {
242
+ mapControl.clearFilter();
243
+ }
244
+ promise.resolve(null);
245
+ });
161
246
  }
162
247
 
163
248
  @ReactMethod
@@ -168,8 +253,9 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
168
253
  locations.add(MapsIndoors.getLocationById(locationId));
169
254
  }
170
255
  MPHighlightBehavior highlightBehavior = gson.fromJson(highlightBehaviorString, MPHighlightBehavior.class);
171
- if (mMapControl != null) {
172
- mMapControl.setHighlight(locations, highlightBehavior);
256
+ MapControl mapControl = mMapControl;
257
+ if (mapControl != null) {
258
+ mapControl.setHighlight(locations, highlightBehavior);
173
259
  promise.resolve(true);
174
260
  }else {
175
261
  promise.resolve(false);
@@ -178,20 +264,22 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
178
264
 
179
265
  @ReactMethod
180
266
  public void clearHighlight(final Promise promise) {
181
- if (mMapControl != null) {
182
- mCtx.runOnUiQueueThread(() -> {
183
- mMapControl.clearHighlight();
184
- promise.resolve(null);
185
- });
186
- }
267
+ mCtx.runOnUiQueueThread(() -> {
268
+ MapControl mapControl = mMapControl;
269
+ if (mapControl != null) {
270
+ mapControl.clearHighlight();
271
+ }
272
+ promise.resolve(null);
273
+ });
187
274
  }
188
275
 
189
276
  @ReactMethod
190
277
  public void showUserPosition(boolean show, final Promise promise) {
191
- if (mMapControl != null) {
192
- mMapControl.showUserPosition(show);
193
- promise.resolve(null);
278
+ MapControl mapControl = mMapControl;
279
+ if (mapControl != null) {
280
+ mapControl.showUserPosition(show);
194
281
  }
282
+ promise.resolve(null);
195
283
  }
196
284
 
197
285
  @ReactMethod
@@ -200,36 +288,36 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
200
288
  case "MPLocation": {
201
289
  MPLocation location = gson.fromJson(entityString, Location.class).toMPLocation();
202
290
  if (maxZoom != -999) {
203
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(location, maxZoom));
291
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(location, maxZoom));
204
292
  }else {
205
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(location));
293
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(location));
206
294
  }
207
295
  break;
208
296
  }
209
297
  case "MPFloor": {
210
298
  MPFloor floor = gson.fromJson(entityString, MPFloor.class);
211
299
  if (maxZoom != -999) {
212
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(floor, maxZoom));
300
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(floor, maxZoom));
213
301
  }else {
214
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(floor));
302
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(floor));
215
303
  }
216
304
  break;
217
305
  }
218
306
  case "MPBuilding": {
219
307
  MPBuilding building = gson.fromJson(entityString, MPBuilding.class);
220
308
  if (maxZoom != -999) {
221
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(building, maxZoom));
309
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(building, maxZoom));
222
310
  }else {
223
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(building));
311
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(building));
224
312
  }
225
313
  break;
226
314
  }
227
315
  case "MPVenue": {
228
316
  MPVenue venue = gson.fromJson(entityString, MPVenue.class);
229
317
  if (maxZoom != -999) {
230
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(venue, maxZoom));
318
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(venue, maxZoom));
231
319
  }else {
232
- mCtx.runOnUiQueueThread(() -> mMapControl.goTo(venue));
320
+ runOnUiQueueThread(null, mapControl -> mapControl.goTo(venue));
233
321
  }
234
322
  break;
235
323
  }
@@ -239,7 +327,12 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
239
327
 
240
328
  @ReactMethod
241
329
  public void getCurrentVenue(final Promise promise) {
242
- final MPVenue venue = mMapControl.getCurrentVenue();
330
+ MapControl mapControl = mMapControl;
331
+ if (mapControl == null) {
332
+ promise.resolve(null);
333
+ return;
334
+ }
335
+ final MPVenue venue = mapControl.getCurrentVenue();
243
336
  if (venue != null) {
244
337
  promise.resolve(gson.toJson(venue));
245
338
  } else {
@@ -249,7 +342,12 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
249
342
 
250
343
  @ReactMethod
251
344
  public void getCurrentBuilding(final Promise promise) {
252
- final MPBuilding building = mMapControl.getCurrentBuilding();
345
+ MapControl mapControl = mMapControl;
346
+ if (mapControl == null) {
347
+ promise.resolve(null);
348
+ return;
349
+ }
350
+ final MPBuilding building = mapControl.getCurrentBuilding();
253
351
  if (building != null) {
254
352
  promise.resolve(gson.toJson(building));
255
353
  } else {
@@ -260,8 +358,8 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
260
358
  @ReactMethod
261
359
  public void selectVenue(String venueString, boolean moveCamera, final Promise promise) {
262
360
  MPVenue venue = gson.fromJson(venueString, MPVenue.class);
263
- mCtx.runOnUiQueueThread(() -> {
264
- mMapControl.selectVenue(venue, moveCamera);
361
+ runOnUiQueueThread(promise, mapControl -> {
362
+ mapControl.selectVenue(venue, moveCamera);
265
363
  promise.resolve(null);
266
364
  });
267
365
  }
@@ -269,8 +367,8 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
269
367
  @ReactMethod
270
368
  public void selectBuilding(String buildingString, boolean moveCamera, final Promise promise) {
271
369
  MPBuilding building = gson.fromJson(buildingString, MPBuilding.class);
272
- mCtx.runOnUiQueueThread(() -> {
273
- mMapControl.selectBuilding(building, moveCamera);
370
+ runOnUiQueueThread(promise, mapControl -> {
371
+ mapControl.selectBuilding(building, moveCamera);
274
372
  promise.resolve(null);
275
373
  });
276
374
  }
@@ -279,8 +377,8 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
279
377
  public void selectLocation(String locationString, String behaviorString, final Promise promise) {
280
378
  MPLocation location = gson.fromJson(locationString, Location.class).toMPLocation();
281
379
  MPSelectionBehavior behavior = gson.fromJson(behaviorString, MPSelectionBehavior.class);
282
- mCtx.runOnUiQueueThread(() -> {
283
- mMapControl.selectLocation(location, behavior);
380
+ runOnUiQueueThread(promise, mapControl -> {
381
+ mapControl.selectLocation(location, behavior);
284
382
  promise.resolve(null);
285
383
  });
286
384
 
@@ -289,8 +387,8 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
289
387
  @ReactMethod
290
388
  public void selectLocationWithId(String locationId, String behaviorString, final Promise promise) {
291
389
  MPSelectionBehavior behavior = gson.fromJson(behaviorString, MPSelectionBehavior.class);
292
- mCtx.runOnUiQueueThread(() -> {
293
- mMapControl.selectLocation(locationId, behavior);
390
+ runOnUiQueueThread(promise, mapControl -> {
391
+ mapControl.selectLocation(locationId, behavior);
294
392
  promise.resolve(null);
295
393
  });
296
394
  }
@@ -298,113 +396,166 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
298
396
  @ReactMethod
299
397
  public void setMapPadding(int start, int top, int end, int bottom, final Promise promise) {
300
398
  double density = mCtx.getResources().getDisplayMetrics().density;
301
- mCtx.runOnUiQueueThread(() -> {
302
- mMapControl.setMapPadding((int)Math.round(start * density), (int)Math.round(top * density), (int)Math.round(end * density), (int) Math.round(bottom * density));
399
+ runOnUiQueueThread(promise, mapControl -> {
400
+ mapControl.setMapPadding((int)Math.round(start * density), (int)Math.round(top * density), (int)Math.round(end * density), (int) Math.round(bottom * density));
303
401
  promise.resolve(null);
304
402
  });
305
403
  }
306
404
 
307
405
  @ReactMethod
308
406
  public void getMapViewPaddingStart(final Promise promise) {
407
+ MapControl mapControl = requireMapControl(promise);
408
+ if (mapControl == null) {
409
+ return;
410
+ }
309
411
  double density = mCtx.getResources().getDisplayMetrics().density;
310
- promise.resolve((int)Math.round(mMapControl.getMapViewPaddingStart()/density));
412
+ promise.resolve((int)Math.round(mapControl.getMapViewPaddingStart()/density));
311
413
  }
312
414
 
313
415
  @ReactMethod
314
416
  public void getMapViewPaddingTop(final Promise promise) {
417
+ MapControl mapControl = requireMapControl(promise);
418
+ if (mapControl == null) {
419
+ return;
420
+ }
315
421
  double density = mCtx.getResources().getDisplayMetrics().density;
316
- promise.resolve((int)Math.round(mMapControl.getMapViewPaddingTop()/density));
422
+ promise.resolve((int)Math.round(mapControl.getMapViewPaddingTop()/density));
317
423
  }
318
424
 
319
425
  @ReactMethod
320
426
  public void getMapViewPaddingEnd(final Promise promise) {
427
+ MapControl mapControl = requireMapControl(promise);
428
+ if (mapControl == null) {
429
+ return;
430
+ }
321
431
  double density = mCtx.getResources().getDisplayMetrics().density;
322
- promise.resolve((int)Math.round(mMapControl.getMapViewPaddingEnd()/density));
432
+ promise.resolve((int)Math.round(mapControl.getMapViewPaddingEnd()/density));
323
433
  }
324
434
 
325
435
  @ReactMethod
326
436
  public void getMapViewPaddingBottom(final Promise promise) {
437
+ MapControl mapControl = requireMapControl(promise);
438
+ if (mapControl == null) {
439
+ return;
440
+ }
327
441
  double density = mCtx.getResources().getDisplayMetrics().density;
328
- promise.resolve((int)Math.round(mMapControl.getMapViewPaddingBottom()/density));
442
+ promise.resolve((int)Math.round(mapControl.getMapViewPaddingBottom()/density));
329
443
  }
330
444
 
331
445
  @ReactMethod
332
446
  public void setMapStyle(String mapStyleString, final Promise promise) {
333
- mMapControl.setMapStyle(gson.fromJson(mapStyleString, MPMapStyle.class));
447
+ MapControl mapControl = requireMapControl(promise);
448
+ if (mapControl == null) {
449
+ return;
450
+ }
451
+ mapControl.setMapStyle(gson.fromJson(mapStyleString, MPMapStyle.class));
334
452
  promise.resolve(null);
335
453
  }
336
454
 
337
455
  @ReactMethod
338
456
  public void getMapStyle(final Promise promise) {
339
- MPMapStyle mapStyle = mMapControl.getMapStyle();
457
+ MapControl mapControl = requireMapControl(promise);
458
+ if (mapControl == null) {
459
+ return;
460
+ }
461
+ MPMapStyle mapStyle = mapControl.getMapStyle();
340
462
  String mapStyleString = mapStyle != null ? gson.toJson(mapStyle) : null;
341
463
  promise.resolve(mapStyleString);
342
464
  }
343
465
 
344
466
  @ReactMethod
345
467
  public void showInfoWindowOnClickedLocation(boolean show, final Promise promise) {
346
- mMapControl.showInfoWindowOnClickedLocation(show);
468
+ MapControl mapControl = requireMapControl(promise);
469
+ if (mapControl == null) {
470
+ return;
471
+ }
472
+ mapControl.showInfoWindowOnClickedLocation(show);
347
473
  promise.resolve(null);
348
474
  }
349
475
 
350
476
  @ReactMethod
351
477
  public void deSelectLocation(final Promise promise) {
352
- mCtx.runOnUiQueueThread(() -> {
353
- mMapControl.deSelectLocation();
478
+ runOnUiQueueThread(promise, mapControl -> {
479
+ mapControl.deSelectLocation();
354
480
  promise.resolve(null);
355
481
  });
356
482
  }
357
483
 
358
484
  @ReactMethod
359
485
  public void getCurrentBuildingFloor(final Promise promise) {
360
- MPFloor floor = mMapControl.getCurrentBuildingFloor();
486
+ MapControl mapControl = mMapControl;
487
+ if (mapControl == null) {
488
+ promise.resolve(null);
489
+ return;
490
+ }
491
+ MPFloor floor = mapControl.getCurrentBuildingFloor();
361
492
  String floorString = floor != null ? gson.toJson(floor) : null;
362
493
  promise.resolve(floorString);
363
494
  }
364
495
 
365
496
  @ReactMethod
366
497
  public void getCurrentFloorIndex(final Promise promise) {
367
- promise.resolve(mMapControl.getCurrentFloorIndex());
498
+ MapControl mapControl = requireMapControl(promise);
499
+ if (mapControl == null) {
500
+ return;
501
+ }
502
+ promise.resolve(mapControl.getCurrentFloorIndex());
368
503
  }
369
504
 
370
505
  @ReactMethod
371
506
  public void getCurrentMapsIndoorsZoom(final Promise promise) {
372
- promise.resolve(mMapControl.getCurrentMapsIndoorsZoom());
507
+ MapControl mapControl = requireMapControl(promise);
508
+ if (mapControl == null) {
509
+ return;
510
+ }
511
+ promise.resolve(mapControl.getCurrentMapsIndoorsZoom());
373
512
  }
374
513
 
375
514
  @ReactMethod
376
515
  public void selectFloor(int floorIndex, final Promise promise) {
377
- mCtx.runOnUiQueueThread(() -> {
378
- mMapControl.selectFloor(floorIndex);
516
+ runOnUiQueueThread(promise, mapControl -> {
517
+ mapControl.selectFloor(floorIndex);
379
518
  promise.resolve(null);
380
519
  });
381
520
  }
382
521
 
383
522
  @ReactMethod
384
523
  public void isFloorSelectorHidden(final Promise promise) {
385
- promise.resolve(mMapControl.isFloorSelectorHidden());
524
+ MapControl mapControl = requireMapControl(promise);
525
+ if (mapControl == null) {
526
+ return;
527
+ }
528
+ promise.resolve(mapControl.isFloorSelectorHidden());
386
529
  }
387
530
 
388
531
  @ReactMethod
389
532
  public void hideFloorSelector(boolean hide, final Promise promise) {
390
- mCtx.runOnUiQueueThread(() -> {
391
- mMapControl.hideFloorSelector(hide);
533
+ runOnUiQueueThread(promise, mapControl -> {
534
+ mapControl.hideFloorSelector(hide);
392
535
  promise.resolve(null);
393
536
  });
394
537
  }
395
538
 
396
539
  @ReactMethod
397
540
  public void isUserPositionShown(final Promise promise) {
398
- promise.resolve(mMapControl.isUserPositionShown());
541
+ MapControl mapControl = requireMapControl(promise);
542
+ if (mapControl == null) {
543
+ return;
544
+ }
545
+ promise.resolve(mapControl.isUserPositionShown());
399
546
  }
400
547
 
401
548
  private final Map<String, OnLiveLocationUpdateListener> liveLocationUpdateListeners = new HashMap<>();
402
549
  @ReactMethod
403
550
  public void enableLiveData(String domainType, boolean hasListener, final Promise promise) {
551
+ MapControl mapControl = requireMapControl(promise);
552
+ if (mapControl == null) {
553
+ return;
554
+ }
404
555
  if (hasListener) {
405
556
  if (liveLocationUpdateListeners.containsKey(domainType)) {
406
557
  liveLocationUpdateListeners.remove(domainType);
407
- mMapControl.disableLiveData(domainType);
558
+ mapControl.disableLiveData(domainType);
408
559
  }
409
560
 
410
561
  OnLiveLocationUpdateListener listener = location -> {
@@ -428,17 +579,22 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
428
579
 
429
580
  liveLocationUpdateListeners.put(domainType, listener);
430
581
 
431
- mMapControl.enableLiveData(domainType, listener);
582
+ mapControl.enableLiveData(domainType, listener);
432
583
  } else {
433
- mMapControl.enableLiveData(domainType);
584
+ mapControl.enableLiveData(domainType);
434
585
  }
435
586
  promise.resolve(null);
436
587
  }
437
588
 
438
589
  @ReactMethod
439
590
  public void disableLiveData(String domainType, final Promise promise) {
591
+ MapControl mapControl = mMapControl;
592
+ if (mapControl == null) {
593
+ promise.resolve(null);
594
+ return;
595
+ }
440
596
  liveLocationUpdateListeners.remove(domainType);
441
- mMapControl.disableLiveData(domainType);
597
+ mapControl.disableLiveData(domainType);
442
598
  promise.resolve(null);
443
599
  }
444
600
 
@@ -446,8 +602,13 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
446
602
  public void animateCamera(String updateString, @Nullable Integer duration, final Promise promise) {
447
603
  MPCameraUpdate update = gson.fromJson(updateString, MPCameraUpdate.class);
448
604
  mCtx.runOnUiQueueThread(() -> {
605
+ RCMapView mapView = mMapView;
606
+ if (mapView == null) {
607
+ promise.reject("MapControlError", "MapView not available");
608
+ return;
609
+ }
449
610
  // we have to parse null as -1 because iOS cannot parse null
450
- mMapView.animateCamera(update, (duration == - 1 ? null : duration), error -> {
611
+ mapView.animateCamera(update, (duration == - 1 ? null : duration), error -> {
451
612
  promise.resolve(null);
452
613
  });
453
614
  });
@@ -457,7 +618,12 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
457
618
  public void moveCamera(String updateString, final Promise promise) {
458
619
  MPCameraUpdate update = gson.fromJson(updateString, MPCameraUpdate.class);
459
620
  mCtx.runOnUiQueueThread(() -> {
460
- mMapView.moveCamera(update);
621
+ RCMapView mapView = mMapView;
622
+ if (mapView == null) {
623
+ promise.reject("MapControlError", "MapView not available");
624
+ return;
625
+ }
626
+ mapView.moveCamera(update);
461
627
  promise.resolve(null);
462
628
  });
463
629
  }
@@ -466,9 +632,14 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
466
632
  public void getCurrentCameraPosition(final Promise promise) {
467
633
  mCtx.runOnUiQueueThread(() -> {
468
634
  // Runs later on the UI queue, off React Native's stack (MS-3983).
635
+ RCMapView mapView = mMapView;
636
+ if (mapView == null) {
637
+ promise.reject("MapControlError", "MapView not available");
638
+ return;
639
+ }
469
640
  final String cameraPositionString;
470
641
  try {
471
- cameraPositionString = gson.toJson(mMapView.getCurrentCameraPosition());
642
+ cameraPositionString = gson.toJson(mapView.getCurrentCameraPosition());
472
643
  } catch (Exception | StackOverflowError t) {
473
644
  RnGson.rejectSerialisationFailure(promise, "MapControlError",
474
645
  "Could not serialise the camera position: " + t);
@@ -480,13 +651,14 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
480
651
 
481
652
  @ReactMethod
482
653
  public void setFloorSelector(boolean setup, Boolean isAutoFloorChangeEnabled, final Promise promise) {
483
- if (mMapControl != null) {
654
+ MapControl mapControl = mMapControl;
655
+ if (mapControl != null) {
484
656
  if (setup) {
485
657
  autoFloorChange = isAutoFloorChangeEnabled;
486
- mCtx.runOnUiQueueThread(() -> mMapControl.setFloorSelector(this));
658
+ runOnUiQueueThread(null, current -> current.setFloorSelector(this));
487
659
  } else {
488
660
  autoFloorChange = true;
489
- mMapControl.setFloorSelector(null);
661
+ mapControl.setFloorSelector(null);
490
662
  }
491
663
  }
492
664
  promise.resolve(null);
@@ -494,9 +666,13 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
494
666
 
495
667
  @ReactMethod
496
668
  public void setLabelOptions(Integer textSize, String color, boolean showHalo, final Promise promise) {
497
- mMapControl.setMapLabelFont(Typeface.DEFAULT, color, showHalo);
669
+ MapControl mapControl = requireMapControl(promise);
670
+ if (mapControl == null) {
671
+ return;
672
+ }
673
+ mapControl.setMapLabelFont(Typeface.DEFAULT, color, showHalo);
498
674
  if (textSize != -1) {
499
- mMapControl.setMapLabelTextSize(textSize);
675
+ mapControl.setMapLabelTextSize(textSize);
500
676
  }
501
677
  promise.resolve(null);
502
678
  }
@@ -505,8 +681,13 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
505
681
 
506
682
  @ReactMethod
507
683
  public void setOnMapClickListener(boolean setup, @Nullable final Boolean consumeEvent) {
684
+ MapControl mapControl = mMapControl;
685
+ if (mapControl == null) {
686
+ warnNoMapControl("setOnMapClickListener");
687
+ return;
688
+ }
508
689
  if (setup) {
509
- mMapControl.setOnMapClickListener((latLng, locations) -> {
690
+ mapControl.setOnMapClickListener((latLng, locations) -> {
510
691
  // Emitted from an SDK callback, off React Native's stack: a throw here takes the
511
692
  // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
512
693
  // The locations list is the payload that crashed in MS-3983, and this fires on every
@@ -524,14 +705,19 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
524
705
  return Boolean.TRUE.equals(consumeEvent);
525
706
  });
526
707
  } else {
527
- mMapControl.setOnMapClickListener(null);
708
+ mapControl.setOnMapClickListener(null);
528
709
  }
529
710
  }
530
711
 
531
712
  @ReactMethod
532
713
  public void setOnLocationSelectedListener(boolean setup, @Nullable final Boolean consumeEvent) {
714
+ MapControl mapControl = mMapControl;
715
+ if (mapControl == null) {
716
+ warnNoMapControl("setOnLocationSelectedListener");
717
+ return;
718
+ }
533
719
  if (setup) {
534
- mMapControl.setOnLocationSelectedListener(location -> {
720
+ mapControl.setOnLocationSelectedListener(location -> {
535
721
  // Emitted from an SDK callback, off React Native's stack: a throw here takes the
536
722
  // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
537
723
  final WritableMap params = Arguments.createMap();
@@ -545,14 +731,19 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
545
731
  return Boolean.TRUE.equals(consumeEvent);
546
732
  });
547
733
  } else {
548
- mMapControl.setOnLocationSelectedListener(null);
734
+ mapControl.setOnLocationSelectedListener(null);
549
735
  }
550
736
  }
551
737
 
552
738
  @ReactMethod
553
739
  public void setOnCurrentVenueChangedListener(boolean setup) {
740
+ MapControl mapControl = mMapControl;
741
+ if (mapControl == null) {
742
+ warnNoMapControl("setOnCurrentVenueChangedListener");
743
+ return;
744
+ }
554
745
  if (setup) {
555
- mMapControl.setOnCurrentVenueChangedListener(venue -> {
746
+ mapControl.setOnCurrentVenueChangedListener(venue -> {
556
747
  // Emitted from an SDK callback, off React Native's stack: a throw here takes the
557
748
  // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
558
749
  final WritableMap params = Arguments.createMap();
@@ -565,14 +756,19 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
565
756
  emit(Listener.ON_VENUE_FOUND_AT_CAMERA_TARGET, params);
566
757
  });
567
758
  } else {
568
- mMapControl.setOnCurrentVenueChangedListener(null);
759
+ mapControl.setOnCurrentVenueChangedListener(null);
569
760
  }
570
761
  }
571
762
 
572
763
  @ReactMethod
573
764
  public void setOnCurrentBuildingChangedListener(boolean setup) {
765
+ MapControl mapControl = mMapControl;
766
+ if (mapControl == null) {
767
+ warnNoMapControl("setOnCurrentBuildingChangedListener");
768
+ return;
769
+ }
574
770
  if (setup) {
575
- mMapControl.setOnCurrentBuildingChangedListener(building -> {
771
+ mapControl.setOnCurrentBuildingChangedListener(building -> {
576
772
  // Emitted from an SDK callback, off React Native's stack: a throw here takes the
577
773
  // host app down and there is no promise to fail. Drop the event and log it (MS-3983).
578
774
  final WritableMap params = Arguments.createMap();
@@ -585,63 +781,87 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
585
781
  emit(Listener.ON_BUILDING_FOUND_AT_CAMERA_TARGET, params);
586
782
  });
587
783
  } else {
588
- mMapControl.setOnCurrentBuildingChangedListener(null);
784
+ mapControl.setOnCurrentBuildingChangedListener(null);
589
785
  }
590
786
  }
591
787
 
592
788
  @ReactMethod
593
789
  public void setOnMarkerClickListener(boolean setup, @Nullable final Boolean consumeEvent) {
790
+ MapControl mapControl = mMapControl;
791
+ if (mapControl == null) {
792
+ warnNoMapControl("setOnMarkerClickListener");
793
+ return;
794
+ }
594
795
  if (setup) {
595
- mMapControl.setOnMarkerClickListener(marker -> {
796
+ mapControl.setOnMarkerClickListener(marker -> {
596
797
  WritableMap params = Arguments.createMap();
597
798
  params.putString("locationId", marker.getId());
598
799
  emit(Listener.ON_MARKER_CLICK, params);
599
800
  return Boolean.TRUE.equals(consumeEvent);
600
801
  });
601
802
  } else {
602
- mMapControl.setOnMarkerClickListener(null);
803
+ mapControl.setOnMarkerClickListener(null);
603
804
  }
604
805
  }
605
806
 
606
807
  @ReactMethod
607
808
  public void setOnMarkerInfoWindowClickListener(boolean setup) {
809
+ MapControl mapControl = mMapControl;
810
+ if (mapControl == null) {
811
+ warnNoMapControl("setOnMarkerInfoWindowClickListener");
812
+ return;
813
+ }
608
814
  if (setup) {
609
- mMapControl.setOnMarkerInfoWindowClickListener(marker -> {
815
+ mapControl.setOnMarkerInfoWindowClickListener(marker -> {
610
816
  WritableMap params = Arguments.createMap();
611
817
  params.putString("locationId", marker.getId());
612
818
  emit(Listener.ON_INFO_WINDOW_CLICK, params);
613
819
  });
614
820
  } else {
615
- mMapControl.setOnMarkerInfoWindowClickListener(null);
821
+ mapControl.setOnMarkerInfoWindowClickListener(null);
616
822
  }
617
823
  }
618
824
 
619
825
  @ReactMethod
620
826
  public void setMPCameraEventListener(boolean setup) {
827
+ MapControl mapControl = mMapControl;
828
+ if (mapControl == null) {
829
+ warnNoMapControl("setMPCameraEventListener");
830
+ return;
831
+ }
621
832
  if (setup) {
622
- mMapControl.addOnCameraEventListener(this);
833
+ mapControl.addOnCameraEventListener(this);
623
834
  } else {
624
- mMapControl.removeOnCameraEventListener(this);
835
+ mapControl.removeOnCameraEventListener(this);
625
836
  }
626
837
  }
627
838
 
628
839
  @ReactMethod
629
840
  public void setOnFloorUpdateListener(boolean setup) {
841
+ MapControl mapControl = mMapControl;
842
+ if (mapControl == null) {
843
+ warnNoMapControl("setOnFloorUpdateListener");
844
+ return;
845
+ }
630
846
  if (setup) {
631
- mMapControl.addOnFloorUpdateListener(this);
847
+ mapControl.addOnFloorUpdateListener(this);
632
848
  } else {
633
- mMapControl.removeOnFloorUpdateListener(this);
849
+ mapControl.removeOnFloorUpdateListener(this);
634
850
  }
635
851
  }
636
852
 
637
853
  @ReactMethod
638
854
  public void setBuildingSelectionMode(Double selectionMode, final Promise promise) {
855
+ MapControl mapControl = requireMapControl(promise);
856
+ if (mapControl == null) {
857
+ return;
858
+ }
639
859
  switch (selectionMode.intValue()) {
640
860
  case 0:
641
- mMapControl.setBuildingSelectionMode(MPSelectionMode.MANUAL);
861
+ mapControl.setBuildingSelectionMode(MPSelectionMode.MANUAL);
642
862
  break;
643
863
  case 1:
644
- mMapControl.setBuildingSelectionMode(MPSelectionMode.AUTOMATIC);
864
+ mapControl.setBuildingSelectionMode(MPSelectionMode.AUTOMATIC);
645
865
  break;
646
866
  }
647
867
  promise.resolve(null);
@@ -649,17 +869,25 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
649
869
 
650
870
  @ReactMethod
651
871
  public void getBuildingSelectionMode(final Promise promise) {
652
- promise.resolve(mMapControl.getBuildingSelectionMode().ordinal());
872
+ MapControl mapControl = requireMapControl(promise);
873
+ if (mapControl == null) {
874
+ return;
875
+ }
876
+ promise.resolve(mapControl.getBuildingSelectionMode().ordinal());
653
877
  }
654
878
 
655
879
  @ReactMethod
656
880
  public void setFloorSelectionMode(Double selectionMode, final Promise promise) {
881
+ MapControl mapControl = requireMapControl(promise);
882
+ if (mapControl == null) {
883
+ return;
884
+ }
657
885
  switch (selectionMode.intValue()) {
658
886
  case 0:
659
- mMapControl.setFloorSelectionMode(MPSelectionMode.MANUAL);
887
+ mapControl.setFloorSelectionMode(MPSelectionMode.MANUAL);
660
888
  break;
661
889
  case 1:
662
- mMapControl.setFloorSelectionMode(MPSelectionMode.AUTOMATIC);
890
+ mapControl.setFloorSelectionMode(MPSelectionMode.AUTOMATIC);
663
891
  break;
664
892
  }
665
893
  promise.resolve(null);
@@ -667,7 +895,11 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
667
895
 
668
896
  @ReactMethod
669
897
  public void getFloorSelectionMode(final Promise promise) {
670
- promise.resolve(mMapControl.getFloorSelectionMode().ordinal());
898
+ MapControl mapControl = requireMapControl(promise);
899
+ if (mapControl == null) {
900
+ return;
901
+ }
902
+ promise.resolve(mapControl.getFloorSelectionMode().ordinal());
671
903
  }
672
904
 
673
905
  @ReactMethod
@@ -696,15 +928,19 @@ public class MapControlModule extends ReactContextBaseJavaModule implements MPCa
696
928
  break;
697
929
  }
698
930
  }
699
- getCurrentActivity().runOnUiThread(()-> {
700
- mMapControl.setHiddenFeatures(features);
931
+ runOnUiQueueThread(promise, mapControl -> {
932
+ mapControl.setHiddenFeatures(features);
701
933
  promise.resolve(null);
702
934
  });
703
935
  }
704
936
 
705
937
  @ReactMethod
706
938
  public void getHiddenFeatures(final Promise promise) {
707
- List<MPFeatureType> features = mMapControl.getHiddenFeatures();
939
+ MapControl mapControl = requireMapControl(promise);
940
+ if (mapControl == null) {
941
+ return;
942
+ }
943
+ List<MPFeatureType> features = mapControl.getHiddenFeatures();
708
944
  List<Integer> featureStrings = new ArrayList<>();
709
945
  for (MPFeatureType feature : features) {
710
946
  featureStrings.add(feature.ordinal());