-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilsModule.java
214 lines (189 loc) · 8.61 KB
/
UtilsModule.java
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
package com.mapsindoorsrn.core;
import androidx.annotation.NonNull;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.google.gson.Gson;
import com.mapsindoors.core.MPCollisionHandling;
import com.mapsindoors.core.MPGeometry;
import com.mapsindoors.core.MPLocation;
import com.mapsindoors.core.MPLocationSettings;
import com.mapsindoors.core.MPMultiPolygonGeometry;
import com.mapsindoors.core.MPPOIType;
import com.mapsindoors.core.MPPoint;
import com.mapsindoors.core.MPPolygonGeometry;
import com.mapsindoors.core.MPSolution;
import com.mapsindoors.core.MPVenue;
import com.mapsindoors.core.MapsIndoors;
import com.mapsindoors.core.MPJsonParser;
import com.mapsindoors.core.errors.MIError;
import com.mapsindoors.core.errors.MIErrorEnum;
import com.mapsindoorsrn.core.models.MPError;
import java.util.Locale;
public class UtilsModule extends ReactContextBaseJavaModule {
private final Gson gson = new Gson();
public UtilsModule(ReactApplicationContext reactApplicationContext) {
super(reactApplicationContext);
}
@NonNull
@Override
public String getName() {
return "UtilsModule";
}
private void reject(Promise promise, MIError error) {
promise.reject("UtilError", gson.toJson(MPError.fromMIError(error)));
}
@ReactMethod
public void venueHasGraph(String venueId, final Promise promise) {
if (MapsIndoors.getVenues() != null) {
MPVenue venue = MapsIndoors.getVenues().getVenueById(venueId);
if (venue != null) {
promise.resolve(venue.hasGraph());
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Cannot find venue with ID: " + venueId));
}
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Venues are not available"));
}
}
@ReactMethod
public void pointAngleBetween(String point1, String point2, final Promise promise) {
MPPoint it = MPJsonParser.parse(point1, MPPoint.class);
MPPoint other = MPJsonParser.parse(point2, MPPoint.class);
if (it != null && other != null) {
promise.resolve(it.angleBetween(other));
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "One or more points are null: " + point1 + ", " + point2));
}
}
@ReactMethod
public void pointDistanceTo(String point1, String point2, final Promise promise) {
MPPoint it = MPJsonParser.parse(point1, MPPoint.class);
MPPoint other = MPJsonParser.parse(point2, MPPoint.class);
if (it != null && other != null) {
promise.resolve(it.distanceTo(other));
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "One or more points are null: " + point1 + ", " + point2));
}
}
@ReactMethod
public void geometryIsInside(String pointString, String geometryString, final Promise promise) {
MPPoint point = MPJsonParser.parse(pointString, MPPoint.class);
MPGeometry geometry = MPJsonParser.parse(geometryString, MPGeometry.class);
if (point != null && geometry != null) {
promise.resolve(geometry.isInside(point.getLatLng()));
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Point (" + point + ") and/or Geometry (" + geometry + ") is null"));
}
}
@ReactMethod
public void geometryArea(String geometryString, final Promise promise) {
MPGeometry geometry = MPJsonParser.parse(geometryString, MPGeometry.class);
if (geometry != null) {
promise.resolve(geometry.getArea());
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Geometry is null"));
}
}
@ReactMethod
public void polygonDistanceToClosestEdge(String pointString, String geometryString, final Promise promise) {
MPPoint point = MPJsonParser.parse(pointString, MPPoint.class);
MPGeometry geometry = MPJsonParser.parse(geometryString, MPGeometry.class);
if (geometry != null && point != null) {
if (MPGeometry.POLYGON.equals(geometry.getType()) ) {
MPPolygonGeometry polygonGeometry = (MPPolygonGeometry) geometry;
promise.resolve(polygonGeometry.getSquaredDistanceToClosestEdge(point));
} else if (MPGeometry.MULTIPOLYGON.equals(geometry.getType())) {
MPMultiPolygonGeometry multiPolygonGeometry = (MPMultiPolygonGeometry) geometry;
promise.resolve(multiPolygonGeometry.getSquaredDistanceToClosestEdge(point));
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Geometry of incompatible type: " + geometry.getType() +
" (should be " + MPGeometry.POLYGON + " or "+ MPGeometry.MULTIPOLYGON + ")"));
}
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Point (" + point + ") or Geometry (" + geometry + ") is null"));
}
}
@ReactMethod
public void parseMapClientUrl(String venueId, String locationId, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
promise.resolve(solution.parseMapClientUrl(venueId, locationId));
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Solution is null, MapsIndoors is probably not ready yet"));
}
}
@ReactMethod
public void setCollisionHandling(Double collisionHandling, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
solution.getConfig().setCollisionHandling(MPCollisionHandling.fromValue(collisionHandling.intValue()));
}
promise.resolve(null);
}
@ReactMethod
public void enableClustering(boolean enable, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
solution.getConfig().setEnableClustering(enable);
}
promise.resolve(null);
}
@ReactMethod
public void setNewSelection(boolean enable, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
solution.getConfig().setNewSelection(enable);
}
promise.resolve(null);
}
@ReactMethod
public void setExtrusionOpacity(Double extrusionOpacity, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
solution.getConfig().getSettings3D().setExtrusionOpacity(extrusionOpacity.floatValue());
}
promise.resolve(null);
}
@ReactMethod
public void setWallOpacity(Double wallOpacity, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
solution.getConfig().getSettings3D().setWallOpacity(wallOpacity.floatValue());
}
promise.resolve(null);
}
@ReactMethod
public void setSelectable(String id, boolean selectable, final Promise promise) {
MPSolution solution = MapsIndoors.getSolution();
if (solution != null) {
if (id.equals("solution")) {
solution.getConfig().getLocationSettings().setSelectable(selectable);
} else {
MPLocation location = MapsIndoors.getLocationById(id);
if (location != null) {
if (location.getLocationSettings() == null) {
location.setLocationSettings(new MPLocationSettings(selectable));
}else {
location.getLocationSettings().setSelectable(selectable);
}
}else {
for (MPPOIType type : solution.getTypes()) {
if (type.getName().equals(id.toLowerCase(Locale.ROOT))) {
if (type.getLocationSettings() == null) {
type.setLocationSettings(new MPLocationSettings(selectable));
}else {
type.getLocationSettings().setSelectable(selectable);
}
break;
}
}
}
}
} else {
reject(promise, new MIError(MIErrorEnum.UNKNOWN_ERROR, "Solution is null, MapsIndoors needs to be loaded before setting selectable"));
}
promise.resolve(null);
}
}