77import android .util .Log ;
88
99import com .facebook .react .bridge .Arguments ;
10+ import com .facebook .react .bridge .Promise ;
1011import com .facebook .react .bridge .ReactApplicationContext ;
1112import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1213import com .facebook .react .bridge .ReactMethod ;
5152import com .instabug .library .visualusersteps .State ;
5253
5354import com .instabug .reactlibrary .utils .ArrayUtil ;
55+ import com .instabug .reactlibrary .utils .ReportUtil ;
5456import com .instabug .reactlibrary .utils .InstabugUtil ;
5557import com .instabug .reactlibrary .utils .MapUtil ;
5658import com .instabug .survey .OnDismissCallback ;
@@ -212,6 +214,7 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
212214 private Instabug mInstabug ;
213215 private InstabugInvocationEvent invocationEvent ;
214216 private InstabugCustomTextPlaceHolder placeHolders ;
217+ private Report currentReport ;
215218
216219 /**
217220 * Instantiates a new Rn instabug reactnative module.
@@ -452,7 +455,8 @@ public void setFileAttachment(String fileUri, String fileNameWithExtension) {
452455 @ ReactMethod
453456 public void sendJSCrash (String exceptionObject ) {
454457 try {
455- sendJSCrashByReflection (exceptionObject , false );
458+ JSONObject jsonObject = new JSONObject (exceptionObject );
459+ sendJSCrashByReflection (jsonObject , false , null );
456460 } catch (Exception e ) {
457461 e .printStackTrace ();
458462 }
@@ -466,7 +470,8 @@ public void sendJSCrash(String exceptionObject) {
466470 @ ReactMethod
467471 public void sendHandledJSCrash (String exceptionObject ) {
468472 try {
469- sendJSCrashByReflection (exceptionObject , true );
473+ JSONObject jsonObject = new JSONObject (exceptionObject );
474+ sendJSCrashByReflection (jsonObject , true , null );
470475 } catch (Exception e ) {
471476 e .printStackTrace ();
472477 }
@@ -490,23 +495,20 @@ public void setCrashReportingEnabled(boolean isEnabled) {
490495 }
491496 }
492497
493- private void sendJSCrashByReflection (String exceptionObject , boolean isHandled ) {
498+ private void sendJSCrashByReflection (JSONObject exceptionObject , boolean isHandled , Report report ) {
494499 try {
495- JSONObject newJSONObject = new JSONObject (exceptionObject );
496- Method method = getMethod (Class .forName ("com.instabug.crash.CrashReporting" ), "reportException" , JSONObject .class , boolean .class );
500+ Method method = getMethod (Class .forName ("com.instabug.crash.CrashReporting" ), "reportException" , JSONObject .class , boolean .class , Report .class );
497501 if (method != null ) {
498- method .invoke (null , newJSONObject , isHandled );
502+ method .invoke (null , exceptionObject , isHandled , currentReport );
503+ currentReport = null ;
499504 }
500505 } catch (ClassNotFoundException e ) {
501506 e .printStackTrace ();
502- } catch (InvocationTargetException e ) {
503- e .printStackTrace ();
504507 } catch (IllegalAccessException e ) {
505508 e .printStackTrace ();
506- } catch (JSONException e ) {
509+ } catch (InvocationTargetException e ) {
507510 e .printStackTrace ();
508511 }
509-
510512 }
511513
512514 /**
@@ -1185,25 +1187,144 @@ public void onInvoke() {
11851187 */
11861188 @ ReactMethod
11871189 public void setPreSendingHandler (final Callback preSendingHandler ) {
1190+ Report .OnReportCreatedListener listener = new Report .OnReportCreatedListener () {
1191+ @ Override
1192+ public void onReportCreated (Report report ) {
1193+ WritableMap reportParam = Arguments .createMap ();
1194+ reportParam .putArray ("tagsArray" , convertArrayListToWritableArray (report .getTags ()));
1195+ reportParam .putArray ("consoleLogs" , convertArrayListToWritableArray (report .getConsoleLog ()));
1196+ reportParam .putString ("userData" , report .getUserData ());
1197+ reportParam .putMap ("userAttributes" , convertFromHashMapToWriteableMap (report .getUserAttributes ()));
1198+ reportParam .putMap ("fileAttachments" , convertFromHashMapToWriteableMap (report .getFileAttachments ()));
1199+ sendEvent (getReactApplicationContext (), "IBGpreSendingHandler" , reportParam );
1200+ currentReport = report ;
1201+ }
1202+ };
1203+
1204+ Method method = getMethod (Instabug .class , "onReportSubmitHandler_Private" , Report .OnReportCreatedListener .class );
1205+ if (method != null ) {
1206+ try {
1207+ method .invoke (null , listener );
1208+ } catch (IllegalAccessException e ) {
1209+ e .printStackTrace ();
1210+ } catch (InvocationTargetException e ) {
1211+ e .printStackTrace ();
1212+ }
1213+ }
1214+ }
1215+
1216+ @ ReactMethod
1217+ public void appendTagToReport (String tag ) {
1218+ if (currentReport != null ) {
1219+ currentReport .addTag (tag );
1220+ }
1221+ }
1222+
1223+ @ ReactMethod
1224+ public void appendConsoleLogToReport (String consoleLog ) {
1225+ if (currentReport != null ) {
1226+ currentReport .appendToConsoleLogs (consoleLog );
1227+ }
1228+ }
1229+
1230+ @ ReactMethod
1231+ public void setUserAttributeToReport (String key , String value ) {
1232+ if (currentReport != null ) {
1233+ currentReport .setUserAttribute (key , value );
1234+ }
1235+ }
1236+
1237+ @ ReactMethod
1238+ public void logDebugToReport (String log ) {
1239+ if (currentReport != null ) {
1240+ currentReport .logDebug (log );
1241+ }
1242+ }
1243+
1244+ @ ReactMethod
1245+ public void logVerboseToReport (String log ) {
1246+ if (currentReport != null ) {
1247+ currentReport .logVerbose (log );
1248+ }
1249+ }
1250+
1251+ @ ReactMethod
1252+ public void logWarnToReport (String log ) {
1253+ if (currentReport != null ) {
1254+ currentReport .logWarn (log );
1255+ }
1256+ }
1257+
1258+ @ ReactMethod
1259+ public void logErrorToReport (String log ) {
1260+ if (currentReport != null ) {
1261+ currentReport .logError (log );
1262+ }
1263+ }
1264+
1265+ @ ReactMethod
1266+ public void logInfoToReport (String log ) {
1267+ if (currentReport != null ) {
1268+ currentReport .logInfo (log );
1269+ }
1270+ }
1271+
1272+ @ ReactMethod
1273+ public void addFileAttachmentWithURLToReport (String urlString , String fileName ) {
1274+ if (currentReport != null ) {
1275+ Uri uri = Uri .parse (urlString );
1276+ currentReport .addFileAttachment (uri , fileName );
1277+ }
1278+ }
1279+
1280+ @ ReactMethod
1281+ public void addFileAttachmentWithDataToReport (String data , String fileName ) {
1282+ if (currentReport != null ) {
1283+ currentReport .addFileAttachment (data .getBytes (), fileName );
1284+ }
1285+ }
1286+
1287+ @ ReactMethod
1288+ public void submitReport () {
1289+ Method method = getMethod (Instabug .class , "setReport" , Report .class );
1290+ if (method != null ) {
1291+ try {
1292+ method .invoke (null , currentReport );
1293+ currentReport = null ;
1294+ } catch (IllegalAccessException e ) {
1295+ e .printStackTrace ();
1296+ } catch (InvocationTargetException e ) {
1297+ e .printStackTrace ();
1298+ }
1299+ }
1300+ }
1301+
1302+ @ ReactMethod
1303+ public void getReport (Promise promise ) {
11881304 try {
1305+ Method method = getMethod (Class .forName ("com.instabug.library.Instabug" ), "getReport" );
1306+ if (method != null ) {
1307+ Report report = (Report ) method .invoke (null );
1308+ WritableMap reportParam = Arguments .createMap ();
1309+ reportParam .putArray ("tagsArray" , convertArrayListToWritableArray (report .getTags ()));
1310+ reportParam .putArray ("consoleLogs" , ReportUtil .parseConsoleLogs (report .getConsoleLog ()));
1311+ reportParam .putString ("userData" , report .getUserData ());
1312+ reportParam .putMap ("userAttributes" , convertFromHashMapToWriteableMap (report .getUserAttributes ()));
1313+ reportParam .putMap ("fileAttachments" , convertFromHashMapToWriteableMap (report .getFileAttachments ()));
1314+ promise .resolve (reportParam );
1315+ currentReport = report ;
1316+ }
11891317
1190- Instabug .onReportSubmitHandler (new Report .OnReportCreatedListener () {
1191- @ Override
1192- public void onReportCreated (Report report ) {
1193- WritableMap reportParam = Arguments .createMap ();
1194- reportParam .putArray ("tagsArray" , convertArrayListToWritableArray (report .getTags ()));
1195- reportParam .putArray ("consoleLogs" , convertArrayListToWritableArray (report .getConsoleLog ()));
1196- reportParam .putString ("userData" , report .getUserData ());
1197- reportParam .putMap ("userAttributes" , convertFromHashMapToWriteableMap (report .getUserAttributes ()));
1198- reportParam .putMap ("fileAttachments" , convertFromHashMapToWriteableMap (report .getFileAttachments ()));
1199- sendEvent (getReactApplicationContext (), "IBGpreSendingHandler" , reportParam );
1200- }
1201- });
1202- } catch (java .lang .Exception exception ) {
1203- exception .printStackTrace ();
1318+ } catch (ClassNotFoundException e ) {
1319+ promise .reject (e );
1320+ } catch (IllegalAccessException e ) {
1321+ promise .reject (e );
1322+ } catch (InvocationTargetException e ) {
1323+ promise .reject (e );
12041324 }
12051325 }
12061326
1327+
12071328 private WritableMap convertFromHashMapToWriteableMap (HashMap hashMap ) {
12081329 WritableMap writableMap = new WritableNativeMap ();
12091330 for (int i = 0 ; i < hashMap .size (); i ++) {
0 commit comments