Skip to content

Commit a4b92e2

Browse files
a7medevHeshamMegid
authored andcommitted
[INSD-9768] Fix Channel Method Calls on Background Threads (#370)
1 parent f082e99 commit a4b92e2

File tree

7 files changed

+127
-24
lines changed

7 files changed

+127
-24
lines changed

CHANGELOG.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,22 @@
44

55
### Fixed
66

7-
- Fix an issue with the `onInvoke` callback not being called and causing `Instabug.show` to break on Android ([#369](https://github.com/Instabug/Instabug-Flutter/pull/369)).
7+
- Fix an issue that caused APIs that return a value or invoke a callback break on Android in some versions of Flutter ([#370](https://github.com/Instabug/Instabug-Flutter/pull/370), [#369](https://github.com/Instabug/Instabug-Flutter/pull/369)).
8+
9+
Below is a list of all the affected APIs:
10+
11+
- `APM.startExecutionTrace`
12+
- `BugReporting.setOnInvokeCallback`
13+
- `BugReporting.setOnDismissCallback`
14+
- `Instabug.getTags`
15+
- `Instabug.getUserAttributeForKey`
16+
- `Instabug.getUserAttributes`
17+
- `Replies.getUnreadRepliesCount`
18+
- `Replies.hasChats`
19+
- `Replies.setOnNewReplyReceivedCallback`
20+
- `Surveys.hasRespondToSurvey`
21+
- `Surveys.setOnShowCallback`
22+
- `Surveys.setOnDismissCallback`
823

924
## [11.12.0](https://github.com/Instabug/Instabug-Flutter/compare/v11.10.1...v11.12.0) (May 30, 2023)
1025

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,30 @@ public void run() {
7676
ExecutionTrace trace = APM.startExecutionTrace(name);
7777
if (trace != null) {
7878
traces.put(id, trace);
79-
result.success(id);
79+
80+
ThreadManager.runOnMainThread(new Runnable() {
81+
@Override
82+
public void run() {
83+
result.success(id);
84+
}
85+
});
8086
} else {
81-
result.success(null);
87+
ThreadManager.runOnMainThread(new Runnable() {
88+
@Override
89+
public void run() {
90+
result.success(null);
91+
}
92+
});
8293
}
8394
} catch (Exception e) {
8495
e.printStackTrace();
85-
result.success(null);
96+
97+
ThreadManager.runOnMainThread(new Runnable() {
98+
@Override
99+
public void run() {
100+
result.success(null);
101+
}
102+
});
86103
}
87104
}
88105
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,14 @@ public void bindOnDismissCallback() {
155155
BugReporting.setOnDismissCallback(new OnSdkDismissCallback() {
156156
@Override
157157
public void call(DismissType dismissType, ReportType reportType) {
158-
flutterApi.onSdkDismiss(dismissType.toString(), reportType.toString(), new BugReportingPigeon.BugReportingFlutterApi.Reply<Void>() {
158+
ThreadManager.runOnMainThread(new Runnable() {
159159
@Override
160-
public void reply(Void reply) {
160+
public void run() {
161+
flutterApi.onSdkDismiss(dismissType.toString(), reportType.toString(), new BugReportingPigeon.BugReportingFlutterApi.Reply<Void>() {
162+
@Override
163+
public void reply(Void reply) {
164+
}
165+
});
161166
}
162167
});
163168
}

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ public void getTags(InstabugPigeon.Result<List<String>> result) {
196196
new Runnable() {
197197
@Override
198198
public void run() {
199-
result.success(Instabug.getTags());
199+
final List<String> tags = Instabug.getTags();
200+
201+
ThreadManager.runOnMainThread(new Runnable() {
202+
@Override
203+
public void run() {
204+
result.success(tags);
205+
}
206+
});
200207
}
201208
}
202209
);
@@ -234,7 +241,14 @@ public void getUserAttributeForKey(@NonNull String key, InstabugPigeon.Result<St
234241
new Runnable() {
235242
@Override
236243
public void run() {
237-
result.success(Instabug.getUserAttribute(key));
244+
final String attribute = Instabug.getUserAttribute(key);
245+
246+
ThreadManager.runOnMainThread(new Runnable() {
247+
@Override
248+
public void run() {
249+
result.success(attribute);
250+
}
251+
});
238252
}
239253
}
240254
);
@@ -246,7 +260,14 @@ public void getUserAttributes(InstabugPigeon.Result<Map<String, String>> result)
246260
new Runnable() {
247261
@Override
248262
public void run() {
249-
result.success(Instabug.getAllUserAttributes());
263+
final Map<String, String> attributes = Instabug.getAllUserAttributes();
264+
265+
ThreadManager.runOnMainThread(new Runnable() {
266+
@Override
267+
public void run() {
268+
result.success(attributes);
269+
}
270+
});
250271
}
251272
}
252273
);

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public void getUnreadRepliesCount(RepliesPigeon.Result<Long> result) {
5252
new Runnable() {
5353
@Override
5454
public void run() {
55-
result.success((long) Replies.getUnreadRepliesCount());
55+
final long count = Replies.getUnreadRepliesCount();
56+
57+
ThreadManager.runOnMainThread(new Runnable() {
58+
@Override
59+
public void run() {
60+
result.success(count);
61+
}
62+
});
5663
}
5764
}
5865
);
@@ -64,7 +71,14 @@ public void hasChats(RepliesPigeon.Result<Boolean> result) {
6471
new Runnable() {
6572
@Override
6673
public void run() {
67-
result.success(Replies.hasChats());
74+
final boolean hasChats = Replies.hasChats();
75+
76+
ThreadManager.runOnMainThread(new Runnable() {
77+
@Override
78+
public void run() {
79+
result.success(hasChats);
80+
}
81+
});
6882
}
6983
}
7084
);
@@ -75,9 +89,14 @@ public void bindOnNewReplyCallback() {
7589
Replies.setOnNewReplyReceivedCallback(new Runnable() {
7690
@Override
7791
public void run() {
78-
flutterApi.onNewReply(new RepliesPigeon.RepliesFlutterApi.Reply<Void>() {
92+
ThreadManager.runOnMainThread(new Runnable() {
7993
@Override
80-
public void reply(Void reply) {
94+
public void run() {
95+
flutterApi.onNewReply(new RepliesPigeon.RepliesFlutterApi.Reply<Void>() {
96+
@Override
97+
public void reply(Void reply) {
98+
}
99+
});
81100
}
82101
});
83102
}

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

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ public void hasRespondedToSurvey(@NonNull String surveyToken, SurveysPigeon.Resu
6969
@Override
7070
public void run() {
7171
final boolean hasResponded = Surveys.hasRespondToSurvey(surveyToken);
72-
result.success(hasResponded);
72+
73+
ThreadManager.runOnMainThread(new Runnable() {
74+
@Override
75+
public void run() {
76+
result.success(hasResponded);
77+
}
78+
});
7379
}
7480
}
7581
);
@@ -88,7 +94,12 @@ public void run() {
8894
titles.add(survey.getTitle());
8995
}
9096

91-
result.success(titles);
97+
ThreadManager.runOnMainThread(new Runnable() {
98+
@Override
99+
public void run() {
100+
result.success(titles);
101+
}
102+
});
92103
}
93104
}
94105
);
@@ -99,9 +110,14 @@ public void bindOnShowSurveyCallback() {
99110
Surveys.setOnShowCallback(new OnShowCallback() {
100111
@Override
101112
public void onShow() {
102-
flutterApi.onShowSurvey(new SurveysPigeon.SurveysFlutterApi.Reply<Void>() {
113+
ThreadManager.runOnMainThread(new Runnable() {
103114
@Override
104-
public void reply(Void reply) {
115+
public void run() {
116+
flutterApi.onShowSurvey(new SurveysPigeon.SurveysFlutterApi.Reply<Void>() {
117+
@Override
118+
public void reply(Void reply) {
119+
}
120+
});
105121
}
106122
});
107123
}
@@ -113,9 +129,14 @@ public void bindOnDismissSurveyCallback() {
113129
Surveys.setOnDismissCallback(new OnDismissCallback() {
114130
@Override
115131
public void onDismiss() {
116-
flutterApi.onDismissSurvey(new SurveysPigeon.SurveysFlutterApi.Reply<Void>() {
132+
ThreadManager.runOnMainThread(new Runnable() {
117133
@Override
118-
public void reply(Void reply) {
134+
public void run() {
135+
flutterApi.onDismissSurvey(new SurveysPigeon.SurveysFlutterApi.Reply<Void>() {
136+
@Override
137+
public void reply(Void reply) {
138+
}
139+
});
119140
}
120141
});
121142
}

android/src/test/java/com/instabug/flutter/util/GlobalMocks.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.json.JSONObject;
1212
import org.mockito.MockedStatic;
1313
import org.mockito.invocation.InvocationOnMock;
14+
import org.mockito.stubbing.Answer;
1415

1516
import java.lang.reflect.Method;
1617

@@ -26,13 +27,17 @@ public static void setUp() throws NoSuchMethodException {
2627

2728
// ThreadManager mock
2829
threadManager = mockStatic(ThreadManager.class);
30+
Answer threadAnswer = (InvocationOnMock invocation) -> {
31+
Runnable runnable = invocation.getArgument(0);
32+
runnable.run();
33+
return null;
34+
};
2935
threadManager
3036
.when(() -> ThreadManager.runOnBackground(any(Runnable.class)))
31-
.thenAnswer((InvocationOnMock invocation) -> {
32-
Runnable runnable = invocation.getArgument(0);
33-
runnable.run();
34-
return null;
35-
});
37+
.thenAnswer(threadAnswer);
38+
threadManager
39+
.when(() -> ThreadManager.runOnMainThread(any(Runnable.class)))
40+
.thenAnswer(threadAnswer);
3641

3742
// Reflection mock
3843
reflection = mockStatic(Reflection.class);

0 commit comments

Comments
 (0)