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 );
@@ -897,7 +901,7 @@ public void networkLog(HashMap<String, Object> jsonObject) throws JSONException
897
901
(new JSONObject ((HashMap <String , String >) jsonObject .get ("requestHeaders" ))).toString (4 ));
898
902
networkLog .setResponseHeaders (
899
903
(new JSONObject ((HashMap <String , String >) jsonObject .get ("responseHeaders" ))).toString (4 ));
900
- networkLog .setTotalDuration (((Number ) jsonObject .get ("duration" )).longValue ());
904
+ networkLog .setTotalDuration (((Number ) jsonObject .get ("duration" )).longValue () / 1000 );
901
905
networkLog .insert ();
902
906
}
903
907
@@ -938,6 +942,211 @@ public void run() {
938
942
});
939
943
}
940
944
945
+ /**
946
+ * Enables and disables everything related to APM feature.
947
+ *
948
+ * @param {boolean} isEnabled
949
+ */
950
+ public void setAPMEnabled (final boolean isEnabled ) {
951
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
952
+ @ Override
953
+ public void run () {
954
+ try {
955
+ APM .setEnabled (isEnabled );
956
+ } catch (Exception e ) {
957
+ e .printStackTrace ();
958
+ }
959
+ }
960
+ });
961
+ }
962
+
963
+ /**
964
+ * Sets the printed logs priority. Filter to one of the following levels.
965
+ *
966
+ * @param {String} logLevel.
967
+ */
968
+ public void setAPMLogLevel (final String logLevel ) {
969
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
970
+ @ Override
971
+ public void run () {
972
+ try {
973
+ if (ArgsRegistry .getDeserializedValue (logLevel , Integer .class ) == null ) {
974
+ return ;
975
+ }
976
+ APM .setLogLevel ((int ) ArgsRegistry .getRawValue (logLevel ));
977
+ } catch (Exception e ) {
978
+ e .printStackTrace ();
979
+ }
980
+ }
981
+ });
982
+ }
983
+
984
+ /**
985
+ * Enables or disables cold app launch tracking.
986
+ * @param isEnabled boolean indicating enabled or disabled.
987
+ */
988
+ public void setColdAppLaunchEnabled (final boolean isEnabled ) {
989
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
990
+ @ Override
991
+ public void run () {
992
+ try {
993
+ APM .setAppLaunchEnabled (isEnabled );
994
+ } catch (Exception e ) {
995
+ e .printStackTrace ();
996
+ }
997
+ }
998
+ });
999
+ }
1000
+ /**
1001
+ * Starts an execution trace
1002
+ * @param name string name of the trace.
1003
+ */
1004
+ public void startExecutionTrace (final String name , final String id ) {
1005
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1006
+ @ Override
1007
+ public void run () {
1008
+ try {
1009
+ String result = null ;
1010
+ ExecutionTrace trace = APM .startExecutionTrace (name );
1011
+ if (trace != null ) {
1012
+ result = id ;
1013
+ traces .put (id , trace );
1014
+ }
1015
+ channel .invokeMethod ("startExecutionTraceCallBack" , result );
1016
+ } catch (Exception e ) {
1017
+ e .printStackTrace ();
1018
+ }
1019
+ }
1020
+ });
1021
+ }
1022
+
1023
+ /**
1024
+ * Sets an execution trace attribute
1025
+ * @param id string id of the trace.
1026
+ * @param key string key of the attribute.
1027
+ * @param value string value of the attribute.
1028
+ */
1029
+ public void setExecutionTraceAttribute (final String id , final String key , final String value ) {
1030
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1031
+ @ Override
1032
+ public void run () {
1033
+ try {
1034
+ traces .get (id ).setAttribute (key , value );
1035
+ } catch (Exception e ) {
1036
+ e .printStackTrace ();
1037
+ }
1038
+ }
1039
+ });
1040
+ }
1041
+
1042
+ /**
1043
+ * Ends an execution trace
1044
+ * @param id string id of the trace.
1045
+ */
1046
+ public void endExecutionTrace (final String id ) {
1047
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1048
+ @ Override
1049
+ public void run () {
1050
+ try {
1051
+ traces .get (id ).end ();
1052
+ } catch (Exception e ) {
1053
+ e .printStackTrace ();
1054
+ }
1055
+ }
1056
+ });
1057
+ }
1058
+
1059
+ /**
1060
+ * Enables or disables auto UI tracing
1061
+ * @param isEnabled boolean indicating enabled or disabled.
1062
+ */
1063
+ public void setAutoUITraceEnabled (final boolean isEnabled ) {
1064
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1065
+ @ Override
1066
+ public void run () {
1067
+ try {
1068
+ APM .setAutoUITraceEnabled (isEnabled );
1069
+ } catch (Exception e ) {
1070
+ e .printStackTrace ();
1071
+ }
1072
+ }
1073
+ });
1074
+ }
1075
+
1076
+ /**
1077
+ * Starts a UI trace
1078
+ * @param name string name of the UI trace.
1079
+ */
1080
+ public void startUITrace (final String name ) {
1081
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1082
+ @ Override
1083
+ public void run () {
1084
+ try {
1085
+ APM .startUITrace (name );
1086
+ } catch (Exception e ) {
1087
+ e .printStackTrace ();
1088
+ }
1089
+ }
1090
+ });
1091
+ }
1092
+
1093
+ /**
1094
+ * Ends the current running UI trace
1095
+ */
1096
+ public void endUITrace () {
1097
+ new Handler (Looper .getMainLooper ()).post (new Runnable () {
1098
+ @ Override
1099
+ public void run () {
1100
+ try {
1101
+ APM .endUITrace ();
1102
+ } catch (Exception e ) {
1103
+ e .printStackTrace ();
1104
+ }
1105
+ }
1106
+ });
1107
+ }
1108
+
1109
+ public void apmNetworkLogByReflection (HashMap <String , Object > jsonObject ) throws JSONException {
1110
+ APMNetworkLogger apmNetworkLogger = new APMNetworkLogger ();
1111
+ final String requestUrl = (String ) jsonObject .get ("url" );
1112
+ final String requestBody = (String ) jsonObject .get ("requestBody" );
1113
+ final String responseBody = (String ) jsonObject .get ("responseBody" );
1114
+ final String requestMethod = (String ) jsonObject .get ("method" );
1115
+ //--------------------------------------------
1116
+ final String requestContentType = (String ) jsonObject .get ("contentType" );
1117
+ final String responseContentType = (String ) jsonObject .get ("responseContentType" );
1118
+ //--------------------------------------------
1119
+ final String errorDomain = (String ) jsonObject .get ("errorDomain" );
1120
+ final Integer statusCode = (Integer ) jsonObject .get ("responseCode" );
1121
+ final long requestDuration = ((Number ) jsonObject .get ("duration" )).longValue () / 1000 ;
1122
+ final long requestStartTime = ((Number ) jsonObject .get ("startTime" )).longValue () * 1000 ;
1123
+ final String requestHeaders = (new JSONObject ((HashMap <String , String >) jsonObject .get ("requestHeaders" ))).toString (4 );
1124
+ final String responseHeaders = (new JSONObject ((HashMap <String , String >) jsonObject .get ("responseHeaders" ))).toString (4 );
1125
+ final String errorMessage ;
1126
+
1127
+ if (errorDomain .equals ("" )) {
1128
+ errorMessage = null ;
1129
+ } else {
1130
+ errorMessage = errorDomain ;
1131
+ }
1132
+
1133
+ try {
1134
+ 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 );
1135
+ if (method != null ) {
1136
+ method .invoke (apmNetworkLogger , requestStartTime , requestDuration , requestHeaders , requestBody , requestMethod , requestUrl , requestContentType , responseHeaders , responseBody , statusCode , responseContentType , errorMessage );
1137
+ } else {
1138
+ Log .e ("IB-CP-Bridge" , "apmNetworkLogByReflection was not found by reflection" );
1139
+ }
1140
+ } catch (ClassNotFoundException e ) {
1141
+ e .printStackTrace ();
1142
+ } catch (IllegalAccessException e ) {
1143
+ e .printStackTrace ();
1144
+ } catch (InvocationTargetException e ) {
1145
+ e .printStackTrace ();
1146
+ }
1147
+
1148
+ }
1149
+
941
1150
/*
942
1151
*
943
1152
* Reports that the screen has been
0 commit comments