Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 345e95f

Browse files
authored
Run all Instabug Android method calls on main thread (Instabug#395)
1 parent 6b84a80 commit 345e95f

15 files changed

+1300
-782
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
**If you are on React Native pre 0.60, please follow the steps [here](https://github.com/Instabug/Instabug-React-Native/blob/master/README.md#updating-to-version-864)**
44
* Fixes an issue on Android that would result in a build error with the message `null is not an object (evaluating u.invocationEventNone)`
5+
* Fixes an issue on Android that would result in some APIs not working when called immediately after `Instabug.start`
56

67
## v8.6.3 (2019-08-29)
78

android/src/main/java/com/instabug/reactlibrary/RNInstabugBugReportingModule.java

Lines changed: 136 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.facebook.react.bridge.WritableMap;
1515
import com.instabug.bug.BugReporting;
1616
import com.instabug.bug.invocation.Option;
17+
import com.instabug.chat.Replies;
1718
import com.instabug.library.Feature;
1819
import com.instabug.library.OnSdkDismissCallback;
1920
import com.instabug.library.extendedbugreport.ExtendedBugReport;
@@ -23,6 +24,7 @@
2324
import com.instabug.library.invocation.util.InstabugVideoRecordingButtonPosition;
2425
import com.instabug.reactlibrary.utils.ArrayUtil;
2526
import com.instabug.reactlibrary.utils.InstabugUtil;
27+
import com.instabug.reactlibrary.utils.MainThreadHandler;
2628

2729
import java.util.ArrayList;
2830
import java.util.Arrays;
@@ -47,7 +49,7 @@ public String getName() {
4749
*/
4850
@ReactMethod
4951
public void setEnabled(final boolean isEnabled) {
50-
new Handler(Looper.getMainLooper()).post(new Runnable() {
52+
MainThreadHandler.runOnMainThread(new Runnable() {
5153
@Override
5254
public void run() {
5355
try {
@@ -73,12 +75,17 @@ public void run() {
7375
*/
7476
@TargetApi(21)
7577
@ReactMethod
76-
public void setAutoScreenRecordingEnabled(boolean autoScreenRecordingEnabled) {
77-
try {
78-
BugReporting.setAutoScreenRecordingEnabled(autoScreenRecordingEnabled);
79-
} catch (Exception e) {
80-
e.printStackTrace();
81-
}
78+
public void setAutoScreenRecordingEnabled(final boolean autoScreenRecordingEnabled) {
79+
MainThreadHandler.runOnMainThread(new Runnable() {
80+
@Override
81+
public void run() {
82+
try {
83+
BugReporting.setAutoScreenRecordingEnabled(autoScreenRecordingEnabled);
84+
} catch (Exception e) {
85+
e.printStackTrace();
86+
}
87+
}
88+
});
8289
}
8390

8491
/**
@@ -88,30 +95,40 @@ public void setAutoScreenRecordingEnabled(boolean autoScreenRecordingEnabled) {
8895
* @param extendedBugReportMode
8996
*/
9097
@ReactMethod
91-
public void setExtendedBugReportMode(String extendedBugReportMode) {
92-
try {
93-
BugReporting.setExtendedBugReportState(
94-
ArgsRegistry.getDeserializedValue(extendedBugReportMode, ExtendedBugReport.State.class));
95-
} catch (Exception e) {
96-
e.printStackTrace();
97-
}
98+
public void setExtendedBugReportMode(final String extendedBugReportMode) {
99+
MainThreadHandler.runOnMainThread(new Runnable() {
100+
@Override
101+
public void run() {
102+
try {
103+
BugReporting.setExtendedBugReportState(
104+
ArgsRegistry.getDeserializedValue(extendedBugReportMode, ExtendedBugReport.State.class));
105+
} catch (Exception e) {
106+
e.printStackTrace();
107+
}
108+
}
109+
});
98110
}
99111

100112
/**
101113
* Enables or disables view hierarchy in the dashboard.
102114
* @param isEnabled boolean indicating enabled or disabled.
103115
*/
104116
@ReactMethod
105-
public void setViewHierarchyEnabled(boolean isEnabled) {
106-
try {
107-
if (isEnabled) {
108-
BugReporting.setViewHierarchyState(Feature.State.ENABLED);
109-
} else {
110-
BugReporting.setViewHierarchyState(Feature.State.DISABLED);
117+
public void setViewHierarchyEnabled(final boolean isEnabled) {
118+
MainThreadHandler.runOnMainThread(new Runnable() {
119+
@Override
120+
public void run() {
121+
try {
122+
if (isEnabled) {
123+
BugReporting.setViewHierarchyState(Feature.State.ENABLED);
124+
} else {
125+
BugReporting.setViewHierarchyState(Feature.State.DISABLED);
126+
}
127+
} catch (Exception e) {
128+
e.printStackTrace();
129+
}
111130
}
112-
} catch (Exception e) {
113-
e.printStackTrace();
114-
}
131+
});
115132
}
116133

117134
/**
@@ -120,13 +137,18 @@ public void setViewHierarchyEnabled(boolean isEnabled) {
120137
* @param corner corner to stick the video recording floating button to
121138
*/
122139
@ReactMethod
123-
public void setVideoRecordingFloatingButtonPosition(String corner) {
124-
try {
125-
BugReporting.setVideoRecordingFloatingButtonPosition(
126-
ArgsRegistry.getDeserializedValue(corner, InstabugVideoRecordingButtonPosition.class));
127-
} catch (Exception e) {
128-
e.printStackTrace();
129-
}
140+
public void setVideoRecordingFloatingButtonPosition(final String corner) {
141+
MainThreadHandler.runOnMainThread(new Runnable() {
142+
@Override
143+
public void run() {
144+
try {
145+
BugReporting.setVideoRecordingFloatingButtonPosition(
146+
ArgsRegistry.getDeserializedValue(corner, InstabugVideoRecordingButtonPosition.class));
147+
} catch (Exception e) {
148+
e.printStackTrace();
149+
}
150+
}
151+
});
130152
}
131153

132154
/**
@@ -138,14 +160,19 @@ public void setVideoRecordingFloatingButtonPosition(String corner) {
138160
* @param {boolean} screenRecording A boolean to enable or disable screen recording attachments.
139161
*/
140162
@ReactMethod
141-
public void setEnabledAttachmentTypes(boolean screenshot, boolean extraScreenshot, boolean
142-
galleryImage, boolean screenRecording) {
143-
try {
144-
BugReporting.setAttachmentTypesEnabled(screenshot, extraScreenshot, galleryImage,
145-
screenRecording);
146-
} catch (Exception e) {
147-
e.printStackTrace();
148-
}
163+
public void setEnabledAttachmentTypes(final boolean screenshot, final boolean extraScreenshot, final boolean
164+
galleryImage, final boolean screenRecording) {
165+
MainThreadHandler.runOnMainThread(new Runnable() {
166+
@Override
167+
public void run() {
168+
try {
169+
BugReporting.setAttachmentTypesEnabled(screenshot, extraScreenshot, galleryImage,
170+
screenRecording);
171+
} catch (Exception e) {
172+
e.printStackTrace();
173+
}
174+
}
175+
});
149176
}
150177

151178
/**
@@ -157,7 +184,7 @@ public void setEnabledAttachmentTypes(boolean screenshot, boolean extraScreensho
157184
*/
158185
@ReactMethod
159186
public void setInvocationEvent(final String invocationEventValue) {
160-
new Handler(Looper.getMainLooper()).post(new Runnable() {
187+
MainThreadHandler.runOnMainThread(new Runnable() {
161188
@Override
162189
public void run() {
163190
try {
@@ -187,7 +214,7 @@ public void setInvocationEvents(ReadableArray invocationEventValues) {
187214
for (String event : stringArray) {
188215
parsedInvocationEvents.add(ArgsRegistry.getDeserializedValue(event, InstabugInvocationEvent.class));
189216
}
190-
new Handler(Looper.getMainLooper()).post(new Runnable() {
217+
MainThreadHandler.runOnMainThread(new Runnable() {
191218
@Override
192219
public void run() {
193220
try {
@@ -208,18 +235,21 @@ public void run() {
208235
* @param optionValues the invocation option value
209236
*/
210237
@ReactMethod
211-
public void setOptions(ReadableArray optionValues) {
212-
try {
213-
214-
Object[] objectArray = ArrayUtil.toArray(optionValues);
215-
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
216-
for (String option : stringArray) {
217-
BugReporting.setOptions((int) ArgsRegistry.getRawValue(option));
238+
public void setOptions(final ReadableArray optionValues) {
239+
MainThreadHandler.runOnMainThread(new Runnable() {
240+
@Override
241+
public void run() {
242+
try {
243+
Object[] objectArray = ArrayUtil.toArray(optionValues);
244+
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
245+
for (String option : stringArray) {
246+
BugReporting.setOptions((int) ArgsRegistry.getRawValue(option));
247+
}
248+
} catch (Exception e) {
249+
e.printStackTrace();
250+
}
218251
}
219-
220-
} catch (Exception e) {
221-
e.printStackTrace();
222-
}
252+
});
223253
}
224254

225255
/**
@@ -232,16 +262,21 @@ public void setOptions(ReadableArray optionValues) {
232262
*/
233263
@ReactMethod
234264
public void setOnInvokeHandler(final Callback onInvokeHandler) {
235-
try {
236-
BugReporting.setOnInvokeCallback(new OnInvokeCallback() {
237-
@Override
238-
public void onInvoke() {
239-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_PRE_INVOCATION_HANDLER, null);
265+
MainThreadHandler.runOnMainThread(new Runnable() {
266+
@Override
267+
public void run() {
268+
try {
269+
BugReporting.setOnInvokeCallback(new OnInvokeCallback() {
270+
@Override
271+
public void onInvoke() {
272+
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_PRE_INVOCATION_HANDLER, null);
273+
}
274+
});
275+
} catch (java.lang.Exception exception) {
276+
exception.printStackTrace();
240277
}
241-
});
242-
} catch (java.lang.Exception exception) {
243-
exception.printStackTrace();
244-
}
278+
}
279+
});
245280
}
246281

247282
/**
@@ -251,7 +286,7 @@ public void onInvoke() {
251286
*/
252287
@ReactMethod
253288
public void setFloatingButtonEdge(final String floatingButtonEdge, final int floatingButtonOffset) {
254-
new Handler(Looper.getMainLooper()).post(new Runnable() {
289+
MainThreadHandler.runOnMainThread(new Runnable() {
255290
@Override
256291
public void run() {
257292
BugReporting.setFloatingButtonOffset(floatingButtonOffset);
@@ -273,19 +308,24 @@ public void run() {
273308
*/
274309
@ReactMethod
275310
public void setOnSDKDismissedHandler(final Callback handler) {
276-
try {
277-
BugReporting.setOnDismissCallback(new OnSdkDismissCallback() {
278-
@Override
279-
public void call(DismissType dismissType, ReportType reportType) {
280-
WritableMap params = Arguments.createMap();
281-
params.putString("dismissType", dismissType.toString());
282-
params.putString("reportType", reportType.toString());
283-
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_POST_INVOCATION_HANDLER, params);
311+
MainThreadHandler.runOnMainThread(new Runnable() {
312+
@Override
313+
public void run() {
314+
try {
315+
BugReporting.setOnDismissCallback(new OnSdkDismissCallback() {
316+
@Override
317+
public void call(DismissType dismissType, ReportType reportType) {
318+
WritableMap params = Arguments.createMap();
319+
params.putString("dismissType", dismissType.toString());
320+
params.putString("reportType", reportType.toString());
321+
InstabugUtil.sendEvent(getReactApplicationContext(), Constants.IBG_POST_INVOCATION_HANDLER, params);
322+
}
323+
});
324+
} catch (java.lang.Exception exception) {
325+
exception.printStackTrace();
284326
}
285-
});
286-
} catch (java.lang.Exception exception) {
287-
exception.printStackTrace();
288-
}
327+
}
328+
});
289329
}
290330

291331
/**
@@ -297,12 +337,17 @@ public void call(DismissType dismissType, ReportType reportType) {
297337
* @param androidThreshold Threshold for android devices.
298338
*/
299339
@ReactMethod
300-
public void setShakingThresholdForAndroid(int androidThreshold) {
301-
try {
302-
BugReporting.setShakingThreshold(androidThreshold);
303-
} catch (Exception e) {
304-
e.printStackTrace();
305-
}
340+
public void setShakingThresholdForAndroid(final int androidThreshold) {
341+
MainThreadHandler.runOnMainThread(new Runnable() {
342+
@Override
343+
public void run() {
344+
try {
345+
BugReporting.setShakingThreshold(androidThreshold);
346+
} catch (Exception e) {
347+
e.printStackTrace();
348+
}
349+
}
350+
});
306351
}
307352

308353
/**
@@ -319,7 +364,7 @@ public void setReportTypes(ReadableArray types) {
319364
for (int i = 0; i < stringArray.length; i++) {
320365
parsedReportTypes[i] = (int) ArgsRegistry.getRawValue(stringArray[i]);
321366
}
322-
new Handler(Looper.getMainLooper()).post(new Runnable() {
367+
MainThreadHandler.runOnMainThread(new Runnable() {
323368
@Override
324369
public void run() {
325370
try {
@@ -339,13 +384,17 @@ public void run() {
339384
* @see Option
340385
*/
341386
@ReactMethod
342-
public void show(String reportType, ReadableArray options) {
343-
if (ArgsRegistry.getDeserializedValue(reportType, Integer.class) == null) {
344-
return;
345-
}
346-
BugReporting.show((int) ArgsRegistry.getRawValue(reportType));
347-
setOptions(options);
348-
387+
public void show(final String reportType, final ReadableArray options) {
388+
MainThreadHandler.runOnMainThread(new Runnable() {
389+
@Override
390+
public void run() {
391+
if (ArgsRegistry.getDeserializedValue(reportType, Integer.class) == null) {
392+
return;
393+
}
394+
BugReporting.show((int) ArgsRegistry.getRawValue(reportType));
395+
setOptions(options);
396+
}
397+
});
349398
}
350399

351400

android/src/main/java/com/instabug/reactlibrary/RNInstabugChatsModule.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import com.facebook.react.bridge.ReactApplicationContext;
77
import com.facebook.react.bridge.ReactContextBaseJavaModule;
88
import com.facebook.react.bridge.ReactMethod;
9+
import com.instabug.bug.BugReporting;
910
import com.instabug.chat.Chats;
1011
import com.instabug.library.Feature;
12+
import com.instabug.reactlibrary.utils.InstabugUtil;
13+
import com.instabug.reactlibrary.utils.MainThreadHandler;
1114

1215
import javax.annotation.Nonnull;
1316

@@ -30,7 +33,7 @@ public String getName() {
3033
*/
3134
@ReactMethod
3235
public void setEnabled(final boolean isEnabled) {
33-
new Handler(Looper.getMainLooper()).post(new Runnable() {
36+
MainThreadHandler.runOnMainThread(new Runnable() {
3437
@Override
3538
public void run() {
3639
try {
@@ -52,6 +55,11 @@ public void run() {
5255
*/
5356
@ReactMethod
5457
public void show() {
55-
Chats.show();
58+
MainThreadHandler.runOnMainThread(new Runnable() {
59+
@Override
60+
public void run() {
61+
Chats.show();
62+
}
63+
});
5664
}
5765
}

0 commit comments

Comments
 (0)