11package com .instabug .reactlibrary ;
22
3+ import android .annotation .SuppressLint ;
34import android .app .Application ;
45import android .net .Uri ;
56import android .os .Handler ;
67import android .os .Looper ;
78import android .util .Log ;
89
910import com .facebook .react .bridge .Arguments ;
11+ import com .facebook .react .bridge .Promise ;
1012import com .facebook .react .bridge .ReactApplicationContext ;
1113import com .facebook .react .bridge .ReactContextBaseJavaModule ;
1214import com .facebook .react .bridge .ReactMethod ;
5153import com .instabug .library .visualusersteps .State ;
5254
5355import com .instabug .reactlibrary .utils .ArrayUtil ;
56+ import com .instabug .reactlibrary .utils .ReportUtil ;
5457import com .instabug .reactlibrary .utils .InstabugUtil ;
5558import com .instabug .reactlibrary .utils .MapUtil ;
5659import com .instabug .survey .OnDismissCallback ;
@@ -212,6 +215,7 @@ public class RNInstabugReactnativeModule extends ReactContextBaseJavaModule {
212215 private Instabug mInstabug ;
213216 private InstabugInvocationEvent invocationEvent ;
214217 private InstabugCustomTextPlaceHolder placeHolders ;
218+ private Report currentReport ;
215219
216220 /**
217221 * Instantiates a new Rn instabug reactnative module.
@@ -259,6 +263,7 @@ public void run() {
259263 * @param invocationMode the invocation mode
260264 * @param invocationOptions the array of invocation options
261265 */
266+ @ SuppressLint ("WrongConstant" )
262267 @ ReactMethod
263268 public void invokeWithInvocationModeAndOptions (String invocationMode , ReadableArray invocationOptions ) {
264269
@@ -333,7 +338,7 @@ public void appendTags(ReadableArray tags) {
333338 @ ReactMethod
334339 public void setAutoScreenRecordingEnabled (boolean autoScreenRecordingEnabled ) {
335340 try {
336- Instabug .setAutoScreenRecordingEnabled (autoScreenRecordingEnabled );
341+ BugReporting .setAutoScreenRecordingEnabled (autoScreenRecordingEnabled );
337342 } catch (Exception e ) {
338343 e .printStackTrace ();
339344 }
@@ -401,10 +406,9 @@ public void setExtendedBugReportMode(String extendedBugReportMode) {
401406 public void setViewHierarchyEnabled (boolean enabled ) {
402407 try {
403408 if (enabled ) {
404- Instabug .setViewHierarchyState (Feature .State .ENABLED );
409+ BugReporting .setViewHierarchyState (Feature .State .ENABLED );
405410 } else {
406-
407- Instabug .setViewHierarchyState (Feature .State .DISABLED );
411+ BugReporting .setViewHierarchyState (Feature .State .DISABLED );
408412 }
409413 } catch (Exception e ) {
410414 e .printStackTrace ();
@@ -452,7 +456,8 @@ public void setFileAttachment(String fileUri, String fileNameWithExtension) {
452456 @ ReactMethod
453457 public void sendJSCrash (String exceptionObject ) {
454458 try {
455- sendJSCrashByReflection (exceptionObject , false );
459+ JSONObject jsonObject = new JSONObject (exceptionObject );
460+ sendJSCrashByReflection (jsonObject , false , null );
456461 } catch (Exception e ) {
457462 e .printStackTrace ();
458463 }
@@ -466,7 +471,8 @@ public void sendJSCrash(String exceptionObject) {
466471 @ ReactMethod
467472 public void sendHandledJSCrash (String exceptionObject ) {
468473 try {
469- sendJSCrashByReflection (exceptionObject , true );
474+ JSONObject jsonObject = new JSONObject (exceptionObject );
475+ sendJSCrashByReflection (jsonObject , true , null );
470476 } catch (Exception e ) {
471477 e .printStackTrace ();
472478 }
@@ -490,23 +496,20 @@ public void setCrashReportingEnabled(boolean isEnabled) {
490496 }
491497 }
492498
493- private void sendJSCrashByReflection (String exceptionObject , boolean isHandled ) {
499+ private void sendJSCrashByReflection (JSONObject exceptionObject , boolean isHandled , Report report ) {
494500 try {
495- JSONObject newJSONObject = new JSONObject (exceptionObject );
496- Method method = getMethod (Class .forName ("com.instabug.crash.CrashReporting" ), "reportException" , JSONObject .class , boolean .class );
501+ Method method = getMethod (Class .forName ("com.instabug.crash.CrashReporting" ), "reportException" , JSONObject .class , boolean .class , Report .class );
497502 if (method != null ) {
498- method .invoke (null , newJSONObject , isHandled );
503+ method .invoke (null , exceptionObject , isHandled , currentReport );
504+ currentReport = null ;
499505 }
500506 } catch (ClassNotFoundException e ) {
501507 e .printStackTrace ();
502- } catch (InvocationTargetException e ) {
503- e .printStackTrace ();
504508 } catch (IllegalAccessException e ) {
505509 e .printStackTrace ();
506- } catch (JSONException e ) {
510+ } catch (InvocationTargetException e ) {
507511 e .printStackTrace ();
508512 }
509-
510513 }
511514
512515 /**
@@ -1185,25 +1188,144 @@ public void onInvoke() {
11851188 */
11861189 @ ReactMethod
11871190 public void setPreSendingHandler (final Callback preSendingHandler ) {
1191+ Report .OnReportCreatedListener listener = new Report .OnReportCreatedListener () {
1192+ @ Override
1193+ public void onReportCreated (Report report ) {
1194+ WritableMap reportParam = Arguments .createMap ();
1195+ reportParam .putArray ("tagsArray" , convertArrayListToWritableArray (report .getTags ()));
1196+ reportParam .putArray ("consoleLogs" , convertArrayListToWritableArray (report .getConsoleLog ()));
1197+ reportParam .putString ("userData" , report .getUserData ());
1198+ reportParam .putMap ("userAttributes" , convertFromHashMapToWriteableMap (report .getUserAttributes ()));
1199+ reportParam .putMap ("fileAttachments" , convertFromHashMapToWriteableMap (report .getFileAttachments ()));
1200+ sendEvent (getReactApplicationContext (), "IBGpreSendingHandler" , reportParam );
1201+ currentReport = report ;
1202+ }
1203+ };
1204+
1205+ Method method = getMethod (Instabug .class , "onReportSubmitHandler_Private" , Report .OnReportCreatedListener .class );
1206+ if (method != null ) {
1207+ try {
1208+ method .invoke (null , listener );
1209+ } catch (IllegalAccessException e ) {
1210+ e .printStackTrace ();
1211+ } catch (InvocationTargetException e ) {
1212+ e .printStackTrace ();
1213+ }
1214+ }
1215+ }
1216+
1217+ @ ReactMethod
1218+ public void appendTagToReport (String tag ) {
1219+ if (currentReport != null ) {
1220+ currentReport .addTag (tag );
1221+ }
1222+ }
1223+
1224+ @ ReactMethod
1225+ public void appendConsoleLogToReport (String consoleLog ) {
1226+ if (currentReport != null ) {
1227+ currentReport .appendToConsoleLogs (consoleLog );
1228+ }
1229+ }
1230+
1231+ @ ReactMethod
1232+ public void setUserAttributeToReport (String key , String value ) {
1233+ if (currentReport != null ) {
1234+ currentReport .setUserAttribute (key , value );
1235+ }
1236+ }
1237+
1238+ @ ReactMethod
1239+ public void logDebugToReport (String log ) {
1240+ if (currentReport != null ) {
1241+ currentReport .logDebug (log );
1242+ }
1243+ }
1244+
1245+ @ ReactMethod
1246+ public void logVerboseToReport (String log ) {
1247+ if (currentReport != null ) {
1248+ currentReport .logVerbose (log );
1249+ }
1250+ }
1251+
1252+ @ ReactMethod
1253+ public void logWarnToReport (String log ) {
1254+ if (currentReport != null ) {
1255+ currentReport .logWarn (log );
1256+ }
1257+ }
1258+
1259+ @ ReactMethod
1260+ public void logErrorToReport (String log ) {
1261+ if (currentReport != null ) {
1262+ currentReport .logError (log );
1263+ }
1264+ }
1265+
1266+ @ ReactMethod
1267+ public void logInfoToReport (String log ) {
1268+ if (currentReport != null ) {
1269+ currentReport .logInfo (log );
1270+ }
1271+ }
1272+
1273+ @ ReactMethod
1274+ public void addFileAttachmentWithURLToReport (String urlString , String fileName ) {
1275+ if (currentReport != null ) {
1276+ Uri uri = Uri .parse (urlString );
1277+ currentReport .addFileAttachment (uri , fileName );
1278+ }
1279+ }
1280+
1281+ @ ReactMethod
1282+ public void addFileAttachmentWithDataToReport (String data , String fileName ) {
1283+ if (currentReport != null ) {
1284+ currentReport .addFileAttachment (data .getBytes (), fileName );
1285+ }
1286+ }
1287+
1288+ @ ReactMethod
1289+ public void submitReport () {
1290+ Method method = getMethod (Instabug .class , "setReport" , Report .class );
1291+ if (method != null ) {
1292+ try {
1293+ method .invoke (null , currentReport );
1294+ currentReport = null ;
1295+ } catch (IllegalAccessException e ) {
1296+ e .printStackTrace ();
1297+ } catch (InvocationTargetException e ) {
1298+ e .printStackTrace ();
1299+ }
1300+ }
1301+ }
1302+
1303+ @ ReactMethod
1304+ public void getReport (Promise promise ) {
11881305 try {
1306+ Method method = getMethod (Class .forName ("com.instabug.library.Instabug" ), "getReport" );
1307+ if (method != null ) {
1308+ Report report = (Report ) method .invoke (null );
1309+ WritableMap reportParam = Arguments .createMap ();
1310+ reportParam .putArray ("tagsArray" , convertArrayListToWritableArray (report .getTags ()));
1311+ reportParam .putArray ("consoleLogs" , ReportUtil .parseConsoleLogs (report .getConsoleLog ()));
1312+ reportParam .putString ("userData" , report .getUserData ());
1313+ reportParam .putMap ("userAttributes" , convertFromHashMapToWriteableMap (report .getUserAttributes ()));
1314+ reportParam .putMap ("fileAttachments" , convertFromHashMapToWriteableMap (report .getFileAttachments ()));
1315+ promise .resolve (reportParam );
1316+ currentReport = report ;
1317+ }
11891318
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 ();
1319+ } catch (ClassNotFoundException e ) {
1320+ promise .reject (e );
1321+ } catch (IllegalAccessException e ) {
1322+ promise .reject (e );
1323+ } catch (InvocationTargetException e ) {
1324+ promise .reject (e );
12041325 }
12051326 }
12061327
1328+
12071329 private WritableMap convertFromHashMapToWriteableMap (HashMap hashMap ) {
12081330 WritableMap writableMap = new WritableNativeMap ();
12091331 for (int i = 0 ; i < hashMap .size (); i ++) {
@@ -1427,14 +1549,11 @@ public void setReproStepsMode(String reproStepsMode) {
14271549 case ENABLED_WITH_NO_SCREENSHOTS :
14281550 Instabug .setReproStepsState (State .ENABLED_WITH_NO_SCREENSHOTS );
14291551 break ;
1430- case ENABLED :
1431- Instabug .setReproStepsState (State .ENABLED );
1432- break ;
14331552 case DISABLED :
14341553 Instabug .setReproStepsState (State .DISABLED );
14351554 break ;
14361555 default :
1437- Instabug .setReproStepsState (State .ENABLED );
1556+ Instabug .setReproStepsState (State .ENABLED_WITH_NO_SCREENSHOTS );
14381557 }
14391558
14401559 } catch (Exception e ) {
@@ -1602,6 +1721,7 @@ public void show() {
16021721 Instabug .show ();
16031722 }
16041723
1724+ @ SuppressLint ("WrongConstant" )
16051725 @ ReactMethod
16061726 public void setReportTypes (ReadableArray types ) {
16071727 Object [] objectArray = ArrayUtil .toArray (types );
@@ -1861,6 +1981,7 @@ public void setShouldShowSurveysWelcomeScreen(boolean shouldShow) {
18611981 * @param isEmailRequired set true to make email field required
18621982 * @param actionTypes Bitwise-or of actions
18631983 */
1984+ @ SuppressLint ("WrongConstant" )
18641985 @ ReactMethod
18651986 public void setEmailFieldRequiredForFeatureRequests (boolean isEmailRequired , ReadableArray actionTypes ) {
18661987 try {
@@ -1907,6 +2028,7 @@ public void networkLog(String jsonObject) throws JSONException {
19072028 networkLog .setResponseCode (newJSONObject .getInt ("responseCode" ));
19082029 networkLog .setRequestHeaders (newJSONObject .getString ("requestHeaders" ));
19092030 networkLog .setResponseHeaders (newJSONObject .getString ("responseHeaders" ));
2031+ networkLog .setTotalDuration (newJSONObject .getLong ("duration" ));
19102032 networkLog .insert ();
19112033 }
19122034
0 commit comments