Skip to content

Commit

Permalink
perf: register system callbacks on background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Jun 24, 2021
1 parent 4dd13bd commit 1768edd
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 209 deletions.
39 changes: 28 additions & 11 deletions bugsnag-android-core/src/main/java/com/bugsnag/android/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,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 @@ -223,11 +223,9 @@ public Unit invoke(String activity, Map<String, ?> metadata) {
exceptionHandler.install();
}

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

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

// load last run info
lastRunInfoStore = new LastRunInfoStore(immutableConfig);
Expand All @@ -236,8 +234,6 @@ public Unit invoke(String activity, Map<String, ?> metadata) {
// initialise plugins before attempting to flush any errors
loadPlugins(configuration);

connectivity.registerForNetworkChanges();

// Flush any on-disk errors and sessions
eventStore.flushOnLaunch();
eventStore.flushAsync();
Expand Down Expand Up @@ -301,6 +297,27 @@ 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);
registerOrientationChangeListener();
registerMemoryTrimListener();
}
});
} 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 Expand Up @@ -342,7 +359,7 @@ private MetadataState copyMetadataState(@NonNull Configuration configuration) {
return configuration.impl.metadataState.copy(copy);
}

private void registerOrientationChangeListener() {
void registerOrientationChangeListener() {
IntentFilter configFilter = new IntentFilter();
configFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
ConfigChangeReceiver receiver = new ConfigChangeReceiver(deviceDataCollector,
Expand All @@ -361,7 +378,7 @@ public Unit invoke(String oldOrientation, String newOrientation) {
ContextExtensionsKt.registerReceiverSafe(appContext, receiver, configFilter, logger);
}

private void registerMemoryTrimListener() {
void registerMemoryTrimListener() {
appContext.registerComponentCallbacks(new ClientComponentCallbacks(
new Function1<Boolean, Unit>() {
@Override
Expand Down

This file was deleted.

Loading

0 comments on commit 1768edd

Please sign in to comment.