Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register system callbacks on background thread #1292

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Avoid unnecessary BroadcastReceiver registration for monitoring device orientation
[#1303](https://github.com/bugsnag/bugsnag-android/pull/1303)

* Register system callbacks on background thread
[#1292](https://github.com/bugsnag/bugsnag-android/pull/1292)

## 5.9.5 (2021-06-25)

* Unity: Properly handle ANRs after multiple calls to autoNotify and autoDetectAnrs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public void onLowMemoryEvent() {
when(context.getApplicationContext()).thenReturn(context);
Client client = new Client(context, BugsnagTestUtils.generateConfiguration());

// block until observer is registered
client.bgTaskService.shutdown();

// capture the registered ComponentCallbacks
verify(context, times(1)).registerComponentCallbacks(componentCallbacksCaptor.capture());

Expand Down
34 changes: 26 additions & 8 deletions bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public class Client implements MetadataAware, CallbackAware, UserAware {

final SessionTracker sessionTracker;

private final SystemBroadcastReceiver systemBroadcastReceiver;
final SystemBroadcastReceiver systemBroadcastReceiver;
private final ActivityBreadcrumbCollector activityBreadcrumbCollector;
private final SessionLifecycleCallback sessionLifecycleCallback;

private final Connectivity connectivity;
final Connectivity connectivity;

@Nullable
private final StorageManager storageManager;
Expand Down Expand Up @@ -220,24 +220,23 @@ public Unit invoke(String activity, Map<String, ?> metadata) {
exceptionHandler.install();
}

// register a receiver for automatic breadcrumbs
systemBroadcastReceiver = SystemBroadcastReceiver.register(this, logger, bgTaskService);
registerComponentCallbacks();

// load last run info
lastRunInfoStore = new LastRunInfoStore(immutableConfig);
lastRunInfo = loadLastRunInfo();

// initialise plugins before attempting to flush any errors
loadPlugins(configuration);

connectivity.registerForNetworkChanges();

// Flush any on-disk errors and sessions
eventStore.flushOnLaunch();
eventStore.flushAsync();
sessionTracker.flushAsync();

// register listeners for system events in the background.
systemBroadcastReceiver = new SystemBroadcastReceiver(this, logger);
registerComponentCallbacks();
registerListenersInBackground();

// leave auto breadcrumb
Map<String, Object> data = Collections.emptyMap();
leaveAutoBreadcrumb("Bugsnag loaded", BreadcrumbType.STATE, data);
Expand Down Expand Up @@ -296,6 +295,25 @@ public Unit invoke(String activity, Map<String, ?> metadata) {
this.exceptionHandler = exceptionHandler;
}

/**
* Registers listeners for system events in the background. This offloads work from the main
* thread that collects useful information from callbacks, but that don't need to be done
* immediately on client construction.
*/
void registerListenersInBackground() {
try {
bgTaskService.submitTask(TaskType.DEFAULT, new Runnable() {
@Override
public void run() {
connectivity.registerForNetworkChanges();
SystemBroadcastReceiver.register(appContext, systemBroadcastReceiver, logger);
}
});
} catch (RejectedExecutionException ex) {
logger.w("Failed to register for system events", ex);
}
}

private LastRunInfo loadLastRunInfo() {
LastRunInfo lastRunInfo = lastRunInfoStore.load();
LastRunInfo currentRunInfo = new LastRunInfo(0, false, false);
Expand Down

This file was deleted.

Loading