Skip to content

Commit 7e8c8ff

Browse files
authored
[MOB-9125] Fix Main Thread Violation (#286)
* Remove Main Thread Wrappers * Run Non-void APIs on Background Thread
1 parent ed2537f commit 7e8c8ff

File tree

14 files changed

+225
-242
lines changed

14 files changed

+225
-242
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
* Fixes main thread violation on Android
34
* Fixes an issue with request and response headers parameters type causing network requests not getting logged on iOS
45
* Uses pigeon for internal communication between Flutter and the host platform
56

android/src/main/java/com/instabug/flutter/modules/ApmApi.java

Lines changed: 67 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
package com.instabug.flutter.modules;
22

3-
import android.os.Handler;
4-
import android.os.Looper;
53
import android.util.Log;
64

75
import androidx.annotation.NonNull;
8-
import androidx.annotation.Nullable;
96

107
import com.instabug.apm.APM;
118
import com.instabug.apm.model.ExecutionTrace;
129
import com.instabug.apm.networking.APMNetworkLogger;
1310
import com.instabug.flutter.generated.ApmPigeon;
1411
import com.instabug.flutter.util.ArgsRegistry;
1512
import com.instabug.flutter.util.Reflection;
13+
import com.instabug.flutter.util.ThreadManager;
1614

1715
import org.json.JSONObject;
1816

@@ -33,148 +31,109 @@ public static void init(BinaryMessenger messenger) {
3331

3432
@Override
3533
public void setEnabled(@NonNull Boolean isEnabled) {
36-
new Handler(Looper.getMainLooper()).post(new Runnable() {
37-
@Override
38-
public void run() {
39-
try {
40-
APM.setEnabled(isEnabled);
41-
} catch (Exception e) {
42-
e.printStackTrace();
43-
}
44-
}
45-
});
34+
try {
35+
APM.setEnabled(isEnabled);
36+
} catch (Exception e) {
37+
e.printStackTrace();
38+
}
4639
}
4740

4841
@Override
4942
public void setColdAppLaunchEnabled(@NonNull Boolean isEnabled) {
50-
new Handler(Looper.getMainLooper()).post(new Runnable() {
51-
@Override
52-
public void run() {
53-
try {
54-
APM.setAppLaunchEnabled(isEnabled);
55-
} catch (Exception e) {
56-
e.printStackTrace();
57-
}
58-
}
59-
});
43+
try {
44+
APM.setAppLaunchEnabled(isEnabled);
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
6048
}
6149

6250
@Override
6351
public void setAutoUITraceEnabled(@NonNull Boolean isEnabled) {
64-
new Handler(Looper.getMainLooper()).post(new Runnable() {
65-
@Override
66-
public void run() {
67-
try {
68-
APM.setAutoUITraceEnabled(isEnabled);
69-
} catch (Exception e) {
70-
e.printStackTrace();
71-
}
72-
}
73-
});
52+
try {
53+
APM.setAutoUITraceEnabled(isEnabled);
54+
} catch (Exception e) {
55+
e.printStackTrace();
56+
}
7457
}
7558

7659
@Override
7760
public void setLogLevel(@NonNull String level) {
78-
new Handler(Looper.getMainLooper()).post(new Runnable() {
79-
@Override
80-
public void run() {
81-
try {
82-
if (ArgsRegistry.getDeserializedValue(level) == null) {
83-
return;
84-
}
85-
APM.setLogLevel((int) ArgsRegistry.getRawValue(level));
86-
} catch (Exception e) {
87-
e.printStackTrace();
88-
}
89-
}
90-
});
91-
}
92-
93-
@Nullable
94-
@Override
95-
public String startExecutionTrace(@NonNull String id, @NonNull String name) {
9661
try {
97-
String result = null;
98-
ExecutionTrace trace = APM.startExecutionTrace(name);
99-
if (trace != null) {
100-
result = id;
101-
traces.put(id, trace);
62+
if (ArgsRegistry.getDeserializedValue(level) == null) {
63+
return;
10264
}
103-
return result;
65+
APM.setLogLevel((int) ArgsRegistry.getRawValue(level));
10466
} catch (Exception e) {
10567
e.printStackTrace();
106-
return null;
10768
}
10869
}
10970

11071
@Override
111-
public void setExecutionTraceAttribute(@NonNull String id, @NonNull String key, @NonNull String value) {
112-
new Handler(Looper.getMainLooper()).post(new Runnable() {
113-
@Override
114-
public void run() {
115-
try {
116-
traces.get(id).setAttribute(key, value);
117-
} catch (Exception e) {
118-
e.printStackTrace();
72+
public void startExecutionTrace(@NonNull String id, @NonNull String name, ApmPigeon.Result<String> result) {
73+
ThreadManager.runOnBackground(
74+
new Runnable() {
75+
@Override
76+
public void run() {
77+
try {
78+
ExecutionTrace trace = APM.startExecutionTrace(name);
79+
if (trace != null) {
80+
traces.put(id, trace);
81+
result.success(id);
82+
} else {
83+
result.success(null);
84+
}
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
result.success(null);
88+
}
89+
}
11990
}
120-
}
121-
});
91+
);
92+
}
93+
94+
@Override
95+
public void setExecutionTraceAttribute(@NonNull String id, @NonNull String key, @NonNull String value) {
96+
try {
97+
traces.get(id).setAttribute(key, value);
98+
} catch (Exception e) {
99+
e.printStackTrace();
100+
}
122101
}
123102

124103
@Override
125104
public void endExecutionTrace(@NonNull String id) {
126-
new Handler(Looper.getMainLooper()).post(new Runnable() {
127-
@Override
128-
public void run() {
129-
try {
130-
traces.get(id).end();
131-
} catch (Exception e) {
132-
e.printStackTrace();
133-
}
134-
}
135-
});
105+
try {
106+
traces.get(id).end();
107+
} catch (Exception e) {
108+
e.printStackTrace();
109+
}
136110
}
137111

138112
@Override
139113
public void startUITrace(@NonNull String name) {
140-
new Handler(Looper.getMainLooper()).post(new Runnable() {
141-
@Override
142-
public void run() {
143-
try {
144-
APM.startUITrace(name);
145-
} catch (Exception e) {
146-
e.printStackTrace();
147-
}
148-
}
149-
});
114+
try {
115+
APM.startUITrace(name);
116+
} catch (Exception e) {
117+
e.printStackTrace();
118+
}
150119
}
151120

152121
@Override
153122
public void endUITrace() {
154-
new Handler(Looper.getMainLooper()).post(new Runnable() {
155-
@Override
156-
public void run() {
157-
try {
158-
APM.endUITrace();
159-
} catch (Exception e) {
160-
e.printStackTrace();
161-
}
162-
}
163-
});
123+
try {
124+
APM.endUITrace();
125+
} catch (Exception e) {
126+
e.printStackTrace();
127+
}
164128
}
165129

166130
@Override
167131
public void endAppLaunch() {
168-
new Handler(Looper.getMainLooper()).post(new Runnable() {
169-
@Override
170-
public void run() {
171-
try {
172-
APM.endAppLaunch();
173-
} catch (Exception e) {
174-
e.printStackTrace();
175-
}
176-
}
177-
});
132+
try {
133+
APM.endAppLaunch();
134+
} catch (Exception e) {
135+
e.printStackTrace();
136+
}
178137
}
179138

180139
@Override

android/src/main/java/com/instabug/flutter/modules/BugReportingApi.java

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.instabug.flutter.modules;
22

3-
import android.os.Handler;
4-
import android.os.Looper;
5-
63
import androidx.annotation.NonNull;
74
import androidx.annotation.Nullable;
85

@@ -36,16 +33,11 @@ public BugReportingApi(BugReportingPigeon.BugReportingFlutterApi flutterApi) {
3633

3734
@Override
3835
public void setEnabled(@NonNull Boolean isEnabled) {
39-
new Handler(Looper.getMainLooper()).post(new Runnable() {
40-
@Override
41-
public void run() {
42-
if (isEnabled) {
43-
BugReporting.setState(Feature.State.ENABLED);
44-
} else {
45-
BugReporting.setState(Feature.State.DISABLED);
46-
}
47-
}
48-
});
36+
if (isEnabled) {
37+
BugReporting.setState(Feature.State.ENABLED);
38+
} else {
39+
BugReporting.setState(Feature.State.DISABLED);
40+
}
4941
}
5042

5143
@Override
@@ -60,32 +52,26 @@ public void show(@NonNull String reportType, @Nullable List<String> invocationOp
6052

6153
@Override
6254
public void setInvocationEvents(@NonNull List<String> events) {
63-
new Handler(Looper.getMainLooper()).post(new Runnable() {
64-
@Override
65-
public void run() {
66-
InstabugInvocationEvent[] invocationEventsArray = new InstabugInvocationEvent[events.size()];
67-
for (int i = 0; i < events.size(); i++) {
68-
String key = events.get(i);
69-
invocationEventsArray[i] = ArgsRegistry.getDeserializedValue(key);
70-
}
71-
BugReporting.setInvocationEvents(invocationEventsArray);
72-
}
73-
});
55+
InstabugInvocationEvent[] invocationEventsArray = new InstabugInvocationEvent[events.size()];
56+
57+
for (int i = 0; i < events.size(); i++) {
58+
String key = events.get(i);
59+
invocationEventsArray[i] = ArgsRegistry.getDeserializedValue(key);
60+
}
61+
62+
BugReporting.setInvocationEvents(invocationEventsArray);
7463
}
7564

7665
@Override
7766
public void setReportTypes(@NonNull List<String> types) {
78-
new Handler(Looper.getMainLooper()).post(new Runnable() {
79-
@Override
80-
public void run() {
81-
int[] reportTypesArray = new int[types.size()];
82-
for (int i = 0; i < types.size(); i++) {
83-
String key = types.get(i);
84-
reportTypesArray[i] = ArgsRegistry.getDeserializedValue(key);
85-
}
86-
BugReporting.setReportTypes(reportTypesArray);
87-
}
88-
});
67+
int[] reportTypesArray = new int[types.size()];
68+
69+
for (int i = 0; i < types.size(); i++) {
70+
String key = types.get(i);
71+
reportTypesArray[i] = ArgsRegistry.getDeserializedValue(key);
72+
}
73+
74+
BugReporting.setReportTypes(reportTypesArray);
8975
}
9076

9177
@Override
Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.instabug.flutter.modules;
22

3-
import android.os.Handler;
4-
import android.os.Looper;
53
import android.util.Log;
64

75
import androidx.annotation.NonNull;
@@ -27,35 +25,26 @@ public static void init(BinaryMessenger messenger) {
2725

2826
@Override
2927
public void setEnabled(@NonNull Boolean isEnabled) {
30-
new Handler(Looper.getMainLooper()).post(new Runnable() {
31-
@Override
32-
public void run() {
33-
if (isEnabled) {
34-
CrashReporting.setState(Feature.State.ENABLED);
35-
} else {
36-
CrashReporting.setState(Feature.State.DISABLED);
37-
}
38-
}
39-
});
28+
if (isEnabled) {
29+
CrashReporting.setState(Feature.State.ENABLED);
30+
} else {
31+
CrashReporting.setState(Feature.State.DISABLED);
32+
}
4033
}
4134

4235
@Override
4336
public void send(@NonNull String jsonCrash, @NonNull Boolean isHandled) {
44-
new Handler(Looper.getMainLooper()).post(new Runnable() {
45-
@Override
46-
public void run() {
47-
try {
48-
final JSONObject exceptionObject = new JSONObject(jsonCrash);
49-
Method method = Reflection.getMethod(Class.forName("com.instabug.crash.CrashReporting"), "reportException",
50-
JSONObject.class, boolean.class);
51-
if (method != null) {
52-
method.invoke(null, exceptionObject, isHandled);
53-
Log.e(TAG, exceptionObject.toString());
54-
}
55-
} catch (Exception e) {
56-
e.printStackTrace();
57-
}
37+
try {
38+
final JSONObject exceptionObject = new JSONObject(jsonCrash);
39+
Method method = Reflection.getMethod(Class.forName("com.instabug.crash.CrashReporting"), "reportException",
40+
JSONObject.class, boolean.class);
41+
if (method != null) {
42+
method.invoke(null, exceptionObject, isHandled);
43+
Log.e(TAG, exceptionObject.toString());
5844
}
59-
});
45+
} catch (Exception e) {
46+
e.printStackTrace();
47+
}
6048
}
49+
6150
}

0 commit comments

Comments
 (0)