-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathNavViewManager.java
More file actions
768 lines (701 loc) · 28.1 KB
/
NavViewManager.java
File metadata and controls
768 lines (701 loc) · 28.1 KB
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
/**
* Copyright 2023 Google LLC
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.react.navsdk;
import static com.google.android.react.navsdk.Command.*;
import android.view.Choreographer;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.libraries.navigation.StylingOptions;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
// NavViewManager is responsible for managing both the regular map fragment as well as the
// navigation map view fragment.
//
public class NavViewManager extends SimpleViewManager<FrameLayout> {
public static final String REACT_CLASS = "NavViewManager";
private static NavViewManager instance;
private final HashMap<Integer, WeakReference<IMapViewFragment>> fragmentMap = new HashMap<>();
private final HashMap<Integer, Choreographer.FrameCallback> frameCallbackMap = new HashMap<>();
// Cache the latest options per view so deferred fragment creation uses fresh values.
private final HashMap<Integer, ReadableMap> mapOptionsCache = new HashMap<>();
private ReactApplicationContext reactContext;
public static synchronized NavViewManager getInstance(ReactApplicationContext reactContext) {
if (instance == null) {
instance = new NavViewManager();
}
instance.setReactContext(reactContext);
return instance;
}
private boolean isFragmentCreated(int viewId) {
WeakReference<IMapViewFragment> weakReference = fragmentMap.get(viewId);
if (weakReference == null) {
return false;
}
IMapViewFragment fragment = weakReference.get();
if (fragment == null) {
// Clean up the map entry if the fragment is not available anymore.
fragmentMap.remove(viewId);
return false;
}
return true;
}
/** Builds GoogleMapOptions with all configured map settings. */
@NonNull
private GoogleMapOptions buildGoogleMapOptions(ReadableMap mapOptionsMap) {
GoogleMapOptions options = new GoogleMapOptions();
if (mapOptionsMap == null) {
return options;
}
if (mapOptionsMap.hasKey("mapId") && !mapOptionsMap.isNull("mapId")) {
String mapIdFromOptions = mapOptionsMap.getString("mapId");
if (mapIdFromOptions != null && !mapIdFromOptions.isEmpty()) {
options.mapId(mapIdFromOptions);
}
}
if (mapOptionsMap.hasKey("mapType") && !mapOptionsMap.isNull("mapType")) {
options.mapType(mapOptionsMap.getInt("mapType"));
}
if (mapOptionsMap.hasKey("mapColorScheme")) {
int jsValue =
mapOptionsMap.isNull("mapColorScheme") ? 0 : mapOptionsMap.getInt("mapColorScheme");
options.mapColorScheme(EnumTranslationUtil.getMapColorSchemeFromJsValue(jsValue));
}
return options;
}
@NonNull
@Override
public String getName() {
return REACT_CLASS;
}
public void setReactContext(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}
/** Return a FrameLayout which will later hold the Fragment */
@NonNull
@Override
public FrameLayout createViewInstance(@NonNull ThemedReactContext reactContext) {
FrameLayout frameLayout = new FrameLayout(reactContext);
return frameLayout;
}
/**
* Ensures the fragment view is properly measured and laid out within its parent FrameLayout. This
* is necessary because React Native's layout system doesn't automatically propagate layout to
* native fragments.
*/
private void layoutFragmentInView(FrameLayout frameLayout, @Nullable IMapViewFragment fragment) {
if (fragment == null || !fragment.isAdded()) {
return;
}
int width = frameLayout.getWidth();
int height = frameLayout.getHeight();
if (width == 0 || height == 0) {
return;
}
View fragmentView = fragment.getView();
if (fragmentView == null) {
return;
}
fragmentView.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
fragmentView.layout(0, 0, width, height);
}
/**
* Starts a per-frame layout loop similar to the older implementation to ensure the fragment view
* stays in sync with its container size.
*/
private void startLayoutLoop(FrameLayout view) {
int viewId = view.getId();
// Remove existing callback if present
Choreographer.FrameCallback existing = frameCallbackMap.get(viewId);
if (existing != null) {
Choreographer.getInstance().removeFrameCallback(existing);
}
Choreographer.FrameCallback frameCallback =
new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
IMapViewFragment fragment = getFragmentForViewId(viewId);
if (fragment != null) {
layoutFragmentInView(view, fragment);
Choreographer.getInstance().postFrameCallback(this);
} else {
frameCallbackMap.remove(viewId);
}
}
};
frameCallbackMap.put(viewId, frameCallback);
Choreographer.getInstance().postFrameCallback(frameCallback);
}
/** Clean up fragment when React Native view is destroyed */
@Override
public void onDropViewInstance(@NonNull FrameLayout view) {
super.onDropViewInstance(view);
int viewId = view.getId();
Choreographer.FrameCallback frameCallback = frameCallbackMap.remove(viewId);
if (frameCallback != null) {
Choreographer.getInstance().removeFrameCallback(frameCallback);
}
FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
if (activity == null) return;
WeakReference<IMapViewFragment> weakReference = fragmentMap.remove(viewId);
mapOptionsCache.remove(viewId);
if (weakReference != null) {
IMapViewFragment fragment = weakReference.get();
if (fragment != null && fragment.isAdded()) {
activity
.getSupportFragmentManager()
.beginTransaction()
.remove((Fragment) fragment)
.commitNowAllowingStateLoss();
}
}
}
@ReactProp(name = "mapOptions")
public void setMapOptions(FrameLayout view, @NonNull ReadableMap mapOptions) {
int viewId = view.getId();
mapOptionsCache.put(viewId, mapOptions);
if (isFragmentCreated(viewId)) {
updateMapOptionValues(viewId, mapOptions);
return;
}
scheduleFragmentTransaction(view, mapOptions);
}
/** Map the "create" command to an integer */
@Nullable
@Override
public Map<String, Integer> getCommandsMap() {
Map<String, Integer> map = new HashMap<>();
map.put(MOVE_CAMERA.toString(), MOVE_CAMERA.getValue());
map.put(SET_TRIP_PROGRESS_BAR_ENABLED.toString(), SET_TRIP_PROGRESS_BAR_ENABLED.getValue());
map.put(SET_NAVIGATION_UI_ENABLED.toString(), SET_NAVIGATION_UI_ENABLED.getValue());
map.put(SET_FOLLOWING_PERSPECTIVE.toString(), SET_FOLLOWING_PERSPECTIVE.getValue());
map.put(SET_NIGHT_MODE.toString(), SET_NIGHT_MODE.getValue());
map.put(SET_SPEEDOMETER_ENABLED.toString(), SET_SPEEDOMETER_ENABLED.getValue());
map.put(SET_SPEED_LIMIT_ICON_ENABLED.toString(), SET_SPEED_LIMIT_ICON_ENABLED.getValue());
map.put(SET_ZOOM_LEVEL.toString(), SET_ZOOM_LEVEL.getValue());
map.put(SET_INDOOR_ENABLED.toString(), SET_INDOOR_ENABLED.getValue());
map.put(SET_TRAFFIC_ENABLED.toString(), SET_TRAFFIC_ENABLED.getValue());
map.put(SET_COMPASS_ENABLED.toString(), SET_COMPASS_ENABLED.getValue());
map.put(SET_MY_LOCATION_BUTTON_ENABLED.toString(), SET_MY_LOCATION_BUTTON_ENABLED.getValue());
map.put(SET_MY_LOCATION_ENABLED.toString(), SET_MY_LOCATION_ENABLED.getValue());
map.put(SET_ROTATE_GESTURES_ENABLED.toString(), SET_ROTATE_GESTURES_ENABLED.getValue());
map.put(SET_SCROLL_GESTURES_ENABLED.toString(), SET_SCROLL_GESTURES_ENABLED.getValue());
map.put(
SET_SCROLL_GESTURES_ENABLED_DURING_ROTATE_OR_ZOOM.toString(),
SET_SCROLL_GESTURES_ENABLED_DURING_ROTATE_OR_ZOOM.getValue());
map.put(SET_ZOOM_CONTROLS_ENABLED.toString(), SET_ZOOM_CONTROLS_ENABLED.getValue());
map.put(SET_TILT_GESTURES_ENABLED.toString(), SET_TILT_GESTURES_ENABLED.getValue());
map.put(SET_ZOOM_GESTURES_ENABLED.toString(), SET_ZOOM_GESTURES_ENABLED.getValue());
map.put(SET_BUILDINGS_ENABLED.toString(), SET_BUILDINGS_ENABLED.getValue());
map.put(SET_MAP_TYPE.toString(), SET_MAP_TYPE.getValue());
map.put(SET_MAP_TOOLBAR_ENABLED.toString(), SET_MAP_TOOLBAR_ENABLED.getValue());
map.put(CLEAR_MAP_VIEW.toString(), CLEAR_MAP_VIEW.getValue());
map.put(RESET_MIN_MAX_ZOOM_LEVEL.toString(), RESET_MIN_MAX_ZOOM_LEVEL.getValue());
map.put(SET_MAP_STYLE.toString(), SET_MAP_STYLE.getValue());
map.put(ANIMATE_CAMERA.toString(), ANIMATE_CAMERA.getValue());
map.put(
SET_TRAFFIC_INCIDENT_CARDS_ENABLED.toString(),
SET_TRAFFIC_INCIDENT_CARDS_ENABLED.getValue());
map.put(SET_RECENTER_BUTTON_ENABLED.toString(), SET_RECENTER_BUTTON_ENABLED.getValue());
map.put(SHOW_ROUTE_OVERVIEW.toString(), SHOW_ROUTE_OVERVIEW.getValue());
map.put(REMOVE_MARKER.toString(), REMOVE_MARKER.getValue());
map.put(REMOVE_POLYLINE.toString(), REMOVE_POLYLINE.getValue());
map.put(REMOVE_POLYGON.toString(), REMOVE_POLYGON.getValue());
map.put(REMOVE_CIRCLE.toString(), REMOVE_CIRCLE.getValue());
map.put(REMOVE_GROUND_OVERLAY.toString(), REMOVE_GROUND_OVERLAY.getValue());
map.put(SET_HEADER_ENABLED.toString(), SET_HEADER_ENABLED.getValue());
map.put(SET_FOOTER_ENABLED.toString(), SET_FOOTER_ENABLED.getValue());
map.put(SET_PADDING.toString(), SET_PADDING.getValue());
return map;
}
@Nullable
public INavViewFragment getNavFragmentForRoot(ViewGroup root) {
IMapViewFragment fragment = getFragmentForRoot(root);
if (fragment == null) {
return null;
}
// Check if the fragment is an INavigationViewFragment
if (fragment instanceof INavViewFragment) {
return (INavViewFragment) fragment;
} else {
throw new IllegalStateException("The fragment is not a nav view fragment");
}
}
@Nullable
public IMapViewFragment getFragmentForRoot(ViewGroup root) {
int viewId = root.getId();
return getFragmentForViewId(viewId);
}
@Nullable
public IMapViewFragment getFragmentForViewId(int viewId) {
WeakReference<IMapViewFragment> weakReference = fragmentMap.get(viewId);
if (weakReference == null) {
return null; // No entry for this viewId
}
IMapViewFragment fragment = weakReference.get();
if (fragment == null) {
// Fragment was garbage collected, clean up the map entry
fragmentMap.remove(viewId);
return null;
}
return fragment;
}
public void onNavigationReady() {
for (WeakReference<IMapViewFragment> weakReference : fragmentMap.values()) {
IMapViewFragment fragment = weakReference.get();
if (fragment instanceof INavViewFragment) {
INavViewFragment navFragment = (INavViewFragment) fragment;
navFragment.setNavigationUiEnabled(true);
navFragment.applyStylingOptions();
}
}
}
@Override
public void receiveCommand(
@NonNull FrameLayout root, String commandId, @Nullable ReadableArray args) {
super.receiveCommand(root, commandId, args);
int commandIdInt = Integer.parseInt(commandId);
Command command = Command.find(commandIdInt);
assert command != null;
IMapViewFragment fragment;
INavViewFragment navFragment;
switch (command) {
case MOVE_CAMERA:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment
.getMapController()
.moveCamera(Objects.requireNonNull(args.getMap(0)).toHashMap());
}
break;
case SET_TRIP_PROGRESS_BAR_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setTripProgressBarEnabled(args.getBoolean(0));
}
break;
case SET_NAVIGATION_UI_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setNavigationUiEnabled(args.getBoolean(0));
}
break;
case SET_FOLLOWING_PERSPECTIVE:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.getMapController().setFollowingPerspective(args.getInt(0));
}
break;
case SET_NIGHT_MODE:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
int nightModeOverride = EnumTranslationUtil.getForceNightModeFromJsValue(args.getInt(0));
navFragment.setNightModeOption(nightModeOverride);
}
break;
case SET_SPEEDOMETER_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setSpeedometerEnabled(args.getBoolean(0));
}
break;
case SET_SPEED_LIMIT_ICON_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setSpeedLimitIconEnabled(args.getBoolean(0));
}
break;
case SET_ZOOM_LEVEL:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
int level = args.getInt(0);
fragment.getMapController().setZoomLevel(level);
}
break;
case SET_INDOOR_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setIndoorEnabled(args.getBoolean(0));
}
break;
case SET_TRAFFIC_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setTrafficEnabled(args.getBoolean(0));
}
break;
case SET_COMPASS_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setCompassEnabled(args.getBoolean(0));
}
break;
case SET_MY_LOCATION_BUTTON_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setCompassEnabled(args.getBoolean(0));
}
break;
case SET_MY_LOCATION_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setMyLocationEnabled(args.getBoolean(0));
}
break;
case SET_ROTATE_GESTURES_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setRotateGesturesEnabled(args.getBoolean(0));
}
break;
case SET_SCROLL_GESTURES_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setScrollGesturesEnabled(args.getBoolean(0));
}
break;
case SET_SCROLL_GESTURES_ENABLED_DURING_ROTATE_OR_ZOOM:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment
.getMapController()
.setScrollGesturesEnabledDuringRotateOrZoom(args.getBoolean(0));
}
break;
case SET_TILT_GESTURES_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setTiltGesturesEnabled(args.getBoolean(0));
}
break;
case SET_ZOOM_CONTROLS_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setZoomControlsEnabled(args.getBoolean(0));
}
break;
case SET_ZOOM_GESTURES_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setZoomGesturesEnabled(args.getBoolean(0));
}
break;
case SET_BUILDINGS_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setBuildingsEnabled(args.getBoolean(0));
}
break;
case SET_MAP_TYPE:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setMapType(args.getInt(0));
}
break;
case SET_MAP_TOOLBAR_ENABLED:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().setMapToolbarEnabled(args.getBoolean(0));
}
break;
case CLEAR_MAP_VIEW:
fragment = getFragmentForRoot(root);
if (fragment != null) {
fragment.getMapController().clearMapView();
}
break;
case RESET_MIN_MAX_ZOOM_LEVEL:
fragment = getFragmentForRoot(root);
if (fragment != null) {
fragment.getMapController().resetMinMaxZoomLevel();
}
break;
case SET_MAP_STYLE:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.setMapStyle(args.getString(0));
}
break;
case ANIMATE_CAMERA:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment
.getMapController()
.animateCamera(Objects.requireNonNull(args.getMap(0)).toHashMap());
}
break;
case SET_TRAFFIC_INCIDENT_CARDS_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setTrafficIncidentCardsEnabled(args.getBoolean(0));
}
break;
case SET_FOOTER_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setEtaCardEnabled(args.getBoolean(0));
}
break;
case SET_HEADER_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setHeaderEnabled(args.getBoolean(0));
}
break;
case SET_RECENTER_BUTTON_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setRecenterButtonEnabled(args.getBoolean(0));
}
break;
case SHOW_ROUTE_OVERVIEW:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
navFragment.showRouteOverview();
}
break;
case REMOVE_MARKER:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().removeMarker(args.getString(0));
}
break;
case REMOVE_POLYLINE:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().removePolyline(args.getString(0));
}
break;
case REMOVE_POLYGON:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().removePolygon(args.getString(0));
}
break;
case REMOVE_CIRCLE:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().removeCircle(args.getString(0));
}
break;
case REMOVE_GROUND_OVERLAY:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment.getMapController().removeGroundOverlay(args.getString(0));
}
break;
case SET_PADDING:
fragment = getFragmentForRoot(root);
if (fragment != null) {
assert args != null;
fragment
.getMapController()
.setPadding(args.getInt(0), args.getInt(1), args.getInt(2), args.getInt(3));
}
break;
case SET_REPORT_INCIDENT_BUTTON_ENABLED:
navFragment = getNavFragmentForRoot(root);
if (navFragment != null) {
assert args != null;
navFragment.setReportIncidentButtonEnabled(args.getBoolean(0));
}
break;
}
}
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
Map<String, Object> baseEventTypeConstants = super.getExportedCustomDirectEventTypeConstants();
Map<String, Object> eventTypeConstants =
baseEventTypeConstants != null ? baseEventTypeConstants : new HashMap<>();
((Map) eventTypeConstants)
.putAll(
MapBuilder.builder()
.put(
"onRecenterButtonClick",
MapBuilder.of("registrationName", "onRecenterButtonClick"))
.put(
"onPromptVisibilityChanged",
MapBuilder.of("registrationName", "onPromptVisibilityChanged"))
.put("onMapReady", MapBuilder.of("registrationName", "onMapReady"))
.put("onMapClick", MapBuilder.of("registrationName", "onMapClick"))
.put("onMarkerClick", MapBuilder.of("registrationName", "onMarkerClick"))
.put("onPolylineClick", MapBuilder.of("registrationName", "onPolylineClick"))
.put("onPolygonClick", MapBuilder.of("registrationName", "onPolygonClick"))
.put("onCircleClick", MapBuilder.of("registrationName", "onCircleClick"))
.put(
"onGroundOverlayClick",
MapBuilder.of("registrationName", "onGroundOverlayClick"))
.put(
"onMarkerInfoWindowTapped",
MapBuilder.of("registrationName", "onMarkerInfoWindowTapped"))
.build());
return (Map) eventTypeConstants;
}
private void scheduleFragmentTransaction(
@NonNull FrameLayout root, @NonNull ReadableMap mapOptions) {
// Commit the fragment transaction after view is added to the view hierarchy.
root.post(() -> tryCommitFragmentTransaction(root, mapOptions));
}
/** Attempt to create/attach the fragment once the parent view has a real size. */
private void tryCommitFragmentTransaction(
@NonNull FrameLayout root, @NonNull ReadableMap initialMapOptions) {
int viewId = root.getId();
if (isFragmentCreated(viewId)) {
return;
}
ReadableMap latestOptions = mapOptionsCache.get(viewId);
ReadableMap optionsToUse = latestOptions != null ? latestOptions : initialMapOptions;
int width = root.getWidth();
int height = root.getHeight();
if (width == 0 || height == 0) {
// Wait for layout to provide a size, then retry without the per-frame choreographer loop.
root.post(() -> tryCommitFragmentTransaction(root, optionsToUse));
return;
}
commitFragmentTransaction(root, optionsToUse);
}
private void updateMapOptionValues(int viewId, @NonNull ReadableMap mapOptions) {
IMapViewFragment fragment = getFragmentForViewId(viewId);
if (fragment == null) {
return;
}
if (mapOptions.hasKey("mapColorScheme")) {
int jsValue = mapOptions.isNull("mapColorScheme") ? 0 : mapOptions.getInt("mapColorScheme");
fragment.setMapColorScheme(EnumTranslationUtil.getMapColorSchemeFromJsValue(jsValue));
}
if (fragment instanceof INavViewFragment
&& mapOptions.hasKey("navigationStylingOptions")
&& !mapOptions.isNull("navigationStylingOptions")) {
ReadableMap stylingMap = mapOptions.getMap("navigationStylingOptions");
if (stylingMap != null) {
StylingOptions stylingOptions =
new StylingOptionsBuilder.Builder(stylingMap.toHashMap()).build();
((INavViewFragment) fragment).setStylingOptions(stylingOptions);
}
}
if (fragment instanceof INavViewFragment && mapOptions.hasKey("navigationNightMode")) {
int nightMode =
mapOptions.isNull("navigationNightMode") ? 0 : mapOptions.getInt("navigationNightMode");
((INavViewFragment) fragment)
.setNightModeOption(EnumTranslationUtil.getForceNightModeFromJsValue(nightMode));
}
}
/** Replace your React Native view with a custom fragment */
private void commitFragmentTransaction(
@NonNull FrameLayout view, @NonNull ReadableMap mapOptions) {
FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
if (activity == null) return;
int viewId = view.getId();
Fragment fragment;
IMapViewFragment mapViewFragment;
CustomTypes.MapViewType mapViewType =
EnumTranslationUtil.getMapViewTypeFromJsValue(mapOptions.getInt("mapViewType"));
GoogleMapOptions googleMapOptions = buildGoogleMapOptions(mapOptions);
if (mapViewType == CustomTypes.MapViewType.MAP) {
MapViewFragment mapFragment =
MapViewFragment.newInstance(reactContext, viewId, googleMapOptions);
fragment = mapFragment;
mapViewFragment = mapFragment;
} else {
NavViewFragment navFragment =
NavViewFragment.newInstance(reactContext, viewId, googleMapOptions);
Integer nightMode = null;
if (mapOptions.hasKey("navigationNightMode")) {
int jsValue =
mapOptions.isNull("navigationNightMode") ? 0 : mapOptions.getInt("navigationNightMode");
nightMode = EnumTranslationUtil.getForceNightModeFromJsValue(jsValue);
navFragment.setNightModeOption(nightMode);
}
if (mapOptions.hasKey("navigationStylingOptions")
&& !mapOptions.isNull("navigationStylingOptions")) {
ReadableMap stylingOptionsMap = mapOptions.getMap("navigationStylingOptions");
StylingOptions stylingOptions =
new StylingOptionsBuilder.Builder(stylingOptionsMap.toHashMap()).build();
navFragment.setStylingOptions(stylingOptions);
}
fragment = navFragment;
mapViewFragment = navFragment;
}
fragmentMap.put(viewId, new WeakReference<IMapViewFragment>(mapViewFragment));
activity
.getSupportFragmentManager()
.beginTransaction()
.replace(viewId, fragment, String.valueOf(viewId))
.commit();
// Start per-frame layout loop to keep fragment sized correctly.
startLayoutLoop(view);
// Trigger layout after fragment is added
// Post to ensure fragment transaction is complete
view.post(() -> layoutFragmentInView(view, mapViewFragment));
}
public GoogleMap getGoogleMap(int viewId) {
try {
IMapViewFragment fragment = getFragmentForViewId(viewId);
if (fragment == null) return null;
return fragment.getGoogleMap();
} catch (Exception e) {
return null;
}
}
}