Skip to content

Commit

Permalink
Merge pull request #1292 from bugsnag/offload-init-work
Browse files Browse the repository at this point in the history
Register system callbacks on background thread
  • Loading branch information
fractalwrench authored Jun 29, 2021
2 parents 361e2b5 + f1190ac commit 1158d2d
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 206 deletions.
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

0 comments on commit 1158d2d

Please sign in to comment.