Skip to content
Prev Previous commit
Next Next commit
Offload system events breadcrumbs to background thread
  • Loading branch information
markushi committed Oct 8, 2024
commit 7561fe45bd88dc28af09a3a28dd7f990aaa3de70
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static final class SystemEventsBroadcastReceiver extends BroadcastReceiver {
private static final long DEBOUNCE_WAIT_TIME_MS = 60 * 1000;
private final @NotNull IHub hub;
private final @NotNull SentryAndroidOptions options;
private final @NotNull Debouncer debouncer =
private final @NotNull Debouncer batteryChangedDebouncer =
new Debouncer(AndroidCurrentDateProvider.getInstance(), DEBOUNCE_WAIT_TIME_MS, 0);

SystemEventsBroadcastReceiver(
Expand All @@ -221,19 +221,38 @@ static final class SystemEventsBroadcastReceiver extends BroadcastReceiver {
}

@Override
public void onReceive(Context context, Intent intent) {
final boolean shouldDebounce = debouncer.checkForDebounce();
final String action = intent.getAction();
public void onReceive(final Context context, final @NotNull Intent intent) {
final @Nullable String action = intent.getAction();
final boolean isBatteryChanged = ACTION_BATTERY_CHANGED.equals(action);
if (isBatteryChanged && shouldDebounce) {
// aligning with iOS which only captures battery status changes every minute at maximum

// aligning with iOS which only captures battery status changes every minute at maximum
if (isBatteryChanged && batteryChangedDebouncer.checkForDebounce()) {
return;
}

try {
options
.getExecutorService()
.submit(
() -> {
final Breadcrumb breadcrumb = createBreadcrumb(intent, action, isBatteryChanged);
final Hint hint = new Hint();
hint.set(ANDROID_INTENT, intent);
hub.addBreadcrumb(breadcrumb, hint);
});
} catch (Throwable t) {
options
.getLogger()
.log(SentryLevel.ERROR, t, "Failed to submit system event breadcrumb action.");
}
}

private @NotNull Breadcrumb createBreadcrumb(
final @NotNull Intent intent, final @Nullable String action, boolean isBatteryChanged) {
final Breadcrumb breadcrumb = new Breadcrumb();
breadcrumb.setType("system");
breadcrumb.setCategory("device.event");
String shortAction = StringUtils.getStringAfterDot(action);
final String shortAction = StringUtils.getStringAfterDot(action);
if (shortAction != null) {
breadcrumb.setData("action", shortAction);
}
Expand Down Expand Up @@ -273,11 +292,7 @@ public void onReceive(Context context, Intent intent) {
}
}
breadcrumb.setLevel(SentryLevel.INFO);

final Hint hint = new Hint();
hint.set(ANDROID_INTENT, intent);

hub.addBreadcrumb(breadcrumb, hint);
return breadcrumb;
}
}
}