7
7
import android .os .Looper ;
8
8
import android .util .Log ;
9
9
10
+ import com .instabug .apm .APM ;
11
+ import com .instabug .apm .model .ExecutionTrace ;
12
+ import com .instabug .apm .networking .APMNetworkLogger ;
10
13
import com .instabug .bug .BugReporting ;
11
14
import com .instabug .bug .invocation .Option ;
12
15
import com .instabug .chat .Chats ;
@@ -64,6 +67,7 @@ public class InstabugFlutterPlugin implements MethodCallHandler {
64
67
final public static String INVOCATION_EVENT_SHAKE = "InvocationEvent.shake" ;
65
68
66
69
private InstabugCustomTextPlaceHolder placeHolder = new InstabugCustomTextPlaceHolder ();
70
+ HashMap <String , ExecutionTrace > traces = new HashMap <String , ExecutionTrace >();
67
71
68
72
static MethodChannel channel ;
69
73
@@ -106,14 +110,14 @@ public void onMethodCall(MethodCall call, Result result) {
106
110
}
107
111
}
108
112
109
- private void setCrossPlatform () {
113
+ private void setCurrentPlatform () {
110
114
try {
111
- Method method = getMethod (Class .forName ("com.instabug.library.Instabug" ), "setCrossPlatform " , int .class );
115
+ Method method = getMethod (Class .forName ("com.instabug.library.Instabug" ), "setCurrentPlatform " , int .class );
112
116
if (method != null ) {
113
- Log .i ("IB-CP-Bridge" , "invoking setCrossPlatform with platform: " + Platform .FLUTTER );
117
+ Log .i ("IB-CP-Bridge" , "invoking setCurrentPlatform with platform: " + Platform .FLUTTER );
114
118
method .invoke (null , Platform .FLUTTER );
115
119
} else {
116
- Log .e ("IB-CP-Bridge" , "setCrossPlatform was not found by reflection" );
120
+ Log .e ("IB-CP-Bridge" , "setCurrentPlatform was not found by reflection" );
117
121
}
118
122
} catch (Exception e ) {
119
123
e .printStackTrace ();
@@ -129,7 +133,7 @@ private void setCrossPlatform() {
129
133
* @param invocationEvents invocationEvents The events that invoke the SDK's UI.
130
134
*/
131
135
public void start (Application application , String token , ArrayList <String > invocationEvents ) {
132
- setCrossPlatform ();
136
+ setCurrentPlatform ();
133
137
InstabugInvocationEvent [] invocationEventsArray = new InstabugInvocationEvent [invocationEvents .size ()];
134
138
for (int i = 0 ; i < invocationEvents .size (); i ++) {
135
139
String key = invocationEvents .get (i );
@@ -138,8 +142,6 @@ public void start(Application application, String token, ArrayList<String> invoc
138
142
new Instabug .Builder (application , token ).setInvocationEvents (invocationEventsArray ).build ();
139
143
enableScreenShotByMediaProjection ();
140
144
141
- // Temporarily disabling APM
142
- APM .setEnabled (false );
143
145
}
144
146
145
147
/**
@@ -422,6 +424,15 @@ public void setSessionProfilerEnabled(boolean sessionProfilerEnabled) {
422
424
}
423
425
}
424
426
427
+ /**
428
+ * Enable/disable SDK logs
429
+ *
430
+ * @param debugEnabled desired state of debug mode
431
+ */
432
+ public void setDebugEnabled (boolean debugEnabled ) {
433
+ Instabug .setDebugEnabled (debugEnabled );
434
+ }
435
+
425
436
/**
426
437
* Set the primary color that the SDK will use to tint certain UI elements in
427
438
* the SDK
@@ -897,7 +908,7 @@ public void networkLog(HashMap<String, Object> jsonObject) throws JSONException
897
908
(new JSONObject ((HashMap <String , String >) jsonObject .get ("requestHeaders" ))).toString (4 ));
898
909
networkLog .setResponseHeaders (
899
910
(new JSONObject ((HashMap <String , String >) jsonObject .get ("responseHeaders" ))).toString (4 ));
900
- networkLog .setTotalDuration (((Number ) jsonObject .get ("duration" )).longValue ());
911
+ networkLog .setTotalDuration (((Number ) jsonObject .get ("duration" )).longValue () / 1000 );
901
912
networkLog .insert ();
902
913
}
903
914
@@ -938,6 +949,211 @@ public void run() {
938
949
});
939
950
}
940
951
952
+ /**
953
+ * Enables and disables everything related to APM feature.
954
+ *
955
+ * @param {boolean} isEnabled
956
+ */
957
+ public void setAPMEnabled (final boolean isEnabled ) {
958
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
959
+ @ Override
960
+ public void run () {
961
+ try {
962
+ APM .setEnabled (isEnabled );
963
+ } catch (Exception e ) {
964
+ e .printStackTrace ();
965
+ }
966
+ }
967
+ });
968
+ }
969
+
970
+ /**
971
+ * Sets the printed logs priority. Filter to one of the following levels.
972
+ *
973
+ * @param {String} logLevel.
974
+ */
975
+ public void setAPMLogLevel (final String logLevel ) {
976
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
977
+ @ Override
978
+ public void run () {
979
+ try {
980
+ if (ArgsRegistry .getDeserializedValue (logLevel , Integer .class ) == null ) {
981
+ return ;
982
+ }
983
+ APM .setLogLevel ((int ) ArgsRegistry .getRawValue (logLevel ));
984
+ } catch (Exception e ) {
985
+ e .printStackTrace ();
986
+ }
987
+ }
988
+ });
989
+ }
990
+
991
+ /**
992
+ * Enables or disables cold app launch tracking.
993
+ * @param isEnabled boolean indicating enabled or disabled.
994
+ */
995
+ public void setColdAppLaunchEnabled (final boolean isEnabled ) {
996
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
997
+ @ Override
998
+ public void run () {
999
+ try {
1000
+ APM .setAppLaunchEnabled (isEnabled );
1001
+ } catch (Exception e ) {
1002
+ e .printStackTrace ();
1003
+ }
1004
+ }
1005
+ });
1006
+ }
1007
+ /**
1008
+ * Starts an execution trace
1009
+ * @param name string name of the trace.
1010
+ */
1011
+ public void startExecutionTrace (final String name , final String id ) {
1012
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1013
+ @ Override
1014
+ public void run () {
1015
+ try {
1016
+ String result = null ;
1017
+ ExecutionTrace trace = APM .startExecutionTrace (name );
1018
+ if (trace != null ) {
1019
+ result = id ;
1020
+ traces .put (id , trace );
1021
+ }
1022
+ channel .invokeMethod ("startExecutionTraceCallBack" , result );
1023
+ } catch (Exception e ) {
1024
+ e .printStackTrace ();
1025
+ }
1026
+ }
1027
+ });
1028
+ }
1029
+
1030
+ /**
1031
+ * Sets an execution trace attribute
1032
+ * @param id string id of the trace.
1033
+ * @param key string key of the attribute.
1034
+ * @param value string value of the attribute.
1035
+ */
1036
+ public void setExecutionTraceAttribute (final String id , final String key , final String value ) {
1037
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1038
+ @ Override
1039
+ public void run () {
1040
+ try {
1041
+ traces .get (id ).setAttribute (key , value );
1042
+ } catch (Exception e ) {
1043
+ e .printStackTrace ();
1044
+ }
1045
+ }
1046
+ });
1047
+ }
1048
+
1049
+ /**
1050
+ * Ends an execution trace
1051
+ * @param id string id of the trace.
1052
+ */
1053
+ public void endExecutionTrace (final String id ) {
1054
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1055
+ @ Override
1056
+ public void run () {
1057
+ try {
1058
+ traces .get (id ).end ();
1059
+ } catch (Exception e ) {
1060
+ e .printStackTrace ();
1061
+ }
1062
+ }
1063
+ });
1064
+ }
1065
+
1066
+ /**
1067
+ * Enables or disables auto UI tracing
1068
+ * @param isEnabled boolean indicating enabled or disabled.
1069
+ */
1070
+ public void setAutoUITraceEnabled (final boolean isEnabled ) {
1071
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1072
+ @ Override
1073
+ public void run () {
1074
+ try {
1075
+ APM .setAutoUITraceEnabled (isEnabled );
1076
+ } catch (Exception e ) {
1077
+ e .printStackTrace ();
1078
+ }
1079
+ }
1080
+ });
1081
+ }
1082
+
1083
+ /**
1084
+ * Starts a UI trace
1085
+ * @param name string name of the UI trace.
1086
+ */
1087
+ public void startUITrace (final String name ) {
1088
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1089
+ @ Override
1090
+ public void run () {
1091
+ try {
1092
+ APM .startUITrace (name );
1093
+ } catch (Exception e ) {
1094
+ e .printStackTrace ();
1095
+ }
1096
+ }
1097
+ });
1098
+ }
1099
+
1100
+ /**
1101
+ * Ends the current running UI trace
1102
+ */
1103
+ public void endUITrace () {
1104
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1105
+ @ Override
1106
+ public void run () {
1107
+ try {
1108
+ APM .endUITrace ();
1109
+ } catch (Exception e ) {
1110
+ e .printStackTrace ();
1111
+ }
1112
+ }
1113
+ });
1114
+ }
1115
+
1116
+ public void apmNetworkLogByReflection (HashMap <String , Object > jsonObject ) throws JSONException {
1117
+ APMNetworkLogger apmNetworkLogger = new APMNetworkLogger ();
1118
+ final String requestUrl = (String ) jsonObject .get ("url" );
1119
+ final String requestBody = (String ) jsonObject .get ("requestBody" );
1120
+ final String responseBody = (String ) jsonObject .get ("responseBody" );
1121
+ final String requestMethod = (String ) jsonObject .get ("method" );
1122
+ //--------------------------------------------
1123
+ final String requestContentType = (String ) jsonObject .get ("contentType" );
1124
+ final String responseContentType = (String ) jsonObject .get ("responseContentType" );
1125
+ //--------------------------------------------
1126
+ final String errorDomain = (String ) jsonObject .get ("errorDomain" );
1127
+ final Integer statusCode = (Integer ) jsonObject .get ("responseCode" );
1128
+ final long requestDuration = ((Number ) jsonObject .get ("duration" )).longValue () / 1000 ;
1129
+ final long requestStartTime = ((Number ) jsonObject .get ("startTime" )).longValue () * 1000 ;
1130
+ final String requestHeaders = (new JSONObject ((HashMap <String , String >) jsonObject .get ("requestHeaders" ))).toString (4 );
1131
+ final String responseHeaders = (new JSONObject ((HashMap <String , String >) jsonObject .get ("responseHeaders" ))).toString (4 );
1132
+ final String errorMessage ;
1133
+
1134
+ if (errorDomain .equals ("" )) {
1135
+ errorMessage = null ;
1136
+ } else {
1137
+ errorMessage = errorDomain ;
1138
+ }
1139
+
1140
+ try {
1141
+ Method method = getMethod (Class .forName ("com.instabug.apm.networking.APMNetworkLogger" ), "log" , long .class , long .class , String .class , String .class , String .class , String .class , String .class , String .class , String .class , int .class , String .class , String .class );
1142
+ if (method != null ) {
1143
+ method .invoke (apmNetworkLogger , requestStartTime , requestDuration , requestHeaders , requestBody , requestMethod , requestUrl , requestContentType , responseHeaders , responseBody , statusCode , responseContentType , errorMessage );
1144
+ } else {
1145
+ Log .e ("IB-CP-Bridge" , "apmNetworkLogByReflection was not found by reflection" );
1146
+ }
1147
+ } catch (ClassNotFoundException e ) {
1148
+ e .printStackTrace ();
1149
+ } catch (IllegalAccessException e ) {
1150
+ e .printStackTrace ();
1151
+ } catch (InvocationTargetException e ) {
1152
+ e .printStackTrace ();
1153
+ }
1154
+
1155
+ }
1156
+
941
1157
/*
942
1158
*
943
1159
* Reports that the screen has been
0 commit comments