@sentiance-react-native/core 6.0.0-beta.9
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.
- package/README.md +24 -0
- package/android/build.gradle +17 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
- package/android/gradle.properties +2 -0
- package/android/gradlew +185 -0
- package/android/gradlew.bat +89 -0
- package/android/package-json-reader.gradle +62 -0
- package/android/sentiance-version-finder.gradle +17 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/SentianceConverter.java +364 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/SentianceEmitter.java +46 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/SentianceHelper.java +207 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/SentianceModule.java +529 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/SentiancePackage.java +30 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/UserLinker.java +34 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/base/AbstractSentianceEmitter.java +59 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/base/AbstractSentianceModule.java +33 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/utils/ErrorCodes.java +16 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/utils/SentianceUtils.java +160 -0
- package/android/src/main/java/com/sentiance/react/bridge/core/utils/UserCreationCompletionHandler.java +31 -0
- package/ios/RNSentianceCore+Converter.h +46 -0
- package/ios/RNSentianceCore+Converter.m +986 -0
- package/ios/RNSentianceCore.h +29 -0
- package/ios/RNSentianceCore.m +1036 -0
- package/ios/RNSentianceCore.podspec +24 -0
- package/ios/RNSentianceCore.xcodeproj/project.pbxproj +290 -0
- package/ios/RNSentianceErrorCodes.h +20 -0
- package/ios/RNSentianceErrorCodes.m +22 -0
- package/ios/RNSentianceHelper.h +31 -0
- package/ios/RNSentianceHelper.m +38 -0
- package/ios/RNSentianceNativeInitialization.h +6 -0
- package/ios/RNSentianceNativeInitialization.m +56 -0
- package/lib/core.js +64 -0
- package/lib/index.d.ts +198 -0
- package/lib/index.js +194 -0
- package/lib/utils.js +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
package com.sentiance.react.bridge.core;
|
|
2
|
+
|
|
3
|
+
import android.location.Location;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.bridge.Arguments;
|
|
6
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
7
|
+
import com.facebook.react.bridge.ReadableMapKeySetIterator;
|
|
8
|
+
import com.facebook.react.bridge.WritableMap;
|
|
9
|
+
import com.sentiance.sdk.DetectionStatus;
|
|
10
|
+
import com.sentiance.sdk.DisableDetectionsResult;
|
|
11
|
+
import com.sentiance.sdk.EnableDetectionsError;
|
|
12
|
+
import com.sentiance.sdk.EnableDetectionsFailureReason;
|
|
13
|
+
import com.sentiance.sdk.EnableDetectionsResult;
|
|
14
|
+
import com.sentiance.sdk.InitState;
|
|
15
|
+
import com.sentiance.sdk.SdkStatus;
|
|
16
|
+
import com.sentiance.sdk.Token;
|
|
17
|
+
import com.sentiance.sdk.UserAccessTokenError;
|
|
18
|
+
import com.sentiance.sdk.UserAccessTokenFailureReason;
|
|
19
|
+
import com.sentiance.sdk.authentication.UserLinkingError;
|
|
20
|
+
import com.sentiance.sdk.authentication.UserLinkingResult;
|
|
21
|
+
import com.sentiance.sdk.detectionupdates.UserActivity;
|
|
22
|
+
import com.sentiance.sdk.detectionupdates.UserActivityType;
|
|
23
|
+
import com.sentiance.sdk.reset.ResetError;
|
|
24
|
+
import com.sentiance.sdk.reset.ResetResult;
|
|
25
|
+
import com.sentiance.sdk.trip.StartTripError;
|
|
26
|
+
import com.sentiance.sdk.trip.StartTripFailureReason;
|
|
27
|
+
import com.sentiance.sdk.trip.StopTripError;
|
|
28
|
+
import com.sentiance.sdk.trip.StopTripFailureReason;
|
|
29
|
+
import com.sentiance.sdk.trip.TransportMode;
|
|
30
|
+
import com.sentiance.sdk.trip.TripType;
|
|
31
|
+
import com.sentiance.sdk.usercreation.UserCreationError;
|
|
32
|
+
import com.sentiance.sdk.usercreation.UserCreationResult;
|
|
33
|
+
import com.sentiance.sdk.usercreation.UserInfo;
|
|
34
|
+
|
|
35
|
+
import java.util.HashMap;
|
|
36
|
+
import java.util.Map;
|
|
37
|
+
|
|
38
|
+
public class SentianceConverter {
|
|
39
|
+
|
|
40
|
+
public static WritableMap createEmptyResult() {
|
|
41
|
+
return Arguments.createMap();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public static Map<String, String> convertReadableMapToMap(ReadableMap inputMap) {
|
|
45
|
+
Map<String, String> map = new HashMap<String, String>();
|
|
46
|
+
ReadableMapKeySetIterator iterator = inputMap.keySetIterator();
|
|
47
|
+
while (iterator.hasNextKey()) {
|
|
48
|
+
String key = iterator.nextKey();
|
|
49
|
+
try {
|
|
50
|
+
map.put(key, String.valueOf(inputMap.getString(key)));
|
|
51
|
+
} catch (Exception ignored) {
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return map;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public static TripType toTripType(final String type) {
|
|
59
|
+
if (type.equals("sdk") || type.equals("TRIP_TYPE_SDK")) {
|
|
60
|
+
return TripType.SDK_TRIP;
|
|
61
|
+
} else if (type.equals("external") || type.equals("TRIP_TYPE_EXTERNAL")) {
|
|
62
|
+
return TripType.EXTERNAL_TRIP;
|
|
63
|
+
} else {
|
|
64
|
+
return TripType.ANY;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public static TransportMode toTransportMode(int t) {
|
|
69
|
+
switch (t) {
|
|
70
|
+
case 2:
|
|
71
|
+
return TransportMode.CAR;
|
|
72
|
+
case 3:
|
|
73
|
+
return TransportMode.BICYCLE;
|
|
74
|
+
case 4:
|
|
75
|
+
return TransportMode.ON_FOOT;
|
|
76
|
+
case 5:
|
|
77
|
+
return TransportMode.TRAIN;
|
|
78
|
+
case 6:
|
|
79
|
+
return TransportMode.TRAM;
|
|
80
|
+
case 7:
|
|
81
|
+
return TransportMode.BUS;
|
|
82
|
+
case 8:
|
|
83
|
+
return TransportMode.PLANE;
|
|
84
|
+
case 9:
|
|
85
|
+
return TransportMode.BOAT;
|
|
86
|
+
case 10:
|
|
87
|
+
return TransportMode.METRO;
|
|
88
|
+
case 11:
|
|
89
|
+
return TransportMode.RUNNING;
|
|
90
|
+
default:
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public static String convertInitState(InitState initState) {
|
|
96
|
+
switch (initState) {
|
|
97
|
+
case NOT_INITIALIZED:
|
|
98
|
+
return "NOT_INITIALIZED";
|
|
99
|
+
case INIT_IN_PROGRESS:
|
|
100
|
+
return "INIT_IN_PROGRESS";
|
|
101
|
+
case INITIALIZED:
|
|
102
|
+
return "INITIALIZED";
|
|
103
|
+
case RESETTING:
|
|
104
|
+
return "RESETTING";
|
|
105
|
+
default:
|
|
106
|
+
return "UNRECOGNIZED_STATE";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public static WritableMap convertToken(Token token) {
|
|
111
|
+
WritableMap map = Arguments.createMap();
|
|
112
|
+
try {
|
|
113
|
+
map.putString("tokenId", token.getTokenId());
|
|
114
|
+
map.putString("expiryDate", String.valueOf(token.getExpiryDate()));
|
|
115
|
+
} catch (Exception ignored) {
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return map;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public static WritableMap convertUserCreationResult(UserCreationResult result) {
|
|
122
|
+
WritableMap map = Arguments.createMap();
|
|
123
|
+
UserInfo userInfo = result.getUserInfo();
|
|
124
|
+
try {
|
|
125
|
+
Token token = userInfo.getToken();
|
|
126
|
+
map.putString("userId", userInfo.getUserId());
|
|
127
|
+
map.putString("tokenId", token.getTokenId());
|
|
128
|
+
map.putString("tokenExpiryDate", token.getExpiryDate().toString());
|
|
129
|
+
map.putBoolean("isTokenExpired", token.isExpired());
|
|
130
|
+
} catch (Exception ignored) {
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return map;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
public static WritableMap convertUserLinkingResult(UserLinkingResult result) {
|
|
137
|
+
WritableMap map = Arguments.createMap();
|
|
138
|
+
UserInfo userInfo = result.getUserInfo();
|
|
139
|
+
try {
|
|
140
|
+
Token token = userInfo.getToken();
|
|
141
|
+
map.putString("userId", userInfo.getUserId());
|
|
142
|
+
map.putString("tokenId", token.getTokenId());
|
|
143
|
+
map.putString("tokenExpiryDate", token.getExpiryDate().toString());
|
|
144
|
+
map.putBoolean("isTokenExpired", token.isExpired());
|
|
145
|
+
} catch (Exception ignored) {
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return map;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public static WritableMap convertInstallId(String installId) {
|
|
152
|
+
WritableMap map = Arguments.createMap();
|
|
153
|
+
try {
|
|
154
|
+
map.putString("installId", installId);
|
|
155
|
+
} catch (Exception ignored) {
|
|
156
|
+
}
|
|
157
|
+
return map;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public static WritableMap convertSdkStatus(SdkStatus status) {
|
|
161
|
+
WritableMap map = Arguments.createMap();
|
|
162
|
+
try {
|
|
163
|
+
map.putString("startStatus", status.startStatus.name());
|
|
164
|
+
map.putString("detectionStatus", status.detectionStatus.name());
|
|
165
|
+
map.putBoolean("canDetect", status.canDetect);
|
|
166
|
+
map.putBoolean("isRemoteEnabled", status.isRemoteEnabled);
|
|
167
|
+
map.putString("locationPermission", status.locationPermission.toString());
|
|
168
|
+
map.putBoolean("isActivityRecognitionPermGranted", status.isActivityRecognitionPermGranted);
|
|
169
|
+
map.putString("locationSetting", status.locationSetting.name());
|
|
170
|
+
map.putBoolean("isAirplaneModeEnabled", status.isAirplaneModeEnabled);
|
|
171
|
+
map.putBoolean("isLocationAvailable", status.isLocationAvailable);
|
|
172
|
+
map.putBoolean("isAccelPresent", status.isAccelPresent);
|
|
173
|
+
map.putBoolean("isGyroPresent", status.isGyroPresent);
|
|
174
|
+
map.putBoolean("isGpsPresent", status.isGpsPresent);
|
|
175
|
+
map.putBoolean("isGooglePlayServicesMissing", status.isGooglePlayServicesMissing);
|
|
176
|
+
map.putBoolean("isBatteryOptimizationEnabled", status.isBatteryOptimizationEnabled);
|
|
177
|
+
map.putBoolean("isBatterySavingEnabled", status.isBatterySavingEnabled);
|
|
178
|
+
map.putBoolean("isBackgroundProcessingRestricted", status.isBackgroundProcessingRestricted);
|
|
179
|
+
map.putBoolean("isPreciseLocationAuthorizationGranted", status.isPreciseLocationPermGranted);
|
|
180
|
+
map.putBoolean("isSchedulingExactAlarmsPermitted", status.isSchedulingExactAlarmsPermitted);
|
|
181
|
+
map.putString("wifiQuotaStatus", status.wifiQuotaStatus.toString());
|
|
182
|
+
map.putString("mobileQuotaStatus", status.mobileQuotaStatus.toString());
|
|
183
|
+
map.putString("diskQuotaStatus", status.diskQuotaStatus.toString());
|
|
184
|
+
} catch (Exception ignored) {
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return map;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
public static WritableMap convertUserActivity(UserActivity activity) {
|
|
191
|
+
WritableMap map = Arguments.createMap();
|
|
192
|
+
try {
|
|
193
|
+
map.putString("type", convertUserActivityType(activity.getActivityType()));
|
|
194
|
+
|
|
195
|
+
//Trip Info
|
|
196
|
+
if (activity.getTripInfo() != null) {
|
|
197
|
+
WritableMap tripInfoMap = Arguments.createMap();
|
|
198
|
+
String tripType = convertTripType(activity.getTripInfo().getTripType());
|
|
199
|
+
tripInfoMap.putString("type", tripType);
|
|
200
|
+
map.putMap("tripInfo", tripInfoMap);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
//Stationary Info
|
|
204
|
+
if (activity.getStationaryInfo() != null) {
|
|
205
|
+
WritableMap stationaryInfoMap = Arguments.createMap();
|
|
206
|
+
if (activity.getStationaryInfo().getLocation() != null) {
|
|
207
|
+
WritableMap locationMap = convertLocation(activity.getStationaryInfo().getLocation());
|
|
208
|
+
stationaryInfoMap.putMap("location", locationMap);
|
|
209
|
+
}
|
|
210
|
+
map.putMap("stationaryInfo", stationaryInfoMap);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
} catch (Exception ignored) {
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return map;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
public static WritableMap convertLocation(Location location) {
|
|
220
|
+
WritableMap locationMap = Arguments.createMap();
|
|
221
|
+
|
|
222
|
+
locationMap.putString("latitude", String.valueOf(location.getLatitude()));
|
|
223
|
+
locationMap.putString("longitude", String.valueOf(location.getLongitude()));
|
|
224
|
+
locationMap.putString("accuracy", String.valueOf(location.getAccuracy()));
|
|
225
|
+
locationMap.putString("altitude", String.valueOf(location.getAltitude()));
|
|
226
|
+
locationMap.putString("provider", location.getProvider());
|
|
227
|
+
|
|
228
|
+
return locationMap;
|
|
229
|
+
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
public static String convertTripType(TripType tripType) {
|
|
233
|
+
switch (tripType) {
|
|
234
|
+
case ANY:
|
|
235
|
+
return "ANY";
|
|
236
|
+
case EXTERNAL_TRIP:
|
|
237
|
+
return "TRIP_TYPE_EXTERNAL";
|
|
238
|
+
case SDK_TRIP:
|
|
239
|
+
return "TRIP_TYPE_SDK";
|
|
240
|
+
default:
|
|
241
|
+
return "TRIP_TYPE_UNRECOGNIZED";
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
public static String convertUserActivityType(UserActivityType activityType) {
|
|
246
|
+
switch (activityType) {
|
|
247
|
+
case TRIP:
|
|
248
|
+
return "USER_ACTIVITY_TYPE_TRIP";
|
|
249
|
+
case STATIONARY:
|
|
250
|
+
return "USER_ACTIVITY_TYPE_STATIONARY";
|
|
251
|
+
case UNKNOWN:
|
|
252
|
+
return "USER_ACTIVITY_TYPE_UNKNOWN";
|
|
253
|
+
default:
|
|
254
|
+
return "USER_ACTIVITY_TYPE_UNRECOGNIZED";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
public static WritableMap convertEnableDetectionsResult(EnableDetectionsResult enableDetectionsResult) {
|
|
259
|
+
return convertDetectionsResult(enableDetectionsResult.getSdkStatus(),
|
|
260
|
+
enableDetectionsResult.getDetectionStatus());
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
public static WritableMap convertDisableDetectionsResult(DisableDetectionsResult disableDetectionsResult) {
|
|
264
|
+
return convertDetectionsResult(disableDetectionsResult.getSdkStatus(),
|
|
265
|
+
disableDetectionsResult.getDetectionStatus());
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
public static WritableMap convertResetResult(ResetResult resetResult) {
|
|
269
|
+
WritableMap result = Arguments.createMap();
|
|
270
|
+
result.putString("initState", resetResult.getInitState().name());
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private static WritableMap convertDetectionsResult(SdkStatus sdkStatus, DetectionStatus detectionStatus) {
|
|
275
|
+
WritableMap result = Arguments.createMap();
|
|
276
|
+
WritableMap sdkStatusMap = convertSdkStatus(sdkStatus);
|
|
277
|
+
|
|
278
|
+
result.putMap("sdkStatus", sdkStatusMap);
|
|
279
|
+
result.putString("detectionStatus", detectionStatus.toString());
|
|
280
|
+
|
|
281
|
+
return result;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
public static String stringifyEnableDetectionsError(EnableDetectionsError error) {
|
|
285
|
+
EnableDetectionsFailureReason reason = error.getReason();
|
|
286
|
+
String details = "";
|
|
287
|
+
switch (reason) {
|
|
288
|
+
case NO_USER:
|
|
289
|
+
details = "No Sentiance user present on the device.";
|
|
290
|
+
break;
|
|
291
|
+
case PAST_EXPIRY_DATE:
|
|
292
|
+
details = "Expiry date is in past.";
|
|
293
|
+
break;
|
|
294
|
+
case USER_DISABLED_REMOTELY:
|
|
295
|
+
details = "The user is disabled remotely.";
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
return String.format("Reason: %s - %s", reason.name(), details);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
public static String stringifyUserLinkingError(UserLinkingError error) {
|
|
302
|
+
return String.format("Reason: %s - %s", error.getReason().name(), error.getDetails());
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
public static String stringifyUserCreationError(UserCreationError error) {
|
|
306
|
+
return String.format("Reason: %s - %s", error.getReason().name(), error.getDetails());
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
public static String stringifyResetError(ResetError error) {
|
|
310
|
+
return String.format("%s - caused by: %s", error.getReason().name(), error.getException());
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
public static String stringifyStartTripError(StartTripError error) {
|
|
314
|
+
StartTripFailureReason reason = error.getReason();
|
|
315
|
+
String details = "";
|
|
316
|
+
switch (reason) {
|
|
317
|
+
case NO_USER:
|
|
318
|
+
details = "No Sentiance user present on the device.";
|
|
319
|
+
break;
|
|
320
|
+
case DETECTIONS_DISABLED:
|
|
321
|
+
details = "Detections are disabled. Enable them first before starting a trip.";
|
|
322
|
+
break;
|
|
323
|
+
case DETECTIONS_BLOCKED:
|
|
324
|
+
details = "Detections are enabled but not running. Check the SDK's status to find out why.";
|
|
325
|
+
break;
|
|
326
|
+
case TRIP_ALREADY_STARTED:
|
|
327
|
+
details = "An external trip is already started. To start a new trip, call `stopTrip()` first.";
|
|
328
|
+
break;
|
|
329
|
+
case USER_DISABLED_REMOTELY:
|
|
330
|
+
details = "The user is disabled remotely.";
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
return String.format("Reason: %s - %s", reason.name(), details);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
public static String stringifyStopTripError(StopTripError error) {
|
|
337
|
+
StopTripFailureReason reason = error.getReason();
|
|
338
|
+
String details = "";
|
|
339
|
+
switch (reason) {
|
|
340
|
+
case NO_ONGOING_TRIP:
|
|
341
|
+
details = "There is no ongoing external trip.";
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
return String.format("Reason: %s - %s", reason.name(), details);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
public static String stringifyUserAccessTokenError(UserAccessTokenError error) {
|
|
348
|
+
UserAccessTokenFailureReason reason = error.getReason();
|
|
349
|
+
String details = "";
|
|
350
|
+
switch (reason) {
|
|
351
|
+
case NO_USER:
|
|
352
|
+
details = "No Sentiance user is present on device.";
|
|
353
|
+
break;
|
|
354
|
+
case NETWORK_ERROR:
|
|
355
|
+
details = "A network error occurred. This can happen when the existing token is expired, " +
|
|
356
|
+
"and it was not possible to contact the Sentiance Platform to refresh it.";
|
|
357
|
+
break;
|
|
358
|
+
case USER_DISABLED_REMOTELY:
|
|
359
|
+
details = "The user is disabled remotely.";
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
return String.format("Reason: %s - %s", reason.name(), details);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
package com.sentiance.react.bridge.core;
|
|
2
|
+
|
|
3
|
+
import static com.sentiance.react.bridge.core.SentianceConverter.convertInstallId;
|
|
4
|
+
import static com.sentiance.react.bridge.core.SentianceConverter.convertSdkStatus;
|
|
5
|
+
import static com.sentiance.react.bridge.core.SentianceConverter.convertUserActivity;
|
|
6
|
+
|
|
7
|
+
import android.content.Context;
|
|
8
|
+
|
|
9
|
+
import com.facebook.react.bridge.Arguments;
|
|
10
|
+
import com.sentiance.react.bridge.core.base.AbstractSentianceEmitter;
|
|
11
|
+
import com.sentiance.sdk.SdkStatus;
|
|
12
|
+
import com.sentiance.sdk.detectionupdates.UserActivity;
|
|
13
|
+
|
|
14
|
+
public class SentianceEmitter extends AbstractSentianceEmitter {
|
|
15
|
+
private static final String USER_LINK = "SENTIANCE_USER_LINK_EVENT";
|
|
16
|
+
private static final String STATUS_UPDATE = "SENTIANCE_STATUS_UPDATE_EVENT";
|
|
17
|
+
private static final String USER_ACTIVITY_UPDATE = "SENTIANCE_USER_ACTIVITY_UPDATE_EVENT";
|
|
18
|
+
private static final String ON_DETECTIONS_ENABLED = "SENTIANCE_ON_DETECTIONS_ENABLED_EVENT";
|
|
19
|
+
private static final String ON_TRIP_TIMED_OUT = "SENTIANCE_ON_TRIP_TIMED_OUT_EVENT";
|
|
20
|
+
|
|
21
|
+
public SentianceEmitter(Context context) {
|
|
22
|
+
super(context);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void sendUserLinkEvent(String installId) {
|
|
26
|
+
sendEvent(USER_LINK, convertInstallId(installId));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public void sendStatusUpdateEvent(SdkStatus status) {
|
|
30
|
+
sendEvent(STATUS_UPDATE, convertSdkStatus(status));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void sendUserActivityUpdate(UserActivity userActivity) {
|
|
34
|
+
sendEvent(USER_ACTIVITY_UPDATE, convertUserActivity(userActivity));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
void sendOnDetectionsEnabledEvent(SdkStatus status) {
|
|
38
|
+
sendEvent(ON_DETECTIONS_ENABLED, convertSdkStatus(status));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
void sendOnTripTimedOutEvent() {
|
|
42
|
+
sendEvent(ON_TRIP_TIMED_OUT, Arguments.createMap());
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
package com.sentiance.react.bridge.core;
|
|
2
|
+
|
|
3
|
+
import static com.sentiance.react.bridge.core.utils.ErrorCodes.E_SDK_DISABLE_DETECTIONS_ERROR;
|
|
4
|
+
import static com.sentiance.react.bridge.core.utils.ErrorCodes.E_SDK_ENABLE_DETECTIONS_ERROR;
|
|
5
|
+
import static com.sentiance.react.bridge.core.utils.ErrorCodes.E_SDK_USER_LINK_AUTH_CODE_ERROR;
|
|
6
|
+
import static com.sentiance.react.bridge.core.utils.ErrorCodes.E_SDK_USER_LINK_ERROR;
|
|
7
|
+
|
|
8
|
+
import android.app.Notification;
|
|
9
|
+
import android.content.Context;
|
|
10
|
+
|
|
11
|
+
import androidx.annotation.NonNull;
|
|
12
|
+
import androidx.annotation.Nullable;
|
|
13
|
+
|
|
14
|
+
import com.facebook.react.bridge.Promise;
|
|
15
|
+
import com.sentiance.react.bridge.core.utils.SentianceUtils;
|
|
16
|
+
import com.sentiance.sdk.DisableDetectionsResult;
|
|
17
|
+
import com.sentiance.sdk.EnableDetectionsError;
|
|
18
|
+
import com.sentiance.sdk.EnableDetectionsResult;
|
|
19
|
+
import com.sentiance.sdk.OnSdkStatusUpdateHandler;
|
|
20
|
+
import com.sentiance.sdk.SdkStatus;
|
|
21
|
+
import com.sentiance.sdk.SdkStatusUpdateListener;
|
|
22
|
+
import com.sentiance.sdk.Sentiance;
|
|
23
|
+
import com.sentiance.sdk.authentication.UserLinkingError;
|
|
24
|
+
import com.sentiance.sdk.init.InitializationFailureReason;
|
|
25
|
+
import com.sentiance.sdk.init.InitializationResult;
|
|
26
|
+
import com.sentiance.sdk.init.SentianceOptions;
|
|
27
|
+
import com.sentiance.sdk.pendingoperation.PendingOperation;
|
|
28
|
+
import com.sentiance.sdk.usercreation.UserCreationError;
|
|
29
|
+
import com.sentiance.sdk.usercreation.UserCreationOptions;
|
|
30
|
+
import com.sentiance.sdk.usercreation.UserCreationResult;
|
|
31
|
+
|
|
32
|
+
import java.lang.ref.WeakReference;
|
|
33
|
+
import java.util.Date;
|
|
34
|
+
|
|
35
|
+
public class SentianceHelper {
|
|
36
|
+
private static SentianceHelper sentianceHelper;
|
|
37
|
+
|
|
38
|
+
private final SentianceEmitter emitter;
|
|
39
|
+
private final WeakReference<Context> weakContext;
|
|
40
|
+
private final UserLinker userLinker;
|
|
41
|
+
|
|
42
|
+
private final OnSdkStatusUpdateHandler onSdkStatusUpdateHandler = this::onSdkStatusUpdated;
|
|
43
|
+
|
|
44
|
+
private final SdkStatusUpdateListener onSdkStatusUpdateListener = this::onSdkStatusUpdated;
|
|
45
|
+
|
|
46
|
+
SdkStatusUpdateListener getOnSdkStatusUpdateListener() {
|
|
47
|
+
return onSdkStatusUpdateListener;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
protected SentianceHelper(Context context) {
|
|
52
|
+
emitter = new SentianceEmitter(context);
|
|
53
|
+
weakContext = new WeakReference<>(context);
|
|
54
|
+
userLinker = new UserLinker(emitter);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public static SentianceHelper getInstance(Context context) {
|
|
58
|
+
if (sentianceHelper == null) {
|
|
59
|
+
synchronized (SentianceHelper.class) {
|
|
60
|
+
sentianceHelper = new SentianceHelper(context);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return sentianceHelper;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
private void onSdkStatusUpdated(@NonNull SdkStatus status) {
|
|
67
|
+
emitter.sendStatusUpdateEvent(status);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
void userLinkCallback(final Boolean linkResult) {
|
|
71
|
+
userLinker.setUserLinkResult(linkResult);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
public InitializationResult initializeSDK() {
|
|
75
|
+
return initializeSDK(false);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public InitializationResult initializeSDK(boolean isAppSessionDataCollectionEnabled) {
|
|
79
|
+
Context context = weakContext.get();
|
|
80
|
+
if (context == null) {
|
|
81
|
+
return new InitializationResult(
|
|
82
|
+
false, InitializationFailureReason.EXCEPTION_OR_ERROR, new Exception("Context is null"));
|
|
83
|
+
}
|
|
84
|
+
Notification notification = SentianceUtils.createNotificationFromManifestData(weakContext);
|
|
85
|
+
if (notification == null) {
|
|
86
|
+
return new InitializationResult(false, InitializationFailureReason.EXCEPTION_OR_ERROR,
|
|
87
|
+
new Throwable("null context while creating notification"));
|
|
88
|
+
}
|
|
89
|
+
Sentiance sentiance = Sentiance.getInstance(context);
|
|
90
|
+
|
|
91
|
+
SentianceOptions options = new SentianceOptions.Builder(context)
|
|
92
|
+
.setNotification(notification, SentianceUtils.getSentianceNotificationId(weakContext))
|
|
93
|
+
.collectAppSessionData(isAppSessionDataCollectionEnabled)
|
|
94
|
+
.build();
|
|
95
|
+
InitializationResult result = sentiance.initialize(options);
|
|
96
|
+
if (result.isSuccessful()) {
|
|
97
|
+
sentiance.setSdkStatusUpdateListener(onSdkStatusUpdateListener);
|
|
98
|
+
}
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
PendingOperation<UserCreationResult, UserCreationError> createLinkedUser(String authCode, String platformUrl) {
|
|
103
|
+
UserCreationOptions.Builder builder = new UserCreationOptions.Builder(authCode);
|
|
104
|
+
if (platformUrl != null) {
|
|
105
|
+
builder.setPlatformUrl(platformUrl);
|
|
106
|
+
}
|
|
107
|
+
return getSentiance().createUser(builder.build());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
PendingOperation<UserCreationResult, UserCreationError> createUnlinkedUser(String appId, String secret,
|
|
111
|
+
String platformUrl) {
|
|
112
|
+
UserCreationOptions.Builder builder = new UserCreationOptions.Builder(appId, secret, UserLinker.NO_OP);
|
|
113
|
+
if (platformUrl != null) {
|
|
114
|
+
builder.setPlatformUrl(platformUrl);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return getSentiance().createUser(builder.build());
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
PendingOperation<UserCreationResult, UserCreationError> createLinkedUser(String appId, String secret,
|
|
121
|
+
String platformUrl) {
|
|
122
|
+
UserCreationOptions.Builder builder = new UserCreationOptions.Builder(appId, secret, userLinker );
|
|
123
|
+
if (platformUrl != null) {
|
|
124
|
+
builder.setPlatformUrl(platformUrl);
|
|
125
|
+
}
|
|
126
|
+
return getSentiance().createUser(builder.build());
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
void enableDetections(@Nullable final Promise promise) {
|
|
130
|
+
enableDetections(null, promise);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
void enableDetections(@Nullable Long stopTime, @Nullable final Promise promise) {
|
|
134
|
+
Sentiance sentiance = getSentiance();
|
|
135
|
+
Date stopDate = null;
|
|
136
|
+
|
|
137
|
+
if (stopTime != null) {
|
|
138
|
+
stopDate = new Date(stopTime);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
sentiance.enableDetections(stopDate)
|
|
142
|
+
.addOnCompleteListener(pendingOperation -> {
|
|
143
|
+
if (pendingOperation.isSuccessful()) {
|
|
144
|
+
EnableDetectionsResult result = pendingOperation.getResult();
|
|
145
|
+
emitter.sendOnDetectionsEnabledEvent(result.getSdkStatus());
|
|
146
|
+
if (promise != null) {
|
|
147
|
+
promise.resolve(SentianceConverter.convertEnableDetectionsResult(result));
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
EnableDetectionsError error = pendingOperation.getError();
|
|
151
|
+
if (promise != null) {
|
|
152
|
+
promise.reject(E_SDK_ENABLE_DETECTIONS_ERROR,
|
|
153
|
+
SentianceConverter.stringifyEnableDetectionsError(error));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
void disableDetections(final Promise promise) {
|
|
160
|
+
Sentiance sentiance = getSentiance();
|
|
161
|
+
sentiance.disableDetections()
|
|
162
|
+
.addOnCompleteListener(pendingOperation -> {
|
|
163
|
+
if (pendingOperation.isSuccessful()) {
|
|
164
|
+
DisableDetectionsResult result = pendingOperation.getResult();
|
|
165
|
+
promise.resolve(SentianceConverter.convertDisableDetectionsResult(result));
|
|
166
|
+
} else {
|
|
167
|
+
promise.reject(E_SDK_DISABLE_DETECTIONS_ERROR, "");
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
void linkUser(final Promise promise) {
|
|
173
|
+
Sentiance sentiance = getSentiance();
|
|
174
|
+
sentiance.linkUser(userLinker)
|
|
175
|
+
.addOnCompleteListener(pendingOperation -> {
|
|
176
|
+
if (pendingOperation.isSuccessful()) {
|
|
177
|
+
promise.resolve(SentianceConverter.convertUserLinkingResult(pendingOperation.getResult()));
|
|
178
|
+
} else {
|
|
179
|
+
UserLinkingError error = pendingOperation.getError();
|
|
180
|
+
promise.reject(E_SDK_USER_LINK_ERROR,
|
|
181
|
+
SentianceConverter.stringifyUserLinkingError(error));
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
void linkUser(String authCode, final Promise promise) {
|
|
187
|
+
Sentiance sentiance = getSentiance();
|
|
188
|
+
sentiance.linkUser(authCode)
|
|
189
|
+
.addOnCompleteListener(pendingOperation -> {
|
|
190
|
+
if (pendingOperation.isSuccessful()) {
|
|
191
|
+
promise.resolve(SentianceConverter.convertUserLinkingResult(pendingOperation.getResult()));
|
|
192
|
+
} else {
|
|
193
|
+
UserLinkingError error = pendingOperation.getError();
|
|
194
|
+
promise.reject(E_SDK_USER_LINK_AUTH_CODE_ERROR,
|
|
195
|
+
SentianceConverter.stringifyUserLinkingError(error));
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private Sentiance getSentiance() {
|
|
201
|
+
Context context = weakContext.get();
|
|
202
|
+
if (context == null) {
|
|
203
|
+
throw new IllegalStateException("Context is null.");
|
|
204
|
+
}
|
|
205
|
+
return Sentiance.getInstance(context);
|
|
206
|
+
}
|
|
207
|
+
}
|