24
24
import com .instabug .bug .PromptOption ;
25
25
import com .instabug .bug .invocation .InvocationMode ;
26
26
import com .instabug .bug .invocation .InvocationOption ;
27
- import com .instabug .chat .InstabugChat ;
28
27
import com .instabug .crash .CrashReporting ;
29
28
import com .instabug .featuresrequest .FeatureRequests ;
30
29
import com .instabug .featuresrequest .ActionType ;
37
36
import com .instabug .library .invocation .InstabugInvocationMode ;
38
37
import com .instabug .library .InstabugColorTheme ;
39
38
import com .instabug .library .invocation .OnInvokeCallback ;
40
- import com .instabug .library .invocation .util .InstabugVideoRecordingButtonCorner ;
41
39
import com .instabug .library .invocation .util .InstabugVideoRecordingButtonPosition ;
42
40
import com .instabug .library .logging .InstabugLog ;
43
41
import com .instabug .library .bugreporting .model .ReportCategory ;
44
42
import com .instabug .library .ui .onboarding .WelcomeMessage ;
45
43
import com .instabug .library .InstabugCustomTextPlaceHolder ;
46
44
import com .instabug .library .model .Report ;
47
45
import com .instabug .library .user .UserEventParam ;
48
- import com .instabug .library .OnSdkDismissedCallback ;
49
- import com .instabug .library .bugreporting .model .Bug ;
50
46
import com .instabug .library .visualusersteps .State ;
51
47
52
48
import com .instabug .reactlibrary .utils .ArrayUtil ;
56
52
import com .instabug .survey .Survey ;
57
53
import com .instabug .survey .Surveys ;
58
54
55
+ import org .json .JSONArray ;
59
56
import org .json .JSONException ;
60
57
import org .json .JSONObject ;
58
+ import org .json .JSONTokener ;
61
59
62
- import java .lang .reflect .Array ;
63
60
import java .lang .reflect .InvocationTargetException ;
64
61
import java .lang .reflect .Method ;
65
62
import java .util .ArrayList ;
66
63
import java .util .Arrays ;
67
64
import java .util .HashMap ;
65
+ import java .util .Iterator ;
68
66
import java .util .List ;
69
67
import java .util .Locale ;
70
68
import java .util .Map ;
@@ -227,14 +225,42 @@ public void run() {
227
225
}
228
226
}
229
227
228
+ /**
229
+ * invoke sdk manually with desire invocation mode
230
+ *
231
+ * @param invocationMode the invocation mode
232
+ */
233
+ @ ReactMethod
234
+ public void invokeWithInvocationMode (String invocationMode ) {
235
+ InstabugInvocationMode mode ;
236
+
237
+ if (invocationMode .equals (INVOCATION_MODE_NEW_BUG )) {
238
+ mode = InstabugInvocationMode .NEW_BUG ;
239
+ } else if (invocationMode .equals (INVOCATION_MODE_NEW_FEEDBACK )) {
240
+ mode = InstabugInvocationMode .NEW_FEEDBACK ;
241
+ } else if (invocationMode .equals (INVOCATION_MODE_NEW_CHAT )) {
242
+ mode = InstabugInvocationMode .NEW_CHAT ;
243
+ } else if (invocationMode .equals (INVOCATION_MODE_CHATS_LIST )) {
244
+ mode = InstabugInvocationMode .CHATS_LIST ;
245
+ } else {
246
+ mode = InstabugInvocationMode .PROMPT_OPTION ;
247
+ }
248
+
249
+ try {
250
+ mInstabug .invoke (mode );
251
+ } catch (Exception e ) {
252
+ e .printStackTrace ();
253
+ }
254
+ }
255
+
230
256
/**
231
257
* invoke sdk manually with desire invocation mode
232
258
*
233
259
* @param invocationMode the invocation mode
234
260
* @param invocationOptions the array of invocation options
235
261
*/
236
262
@ ReactMethod
237
- public void invokeWithInvocationMode (String invocationMode , ReadableArray invocationOptions ) {
263
+ public void invokeWithInvocationModeAndOptions (String invocationMode , ReadableArray invocationOptions ) {
238
264
InvocationMode mode ;
239
265
240
266
@@ -250,8 +276,6 @@ public void invokeWithInvocationMode(String invocationMode, ReadableArray invoca
250
276
mode = InvocationMode .PROMPT_OPTION ;
251
277
}
252
278
253
- Log .d ("where is" , invocationMode + " " + mode );
254
-
255
279
Object [] objectArray = ArrayUtil .toArray (invocationOptions );
256
280
String [] stringArray = Arrays .copyOf (objectArray , objectArray .length , String [].class );
257
281
@@ -275,8 +299,6 @@ public void invokeWithInvocationMode(String invocationMode, ReadableArray invoca
275
299
break ;
276
300
}
277
301
}
278
- Log .d ("where is" , Arrays .toString (stringArray ) + " " + Arrays .toString (arrayOfParsedOptions ));
279
-
280
302
try {
281
303
BugReporting .invoke (mode , arrayOfParsedOptions );
282
304
} catch (Exception e ) {
@@ -1309,31 +1331,67 @@ public void onInvoke() {
1309
1331
@ ReactMethod
1310
1332
public void setPreSendingHandler (final Callback preSendingHandler ) {
1311
1333
try {
1312
- Runnable preSendingRunnable = new Runnable () {
1313
- @ Override
1314
- public void run () {
1315
- sendEvent (getReactApplicationContext (), "IBGpreSendingHandler" , null );
1316
- }
1317
- };
1318
-
1319
1334
1320
1335
Instabug .onReportSubmitHandler (new Report .OnReportCreatedListener () {
1321
1336
@ Override
1322
1337
public void onReportCreated (Report report ) {
1323
1338
WritableMap reportParam = Arguments .createMap ();
1324
- reportParam .putArray ("tagsArray" , ( WritableArray ) report .getTags ());
1325
- reportParam .putArray ("consoleLogs" , ( WritableArray ) report .getConsoleLog ());
1339
+ reportParam .putArray ("tagsArray" , convertArrayListToWritableArray ( report .getTags () ));
1340
+ reportParam .putArray ("consoleLogs" , convertArrayListToWritableArray ( report .getConsoleLog () ));
1326
1341
reportParam .putString ("userData" , report .getUserData ());
1327
- reportParam .putMap ("userAttributes" , ( WritableMap ) report .getUserAttributes ());
1328
- reportParam .putMap ("fileAttachments" , ( WritableMap ) report .getFileAttachments ());
1329
- sendEvent (getReactApplicationContext (), "IBGpostInvocationHandler " , reportParam );
1342
+ reportParam .putMap ("userAttributes" , convertFromHashMapToWriteableMap ( report .getUserAttributes () ));
1343
+ reportParam .putMap ("fileAttachments" , convertFromHashMapToWriteableMap ( report .getFileAttachments () ));
1344
+ sendEvent (getReactApplicationContext (), "IBGpreSendingHandler " , reportParam );
1330
1345
}
1331
1346
});
1332
1347
} catch (java .lang .Exception exception ) {
1333
1348
exception .printStackTrace ();
1334
1349
}
1335
1350
}
1336
1351
1352
+ private WritableMap convertFromHashMapToWriteableMap (HashMap hashMap ) {
1353
+ WritableMap writableMap = new WritableNativeMap ();
1354
+ for (int i = 0 ; i < hashMap .size (); i ++) {
1355
+ Object key = hashMap .keySet ().toArray ()[i ];
1356
+ Object value = hashMap .get (key );
1357
+ writableMap .putString ((String ) key ,(String ) value );
1358
+ }
1359
+ return writableMap ;
1360
+ }
1361
+
1362
+ private static JSONObject objectToJSONObject (Object object ){
1363
+ Object json = null ;
1364
+ JSONObject jsonObject = null ;
1365
+ try {
1366
+ json = new JSONTokener (object .toString ()).nextValue ();
1367
+ } catch (JSONException e ) {
1368
+ e .printStackTrace ();
1369
+ }
1370
+ if (json instanceof JSONObject ) {
1371
+ jsonObject = (JSONObject ) json ;
1372
+ }
1373
+ return jsonObject ;
1374
+ }
1375
+
1376
+ private WritableArray convertArrayListToWritableArray (List arrayList ) {
1377
+ WritableArray writableArray = new WritableNativeArray ();
1378
+
1379
+ for (int i = 0 ; i < arrayList .size (); i ++) {
1380
+ Object object = arrayList .get (i );
1381
+
1382
+ if (object instanceof String ) {
1383
+ writableArray .pushString ((String ) object );
1384
+ }
1385
+ else {
1386
+ JSONObject jsonObject = objectToJSONObject (object );
1387
+ writableArray .pushMap ((WritableMap ) jsonObject );
1388
+ }
1389
+ }
1390
+
1391
+ return writableArray ;
1392
+
1393
+ }
1394
+
1337
1395
/**
1338
1396
* Sets a block of code to be executed right after the SDK's UI is dismissed.
1339
1397
* This block is executed on the UI thread. Could be used for performing any
@@ -1351,7 +1409,7 @@ public void call(DismissType dismissType, ReportType reportType) {
1351
1409
WritableMap params = Arguments .createMap ();
1352
1410
params .putString ("dismissType" , dismissType .toString ());
1353
1411
params .putString ("reportType" , reportType .toString ());
1354
- sendEvent (getReactApplicationContext (), "IBGpostInvocationHandler" , null );
1412
+ sendEvent (getReactApplicationContext (), "IBGpostInvocationHandler" , params );
1355
1413
}
1356
1414
});
1357
1415
} catch (java .lang .Exception exception ) {
@@ -1671,14 +1729,84 @@ public void setThresholdForReshowingSurveyAfterDismiss(int sessionsCount, int da
1671
1729
*/
1672
1730
@ ReactMethod
1673
1731
public void getAvailableSurveys (Callback availableSurveysCallback ) {
1674
- ArrayList <Survey > availableSurveys = new ArrayList <Survey >();
1675
1732
try {
1676
- availableSurveys = (ArrayList <Survey >) Surveys .getAvailableSurveys ();
1733
+ List <Survey > availableSurveys = Surveys .getAvailableSurveys ();
1734
+ JSONArray surveysArray = toJson (availableSurveys );
1735
+ WritableArray array = convertJsonToArray (surveysArray );
1736
+ availableSurveysCallback .invoke (array );
1677
1737
} catch (Exception e ) {
1678
1738
e .printStackTrace ();
1679
1739
}
1680
1740
1681
- availableSurveysCallback .invoke (availableSurveys );
1741
+ }
1742
+
1743
+
1744
+ private static WritableMap convertJsonToMap (JSONObject jsonObject ) throws JSONException {
1745
+ WritableMap map = new WritableNativeMap ();
1746
+
1747
+ Iterator <String > iterator = jsonObject .keys ();
1748
+ while (iterator .hasNext ()) {
1749
+ String key = iterator .next ();
1750
+ Object value = jsonObject .get (key );
1751
+ if (value instanceof JSONObject ) {
1752
+ map .putMap (key , convertJsonToMap ((JSONObject ) value ));
1753
+ } else if (value instanceof JSONArray ) {
1754
+ map .putArray (key , convertJsonToArray ((JSONArray ) value ));
1755
+ } else if (value instanceof Boolean ) {
1756
+ map .putBoolean (key , (Boolean ) value );
1757
+ } else if (value instanceof Integer ) {
1758
+ map .putInt (key , (Integer ) value );
1759
+ } else if (value instanceof Double ) {
1760
+ map .putDouble (key , (Double ) value );
1761
+ } else if (value instanceof String ) {
1762
+ map .putString (key , (String ) value );
1763
+ } else {
1764
+ map .putString (key , value .toString ());
1765
+ }
1766
+ }
1767
+ return map ;
1768
+ }
1769
+
1770
+ private static WritableArray convertJsonToArray (JSONArray jsonArray ) throws JSONException {
1771
+ WritableArray array = new WritableNativeArray ();
1772
+
1773
+ for (int i = 0 ; i < jsonArray .length (); i ++) {
1774
+ Object value = jsonArray .get (i );
1775
+ if (value instanceof JSONObject ) {
1776
+ array .pushMap (convertJsonToMap ((JSONObject ) value ));
1777
+ } else if (value instanceof JSONArray ) {
1778
+ array .pushArray (convertJsonToArray ((JSONArray ) value ));
1779
+ } else if (value instanceof Boolean ) {
1780
+ array .pushBoolean ((Boolean ) value );
1781
+ } else if (value instanceof Integer ) {
1782
+ array .pushInt ((Integer ) value );
1783
+ } else if (value instanceof Double ) {
1784
+ array .pushDouble ((Double ) value );
1785
+ } else if (value instanceof String ) {
1786
+ array .pushString ((String ) value );
1787
+ }
1788
+ }
1789
+ return array ;
1790
+ }
1791
+
1792
+ /**
1793
+ * Convenience method to convert from a list of Surveys to a JSON array
1794
+ *
1795
+ * @param list
1796
+ * List of Surveys to be converted to JSON array
1797
+ */
1798
+ public static JSONArray toJson (List <Survey > list ) {
1799
+ JSONArray jsonArray = new JSONArray ();
1800
+ try {
1801
+ for (Survey obj : list ) {
1802
+ JSONObject object = new JSONObject ();
1803
+ object .put ("title" , obj .getTitle ());
1804
+ jsonArray .put (object );
1805
+ }
1806
+ } catch (JSONException e ) {
1807
+ e .printStackTrace ();
1808
+ }
1809
+ return jsonArray ;
1682
1810
}
1683
1811
1684
1812
/**
0 commit comments