-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPluginHelper.java
More file actions
1043 lines (905 loc) · 47.3 KB
/
PluginHelper.java
File metadata and controls
1043 lines (905 loc) · 47.3 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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.situm.plugin;
import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.situm.plugin.utils.ReactNativeUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import androidx.annotation.NonNull;
import es.situm.sdk.SitumSdk;
import es.situm.sdk.communication.CommunicationManager;
import es.situm.sdk.directions.DirectionsRequest;
import es.situm.sdk.error.Error;
import es.situm.sdk.location.LocationListener;
import es.situm.sdk.location.LocationRequest;
import es.situm.sdk.location.LocationStatus;
import es.situm.sdk.model.cartography.Building;
import es.situm.sdk.model.cartography.BuildingInfo;
import es.situm.sdk.model.cartography.Floor;
import es.situm.sdk.model.cartography.Geofence;
import es.situm.sdk.model.cartography.Poi;
import es.situm.sdk.model.cartography.PoiCategory;
import es.situm.sdk.model.cartography.Point;
import es.situm.sdk.model.directions.Route;
import es.situm.sdk.model.location.Location;
import es.situm.sdk.model.navigation.NavigationProgress;
import es.situm.sdk.model.realtime.RealTimeData;
import es.situm.sdk.navigation.NavigationListener;
import es.situm.sdk.navigation.NavigationManager;
import es.situm.sdk.navigation.NavigationRequest;
import es.situm.sdk.realtime.RealTimeListener;
import es.situm.sdk.realtime.RealTimeManager;
import es.situm.sdk.realtime.RealTimeRequest;
import es.situm.sdk.utils.Handler;
import es.situm.sdk.v1.SitumEvent;
import es.situm.sdk.location.GeofenceListener;
import es.situm.sdk.navigation.ExternalNavigation;
import es.situm.sdk.userhelper.UserHelperColorScheme;
import static com.situm.plugin.SitumPlugin.EVENT_LOCATION_CHANGED;
import static com.situm.plugin.SitumPlugin.EVENT_LOCATION_ERROR;
import static com.situm.plugin.SitumPlugin.EVENT_LOCATION_STATUS_CHANGED;
import static com.situm.plugin.SitumPlugin.EVENT_LOCATION_STOPPED;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_START;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_PROGRESS;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_DESTINATION_REACHED;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_FINISHED;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_OUTSIDE_ROUTE;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_CANCELLATION;
import static com.situm.plugin.SitumPlugin.EVENT_NAVIGATION_ERROR;
import static com.situm.plugin.SitumPlugin.EVENT_REALTIME_ERROR;
import static com.situm.plugin.SitumPlugin.EVENT_REALTIME_UPDATE;
import static com.situm.plugin.SitumPlugin.EVENT_ENTER_GEOFENCES;
import static com.situm.plugin.SitumPlugin.EVENT_EXIT_GEOFENCES;
import static com.situm.plugin.utils.ReactNativeUtils.convertJsonToArray;
import static com.situm.plugin.utils.ReactNativeUtils.convertJsonToMap;
import static com.situm.plugin.utils.ReactNativeUtils.convertMapToJson;
import static com.situm.plugin.utils.ReactNativeUtils.convertReadableMapToMap;
import static com.situm.plugin.utils.ReactNativeUtils.convertMapToReadableMap;
public class PluginHelper {
private static final String TAG = "PluginHelper";
private GeometryFactory geometryFactory = new GeometryFactory();
private LocationListener locationListener;
private LocationRequest locationRequest;
private NavigationListener navigationListener;
private NavigationRequest navigationRequest;
private boolean emitEnterGeofences = false;
private boolean emitExitGeofences = false;
private volatile CommunicationManager cmInstance;
private volatile NavigationManager nmInstance;
private RealTimeListener realtimeListener;
private volatile RealTimeManager rmInstance;
private Route computedRoute;
private Location computedLocation;
private Map<Geofence, Polygon> geofencePolygonMap = new HashMap<>();
private CommunicationManager getCommunicationManagerInstance() {
if (cmInstance == null) { // Check for the first time
synchronized (CommunicationManager.class) { // Check for the second time.
// if there is no instance available... create new one
if (cmInstance == null)
cmInstance = SitumSdk.communicationManager();
}
}
return cmInstance;
}
public void setCommunicationManager(CommunicationManager communicationManager) {
cmInstance = communicationManager;
}
private RealTimeManager getRealtimeManagerInstance() {
if (rmInstance == null) {
synchronized (RealTimeManager.class) { // Check for the second time.
// if there is no instance available... create new one
if (rmInstance == null)
rmInstance = SitumSdk.realtimeManager();
}
}
return rmInstance;
}
private NavigationManager getNavigationManagerInstance() {
if (nmInstance == null) { // Check for the first time
synchronized (NavigationManager.class) { // Check for the second time.
// if there is no instance available... create new one
if (nmInstance == null)
nmInstance = SitumSdk.navigationManager();
}
}
return nmInstance;
}
public void fetchBuildings(Callback success, Callback error) {
try {
getCommunicationManagerInstance().fetchBuildings(new Handler<Collection<Building>>() {
public void onSuccess(Collection<Building> buildings) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Buildings fetched successfully.");
JSONArray jsonArrayBuildings = new JSONArray();
for (Building building : buildings) {
Log.i(PluginHelper.TAG,
"onSuccess: " + building.getIdentifier() + " - " + building.getName());
JSONObject jsonBuilding = SitumMapper.buildingToJsonObject(building);
jsonArrayBuildings.put(jsonBuilding);
}
if (buildings.isEmpty()) {
Log.e(PluginHelper.TAG, "onSuccess: you have no buildings. Create one in the Dashboard");
}
invokeCallback(success, convertJsonToArray(jsonArrayBuildings));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
invokeCallback(error, e.getMessage());
}
}
// building, floors, events, indoorPois, outdoorPois, ¿geofences? ¿Paths?
public void fetchTilesFromBuilding(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonBuilding = ReactNativeUtils.convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonBuilding);
getCommunicationManagerInstance().fetchTilesFromBuilding(building.getIdentifier(), new Handler<String>() {
@Override
public void onSuccess(String url) {
try {
Log.d(PluginHelper.TAG, "onSuccess: fetch building tiles offline fetched successfully.");
JSONObject jsonObject = new JSONObject();
jsonObject.put("results", url);
invokeCallback(success, ReactNativeUtils.convertJsonToMap(jsonObject));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in building info response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
// building, floors, events, indoorPois, outdoorPois, ¿geofences? ¿Paths?
public void fetchBuildingInfo(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonBuilding = ReactNativeUtils.convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonBuilding);
getCommunicationManagerInstance().fetchBuildingInfo(building, new Handler<BuildingInfo>() {
@Override
public void onSuccess(BuildingInfo object) {
try {
Log.d(PluginHelper.TAG, "onSuccess: building info fetched successfully.");
JSONObject jsonObject = SitumMapper.buildingInfoToJsonObject(object); // Include geofences to
// parse ? This needs to
// be on sdk
invokeCallback(success, ReactNativeUtils.convertJsonToMap(jsonObject));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in building info response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void fetchFloorsFromBuilding(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonBuilding = ReactNativeUtils.convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonBuilding);
getCommunicationManagerInstance().fetchFloorsFromBuilding(building, new Handler<Collection<Floor>>() {
@Override
public void onSuccess(Collection<Floor> floors) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Floors fetched successfully.");
JSONArray jsonArrayFloors = new JSONArray();
for (Floor floor : floors) {
Log.i(PluginHelper.TAG, "onSuccess: " + floor.getIdentifier());
JSONObject jsonFloor = SitumMapper.floorToJsonObject(floor);
jsonArrayFloors.put(jsonFloor);
}
if (floors.isEmpty()) {
Log.e(PluginHelper.TAG, "onSuccess: you have no floors defined for this building");
}
invokeCallback(success, convertJsonToArray(jsonArrayFloors));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in floor response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void fetchMapFromFloor(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonFloor = ReactNativeUtils.convertMapToJson(buildingMap);
Floor floor = SitumMapper.floorJsonObjectToFloor(jsonFloor);
getCommunicationManagerInstance().fetchMapFromFloor(floor, new Handler<Bitmap>() {
@Override
public void onSuccess(Bitmap bitmap) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Map fetched successfully");
invokeCallback(success, SitumMapper.bitmapToString(bitmap).getString("data"));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure: " + error);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in map download", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void fetchGeofencesFromBuilding(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonBuilding = ReactNativeUtils.convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonBuilding);
getCommunicationManagerInstance().fetchGeofencesFromBuilding(building, new Handler<List<Geofence>>() {
@Override
public void onSuccess(List<Geofence> geofences) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Geofences fetched successfully.");
JSONArray jsonArrayGeofences = new JSONArray();
for (Geofence geofence : geofences) {
Log.i(PluginHelper.TAG, "onSuccess: " + geofence.getIdentifier());
JSONObject jsonoGeofence = SitumMapper.geofenceToJsonObject(geofence);
jsonArrayGeofences.put(jsonoGeofence);
}
if (geofences.isEmpty()) {
Log.e(PluginHelper.TAG, "onSuccess: you have no geofences defined for this building");
}
invokeCallback(success, convertJsonToArray(jsonArrayGeofences));
createAndAssignPolygonsToGeofences(geofences);
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in building info response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void startPositioning(final ReadableMap locationRequestMap,
DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter) {
try {
if (locationListener != null) {
SitumSdk.locationManager().removeLocationListener(locationListener);
}
JSONObject jsonRequst = ReactNativeUtils.convertMapToJson(locationRequestMap);
LocationRequest.Builder locationBuilder = new LocationRequest.Builder();
SitumMapper.locationRequestJSONObjectToLocationRequest(jsonRequst, locationBuilder);
LocationRequest locationRequest = locationBuilder.build();
locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
try {
PluginHelper.this.computedLocation = location; // This is for testing purposes
Log.i(PluginHelper.TAG, "onLocationChanged() called with: location = [" + location + "]");
JSONObject jsonObject = SitumMapper.locationToJsonObject(location);
eventEmitter.emit(EVENT_LOCATION_CHANGED, convertJsonToMap(jsonObject));
} catch (JSONException e) {
eventEmitter.emit(EVENT_LOCATION_ERROR, e.getMessage());
}
}
public void onStatusChanged(@NonNull LocationStatus status) {
try {
Log.i(PluginHelper.TAG, "onStatusChanged() called with: status = [" + status + "]");
JSONObject jsonObject = SitumMapper.locationStatusToJsonObject(status);
eventEmitter.emit(EVENT_LOCATION_STATUS_CHANGED, convertJsonToMap(jsonObject));
} catch (JSONException e) {
eventEmitter.emit(EVENT_LOCATION_ERROR, e.getMessage());
}
}
public void onError(@NonNull Error error) {
Log.e(PluginHelper.TAG, "onError() called with: error = [" + error + "]");
locationListener = null;
try {
JSONObject jsonObject = SitumMapper.locationErrorToJsonObject(error);
eventEmitter.emit(EVENT_LOCATION_ERROR, convertJsonToMap(jsonObject));
} catch (JSONException e) {
Log.e(PluginHelper.TAG, "onError() could not throw error " + e);
}
}
};
try {
SitumSdk.locationManager().addLocationListener(locationListener);
SitumSdk.locationManager().requestLocationUpdates(locationRequest);
} catch (Exception e) {
Log.e(PluginHelper.TAG, "onError() called with: error = [" + e + "]");
}
} catch (Exception e) {
Log.e(TAG, "Unexpected error building response", e.getCause());
eventEmitter.emit(EVENT_LOCATION_ERROR, e.getMessage());
}
}
public void stopPositioning(Callback callback, DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter) {
try {
SitumSdk.locationManager().removeUpdates();
WritableMap map = Arguments.createMap();
map.putBoolean("success", true);
map.putString("message", "Stopped Successfully");
invokeCallback(callback, map);
eventEmitter.emit(EVENT_LOCATION_STOPPED, null);
} catch (Exception e) {
invokeCallback(callback, e.getMessage());
}
}
public void requestDirections(ReadableArray requestArray, Callback success, Callback error,
ReactApplicationContext context) {
try {
JSONObject jsonBuilding = convertMapToJson(Objects.requireNonNull(requestArray.getMap(0)));
JSONObject jsonFrom = convertMapToJson(Objects.requireNonNull(requestArray.getMap(1)));
JSONObject jsonTo = convertMapToJson(Objects.requireNonNull(requestArray.getMap(2)));
JSONObject jsonOptions = null;
if (requestArray.size() >= 4) {
jsonOptions = convertMapToJson(Objects.requireNonNull(requestArray.getMap(3)));
}
DirectionsRequest directionRequest = SitumMapper.jsonObjectToDirectionsRequest(jsonBuilding, jsonFrom,
jsonTo, jsonOptions);
SitumSdk.directionsManager().requestDirections(directionRequest, new Handler<Route>() {
@Override
public void onSuccess(Route route) {
try {
PluginHelper.this.computedRoute = route;
JSONObject jsonRoute = SitumMapper.routeToJsonObject(route);
Log.i(TAG, "onSuccess: Route calculated successfully" + route);
invokeCallback(success, convertJsonToMap(jsonRoute));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(TAG, "onError:" + e.getMessage());
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
e.printStackTrace();
invokeCallback(error, e.getMessage());
}
}
public void fetchPoiCategories(Callback success, Callback error) {
getCommunicationManagerInstance().fetchPoiCategories(new Handler<Collection<PoiCategory>>() {
@Override
public void onSuccess(Collection<PoiCategory> poiCategories) {
try {
Log.d(PluginHelper.TAG, "onSuccess: POI Categories fetched successfully.");
JSONArray jsonaPoiCategories = new JSONArray();
for (PoiCategory poiCategory : poiCategories) {
Log.i(PluginHelper.TAG, "onSuccess: " + poiCategory.getCode() + " - " + poiCategory.getName());
JSONObject jsonoPoiCategory = SitumMapper.poiCategoryToJsonObject(poiCategory);
jsonaPoiCategories.put(jsonoPoiCategory);
}
if (poiCategories.isEmpty()) {
Log.e(PluginHelper.TAG, "onSuccess: you have no categories defined for POIs");
}
invokeCallback(success, convertJsonToArray(jsonaPoiCategories));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
}
public void fetchPoiCategoryIconNormal(ReadableMap categoryMap, Callback success, Callback error) {
try {
JSONObject jsonoCategory = convertMapToJson(categoryMap);
PoiCategory category = SitumMapper.poiCategoryFromJsonObject(jsonoCategory);
getCommunicationManagerInstance().fetchPoiCategoryIconNormal(category, new Handler<Bitmap>() {
@Override
public void onSuccess(Bitmap bitmap) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Poi icon fetched successfully");
JSONObject jsonoMap = SitumMapper.bitmapToString(bitmap);
invokeCallback(success, convertJsonToMap(jsonoMap));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure: " + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in situm POI response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void fetchPoiCategoryIconSelected(ReadableMap categoryMap, Callback success, Callback error) {
try {
JSONObject jsonoCategory = convertMapToJson(categoryMap);
PoiCategory category = SitumMapper.poiCategoryFromJsonObject(jsonoCategory);
getCommunicationManagerInstance().fetchPoiCategoryIconSelected(category, new Handler<Bitmap>() {
@Override
public void onSuccess(Bitmap bitmap) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Poi icon fetched successfully");
JSONObject jsonoMap = SitumMapper.bitmapToString(bitmap);
invokeCallback(success, convertJsonToMap(jsonoMap));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure: " + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in situm POI response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void requestNavigationUpdates(ReadableMap options,
DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter, Context context) {
// 1) Parse and check arguments
if (this.computedRoute == null) {
Log.d(TAG, "Situm >> There is not an stored route so you are not allowed to navigate");
eventEmitter.emit(EVENT_NAVIGATION_ERROR,
"Compute a valid route with requestDirections before trying to navigate within a route");
return;
}
// try??
Route route = this.computedRoute; // args.getJSONObject(0); // Retrieve route from arguments, we do this since
// Route object has internal properties that we do not want to expose
// 2) Build Navigation Arguments
// 2.1) Build Navigation Request
Log.d(TAG, "requestNavigationUpdates executed: passed route: " + route);
NavigationRequest.Builder builder = new NavigationRequest.Builder().route(route);
try {
JSONObject navigationJSONOptions = convertMapToJson(options);
if (navigationJSONOptions.has(SitumMapper.DISTANCE_TO_IGNORE_FIRST_INDICATION)) {
Double distanceToIgnoreFirstIndication = navigationJSONOptions
.getDouble(SitumMapper.DISTANCE_TO_IGNORE_FIRST_INDICATION);
builder.distanceToIgnoreFirstIndication(distanceToIgnoreFirstIndication);
}
if (navigationJSONOptions.has(SitumMapper.OUTSIDE_ROUTE_THRESHOLD)) {
Double outsideRouteThreshold = navigationJSONOptions.getDouble(SitumMapper.OUTSIDE_ROUTE_THRESHOLD);
builder.outsideRouteThreshold(outsideRouteThreshold);
}
if (navigationJSONOptions.has(SitumMapper.DISTANCE_TO_GOAL_THRESHOLD)) {
Double distanceToGoalThreshold = navigationJSONOptions
.getDouble(SitumMapper.DISTANCE_TO_GOAL_THRESHOLD);
builder.distanceToGoalThreshold(distanceToGoalThreshold);
}
if (navigationJSONOptions.has(SitumMapper.DISTANCE_TO_CHANGE_FLOOR_THRESHOLD)) {
Double distanceToChangeFloorThreshold = navigationJSONOptions
.getDouble(SitumMapper.DISTANCE_TO_CHANGE_FLOOR_THRESHOLD);
builder.distanceToChangeFloorThreshold(distanceToChangeFloorThreshold);
}
if (navigationJSONOptions.has(SitumMapper.DISTANCE_TO_CHANGE_INDICATION_THRESHOLD)) {
Double distanceToChangeIndicationThreshold = navigationJSONOptions
.getDouble(SitumMapper.DISTANCE_TO_CHANGE_INDICATION_THRESHOLD);
builder.distanceToChangeIndicationThreshold(distanceToChangeIndicationThreshold);
}
if (navigationJSONOptions.has(SitumMapper.INDICATIONS_INTERVAL)) {
Long indicationsInterval = navigationJSONOptions.getLong(SitumMapper.INDICATIONS_INTERVAL);
builder.indicationsInterval(indicationsInterval);
}
if (navigationJSONOptions.has(SitumMapper.TIME_TO_FIRST_INDICATION)) {
Long timeToFirstIndication = navigationJSONOptions.getLong(SitumMapper.TIME_TO_FIRST_INDICATION);
builder.timeToFirstIndication(timeToFirstIndication);
}
if (navigationJSONOptions.has(SitumMapper.ROUND_INDICATION_STEP)) {
Integer roundIndicationsStep = navigationJSONOptions.getInt(SitumMapper.ROUND_INDICATION_STEP);
builder.roundIndicationsStep(roundIndicationsStep);
}
if (navigationJSONOptions.has(SitumMapper.TIME_TO_IGNORE_UNEXPECTED_FLOOR_CHANGES)) {
Integer timeToIgnoreUnexpectedFloorChanges = navigationJSONOptions
.getInt(SitumMapper.TIME_TO_IGNORE_UNEXPECTED_FLOOR_CHANGES);
builder.timeToIgnoreUnexpectedFloorChanges(timeToIgnoreUnexpectedFloorChanges);
}
if (navigationJSONOptions.has(SitumMapper.IGNORE_LOW_QUALITY_LOCATIONS)) {
Boolean ignoreLowQualityLocations = navigationJSONOptions
.getBoolean(SitumMapper.IGNORE_LOW_QUALITY_LOCATIONS);
builder.ignoreLowQualityLocations(ignoreLowQualityLocations);
}
} catch (Exception e) {
// TODO: handle exception
Log.d(TAG, "Situm >> Unable to retrieve navigation options. Applying default ones");
}
navigationRequest = builder.build();
getNavigationManagerInstance().requestNavigationUpdates(navigationRequest);
}
public void updateNavigationWithLocation(ReadableMap locationMap, Callback success, Callback error) {
try {
// 1) Check for location arguments
JSONObject jsonLocation = convertMapToJson(locationMap);
// 2) Create a Location Object from argument
Location actualLocation = SitumMapper.jsonLocationObjectToLocation(jsonLocation); // Location Objet from
// JSON
// Location actualLocation = PluginHelper.computedLocation;
Log.i(TAG, "UpdateNavigation with Location: " + actualLocation);
// 3) Connect interfaces
getNavigationManagerInstance().updateWithLocation(actualLocation);
invokeCallback(success, "Navigation updated");
} catch (Exception e) {
e.printStackTrace();
invokeCallback(error, e.getMessage());
}
}
public void updateNavigationState(ReadableMap payload) {
try {
Map<String, Object> externalNavigationMap = convertReadableMapToMap(payload);
getNavigationManagerInstance().updateNavigationState(
ExternalNavigation.fromMap(externalNavigationMap)
);
} catch (Exception e) {
e.printStackTrace();
}
}
public void removeNavigationUpdates(Callback callback) {
Log.i(TAG, "Remove navigation updates");
boolean success = getNavigationManagerInstance().removeUpdates();
WritableMap map = Arguments.createMap();
map.putBoolean("success", success);
invokeCallback(callback, map);
}
public void fetchIndoorPOIsFromBuilding(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonoBuilding = convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonoBuilding);
getCommunicationManagerInstance().fetchIndoorPOIsFromBuilding(building, new HashMap<String, Object>(),
new Handler<Collection<Poi>>() {
@Override
public void onSuccess(Collection<Poi> pois) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Pois fetched successfully.");
JSONArray jsonaPois = new JSONArray();
for (Poi poi : pois) {
Log.i(PluginHelper.TAG,
"onSuccess: " + poi.getIdentifier() + " - " + poi.getName() + "-"
+ poi.getCustomFields());
Log.d(PluginHelper.TAG, "Some log that should appear");
JSONObject jsonoPoi = SitumMapper.poiToJsonObject(poi);
jsonaPois.put(jsonoPoi);
}
if (pois.isEmpty()) {
Log.e(PluginHelper.TAG,
"onSuccess: you have no indoor pois defined for this building");
}
invokeCallback(success, convertJsonToArray(jsonaPois));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in poi response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void fetchOutdoorPOIsFromBuilding(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonoBuilding = convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonoBuilding);
getCommunicationManagerInstance().fetchOutdoorPOIsFromBuilding(building, new HashMap<String, Object>(),
new Handler<Collection<Poi>>() {
@Override
public void onSuccess(Collection<Poi> pois) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Floors fetched successfully.");
JSONArray jsonaPois = new JSONArray();
for (Poi poi : pois) {
Log.i(PluginHelper.TAG,
"onSuccess: " + poi.getIdentifier() + " - " + poi.getName());
JSONObject jsonoPoi = SitumMapper.poiToJsonObject(poi);
jsonaPois.put(jsonoPoi);
}
if (pois.isEmpty()) {
Log.e(PluginHelper.TAG,
"onSuccess: you have no outdoor pois defined for this building");
}
invokeCallback(success, convertJsonToArray(jsonaPois));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in poi response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void fetchEventsFromBuilding(ReadableMap buildingMap, Callback success, Callback error) {
try {
JSONObject jsonoBuilding = convertMapToJson(buildingMap);
Building building = SitumMapper.buildingJsonObjectToBuilding(jsonoBuilding);
getCommunicationManagerInstance().fetchEventsFromBuilding(building, new HashMap<String, Object>(),
new Handler<Collection<SitumEvent>>() {
@Override
public void onSuccess(Collection<SitumEvent> situmEvents) {
try {
Log.d(PluginHelper.TAG, "onSuccess: Floors fetched successfully.");
JSONArray jsonaEvents = new JSONArray();
for (SitumEvent situmEvent : situmEvents) {
Log.i(PluginHelper.TAG,
"onSuccess: " + situmEvent.getId() + " - " + situmEvent.getName());
JSONObject jsonoSitumEvent = SitumMapper.situmEventToJsonObject(situmEvent);
jsonaEvents.put(jsonoSitumEvent);
}
if (situmEvents.isEmpty()) {
Log.e(PluginHelper.TAG, "onSuccess: you have no events defined for this building");
}
invokeCallback(success, convertJsonToArray(jsonaEvents));
} catch (JSONException e) {
invokeCallback(error, e.getMessage());
}
}
@Override
public void onFailure(Error e) {
Log.e(PluginHelper.TAG, "onFailure:" + e);
invokeCallback(error, e.getMessage());
}
});
} catch (Exception e) {
Log.e(TAG, "Unexpected error in poi response", e.getCause());
invokeCallback(error, e.getMessage());
}
}
public void requestRealTimeUpdates(ReadableMap options,
DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter) {
try {
// Convert request to native
JSONObject jsonRequest = convertMapToJson(options);
RealTimeRequest request = SitumMapper.jsonObjectRealtimeRequest(jsonRequest);
// Call
realtimeListener = new RealTimeListener() {
@Override
public void onUserLocations(RealTimeData realTimeData) {
Log.d(TAG, "Success retrieving realtime data" + realTimeData);
try {
// Parse information
JSONObject jsonResult = SitumMapper.realtimeDataToJson(realTimeData);
eventEmitter.emit(EVENT_REALTIME_UPDATE, convertJsonToMap(jsonResult));
} catch (Exception e) {
Log.d(TAG, "Error exception realtime data" + e);
eventEmitter.emit(EVENT_REALTIME_ERROR, e.getMessage());
}
}
@Override
public void onError(Error e) {
Log.e(TAG, "Error request realtime data" + e);
eventEmitter.emit(EVENT_REALTIME_ERROR, e.getMessage());
}
};
try {
getRealtimeManagerInstance().requestRealTimeUpdates(request, realtimeListener);
} catch (Exception e) {
Log.e(PluginHelper.TAG, "onError() called with: error = [" + e + "]");
}
} catch (Exception e) {
Log.d(TAG, "exception: " + e);
e.printStackTrace();
eventEmitter.emit(EVENT_REALTIME_ERROR, e.getMessage());
}
}
public void removeRealTimeUpdates() {
Log.i(TAG, "Remove realtime updates");
getRealtimeManagerInstance().removeRealTimeUpdates();
}
public void checkIfPointIsInsideGeoFence(ReadableMap map, Callback callback) {
if (geofencePolygonMap.isEmpty()) {
return;
}
try {
String floorId = null;
if (map.hasKey("floorIdentifier")) {
floorId = map.getString("floorIdentifier");
}
ReadableMap latLngMap = map.getMap("coordinate");
org.locationtech.jts.geom.Point point = geometryFactory
.createPoint(new Coordinate(latLngMap.getDouble("latitude"), latLngMap.getDouble("longitude")));
Geofence geofence = null;
for (Map.Entry<Geofence, Polygon> entry : geofencePolygonMap.entrySet()) {
if (!TextUtils.isEmpty(floorId) && !entry.getKey().getFloorIdentifier().equals(floorId)) {
continue;
}
if (point.within(entry.getValue())) {
geofence = entry.getKey();
Log.i(TAG, "The point is inside the geofence: " + entry.getKey().getName());
}
}
WritableMap responseMap = Arguments.createMap();
responseMap.putBoolean("isInsideGeofence", geofence != null);
if (geofence != null) {
WritableMap geofenceMap = Arguments.createMap();
geofenceMap.putString("name", geofence.getName());
geofenceMap.putString("identifier", geofence.getIdentifier());
responseMap.putMap("geofence", geofenceMap);
}
invokeCallback(callback, responseMap);
} catch (Exception e) {
WritableMap error = Arguments.createMap();
error.putString("error", e.getMessage());
invokeCallback(callback, error);
}
}
public void invalidateCache() {
geofencePolygonMap = new HashMap<>();
getCommunicationManagerInstance().invalidateCache();
}
private void createAndAssignPolygonsToGeofences(List<Geofence> geofences) {
if (geofences.isEmpty()) {
return;
}
for (Geofence geofence : geofences) {
List<Coordinate> jtsCoordinates = new ArrayList<>();
for (Point point : geofence.getPolygonPoints()) {
Coordinate coordinate = new Coordinate(point.getCoordinate().getLatitude(),
point.getCoordinate().getLongitude());
jtsCoordinates.add(coordinate);
}
if (!jtsCoordinates.isEmpty()) {
Polygon polygon = geometryFactory.createPolygon(jtsCoordinates.toArray(new Coordinate[0]));
geofencePolygonMap.put(geofence, polygon);
}
}
}
private void invokeCallback(Callback callback, Object args) {
if (callback != null) {
callback.invoke(args);
}
}
public void onEnterGeofences(DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter) {
emitEnterGeofences = true;
createAndSetGeofenceListener(eventEmitter);
}
public void onExitGeofences(DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter) {
emitExitGeofences = true;
createAndSetGeofenceListener(eventEmitter);
}
private void createAndSetGeofenceListener(DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter) {
GeofenceListener geofenceListener = new GeofenceListener() {
public void onEnteredGeofences(List<Geofence> enteredGeofences) {
if (emitEnterGeofences) {
emitGeofences(EVENT_ENTER_GEOFENCES, enteredGeofences);
}
}
public void onExitedGeofences(List<Geofence> exitedGeofences) {
if (emitExitGeofences) {
emitGeofences(EVENT_EXIT_GEOFENCES, exitedGeofences);
}
}
private void emitGeofences(String event, List<Geofence> geofences) {
eventEmitter.emit(event, SitumMapper.mapList(geofences));
}
};
SitumSdk.locationManager().setGeofenceListener(geofenceListener);
}
public void onSdkInitialized(
DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter,
Context context
) {
navigationListener = _buildNavigationListener(eventEmitter, context);
SitumSdk.navigationManager().addNavigationListener(navigationListener);
}
private NavigationListener _buildNavigationListener(
DeviceEventManagerModule.RCTDeviceEventEmitter eventEmitter,
Context context
) {
return new NavigationListener() {
public void onStart(Route route) {
Log.d(TAG, "NavigationListener.onStart() called with: "+ route);
try {
eventEmitter.emit(EVENT_NAVIGATION_START, convertMapToReadableMap(route.toMap()));
} catch (Exception e) {
Log.e(TAG, "error building onStart() hybrid message with native Route: "+ route);
eventEmitter.emit(EVENT_NAVIGATION_ERROR, e.getMessage());
}
}
public void onProgress(NavigationProgress progress) {
Log.d(TAG, "NavigationListener.onProgress() called with: " + progress);
try {
eventEmitter.emit(EVENT_NAVIGATION_PROGRESS, convertMapToReadableMap(progress.toMap()));
} catch (Exception e) {
Log.e(TAG, "error building onProgress() hybrid message with native NavigationProgress: " + progress);
eventEmitter.emit(EVENT_NAVIGATION_ERROR, e.getMessage());
}
}
public void onDestinationReached(Route route) {
Log.d(TAG, "NavigationListener.onDestinationReached() called with: " + route);
try {
eventEmitter.emit(EVENT_NAVIGATION_DESTINATION_REACHED, convertMapToReadableMap(route.toMap()));
eventEmitter.emit(EVENT_NAVIGATION_FINISHED, convertMapToReadableMap(route.toMap()));
} catch (Exception e) {
Log.e(TAG, "error building onDestinationReached() hybrid message with native Route: "+ route);