2424import com .instabug .bug .PromptOption ;
2525import com .instabug .bug .invocation .InvocationMode ;
2626import com .instabug .bug .invocation .InvocationOption ;
27- import com .instabug .chat .InstabugChat ;
2827import com .instabug .crash .CrashReporting ;
2928import com .instabug .featuresrequest .FeatureRequests ;
3029import com .instabug .featuresrequest .ActionType ;
3736import com .instabug .library .invocation .InstabugInvocationMode ;
3837import com .instabug .library .InstabugColorTheme ;
3938import com .instabug .library .invocation .OnInvokeCallback ;
40- import com .instabug .library .invocation .util .InstabugVideoRecordingButtonCorner ;
4139import com .instabug .library .invocation .util .InstabugVideoRecordingButtonPosition ;
4240import com .instabug .library .logging .InstabugLog ;
4341import com .instabug .library .bugreporting .model .ReportCategory ;
4442import com .instabug .library .ui .onboarding .WelcomeMessage ;
4543import com .instabug .library .InstabugCustomTextPlaceHolder ;
4644import com .instabug .library .model .Report ;
4745import com .instabug .library .user .UserEventParam ;
48- import com .instabug .library .OnSdkDismissedCallback ;
49- import com .instabug .library .bugreporting .model .Bug ;
5046import com .instabug .library .visualusersteps .State ;
5147
5248import com .instabug .reactlibrary .utils .ArrayUtil ;
5652import com .instabug .survey .Survey ;
5753import com .instabug .survey .Surveys ;
5854
55+ import org .json .JSONArray ;
5956import org .json .JSONException ;
6057import org .json .JSONObject ;
58+ import org .json .JSONTokener ;
6159
62- import java .lang .reflect .Array ;
6360import java .lang .reflect .InvocationTargetException ;
6461import java .lang .reflect .Method ;
6562import java .util .ArrayList ;
6663import java .util .Arrays ;
6764import java .util .HashMap ;
65+ import java .util .Iterator ;
6866import java .util .List ;
6967import java .util .Locale ;
7068import java .util .Map ;
@@ -227,14 +225,42 @@ public void run() {
227225 }
228226 }
229227
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+
230256 /**
231257 * invoke sdk manually with desire invocation mode
232258 *
233259 * @param invocationMode the invocation mode
234260 * @param invocationOptions the array of invocation options
235261 */
236262 @ ReactMethod
237- public void invokeWithInvocationMode (String invocationMode , ReadableArray invocationOptions ) {
263+ public void invokeWithInvocationModeAndOptions (String invocationMode , ReadableArray invocationOptions ) {
238264 InvocationMode mode ;
239265
240266
@@ -250,8 +276,6 @@ public void invokeWithInvocationMode(String invocationMode, ReadableArray invoca
250276 mode = InvocationMode .PROMPT_OPTION ;
251277 }
252278
253- Log .d ("where is" , invocationMode + " " + mode );
254-
255279 Object [] objectArray = ArrayUtil .toArray (invocationOptions );
256280 String [] stringArray = Arrays .copyOf (objectArray , objectArray .length , String [].class );
257281
@@ -275,8 +299,6 @@ public void invokeWithInvocationMode(String invocationMode, ReadableArray invoca
275299 break ;
276300 }
277301 }
278- Log .d ("where is" , Arrays .toString (stringArray ) + " " + Arrays .toString (arrayOfParsedOptions ));
279-
280302 try {
281303 BugReporting .invoke (mode , arrayOfParsedOptions );
282304 } catch (Exception e ) {
@@ -1309,31 +1331,67 @@ public void onInvoke() {
13091331 @ ReactMethod
13101332 public void setPreSendingHandler (final Callback preSendingHandler ) {
13111333 try {
1312- Runnable preSendingRunnable = new Runnable () {
1313- @ Override
1314- public void run () {
1315- sendEvent (getReactApplicationContext (), "IBGpreSendingHandler" , null );
1316- }
1317- };
1318-
13191334
13201335 Instabug .onReportSubmitHandler (new Report .OnReportCreatedListener () {
13211336 @ Override
13221337 public void onReportCreated (Report report ) {
13231338 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 () ));
13261341 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 );
13301345 }
13311346 });
13321347 } catch (java .lang .Exception exception ) {
13331348 exception .printStackTrace ();
13341349 }
13351350 }
13361351
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+
13371395 /**
13381396 * Sets a block of code to be executed right after the SDK's UI is dismissed.
13391397 * 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) {
13511409 WritableMap params = Arguments .createMap ();
13521410 params .putString ("dismissType" , dismissType .toString ());
13531411 params .putString ("reportType" , reportType .toString ());
1354- sendEvent (getReactApplicationContext (), "IBGpostInvocationHandler" , null );
1412+ sendEvent (getReactApplicationContext (), "IBGpostInvocationHandler" , params );
13551413 }
13561414 });
13571415 } catch (java .lang .Exception exception ) {
@@ -1671,14 +1729,84 @@ public void setThresholdForReshowingSurveyAfterDismiss(int sessionsCount, int da
16711729 */
16721730 @ ReactMethod
16731731 public void getAvailableSurveys (Callback availableSurveysCallback ) {
1674- ArrayList <Survey > availableSurveys = new ArrayList <Survey >();
16751732 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 );
16771737 } catch (Exception e ) {
16781738 e .printStackTrace ();
16791739 }
16801740
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 ;
16821810 }
16831811
16841812 /**
0 commit comments