Skip to content
Prev Previous commit
Address PR feedback
  • Loading branch information
markushi committed Oct 9, 2024
commit e524d077db5e07584c3dd676f18ad3ed0543737b
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,25 @@ public void close() throws IOException {
@SuppressWarnings("deprecation")
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
executeInBackground(() -> captureConfigurationChangedBreadcrumb(newConfig));
final long now = System.currentTimeMillis();
executeInBackground(() -> captureConfigurationChangedBreadcrumb(now, newConfig));
}

@Override
public void onLowMemory() {
executeInBackground(() -> captureLowMemoryBreadcrumb(null));
final long now = System.currentTimeMillis();
executeInBackground(() -> captureLowMemoryBreadcrumb(now, null));
}

@Override
public void onTrimMemory(final int level) {
executeInBackground(() -> captureLowMemoryBreadcrumb(level));
final long now = System.currentTimeMillis();
executeInBackground(() -> captureLowMemoryBreadcrumb(now, level));
}

private void captureLowMemoryBreadcrumb(final @Nullable Integer level) {
private void captureLowMemoryBreadcrumb(final long timeMs, final @Nullable Integer level) {
if (hub != null) {
final Breadcrumb breadcrumb = new Breadcrumb();
final Breadcrumb breadcrumb = new Breadcrumb(timeMs);
if (level != null) {
// only add breadcrumb if TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_MODERATE or
// TRIM_MEMORY_COMPLETE.
Expand All @@ -127,7 +130,8 @@ private void captureLowMemoryBreadcrumb(final @Nullable Integer level) {
}
}

private void captureConfigurationChangedBreadcrumb(final @NotNull Configuration newConfig) {
private void captureConfigurationChangedBreadcrumb(
final long timeMs, final @NotNull Configuration newConfig) {
if (hub != null) {
final Device.DeviceOrientation deviceOrientation =
DeviceOrientations.getOrientation(context.getResources().getConfiguration().orientation);
Expand All @@ -139,7 +143,7 @@ private void captureConfigurationChangedBreadcrumb(final @NotNull Configuration
orientation = "undefined";
}

final Breadcrumb breadcrumb = new Breadcrumb();
final Breadcrumb breadcrumb = new Breadcrumb(timeMs);
breadcrumb.setType("navigation");
breadcrumb.setCategory("device.orientation");
breadcrumb.setData("position", orientation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class NetworkBreadcrumbsIntegration implements Integration, Closeab
private final @NotNull Context context;
private final @NotNull BuildInfoProvider buildInfoProvider;
private final @NotNull ILogger logger;
private final @NotNull Object lock = new Object();
private volatile boolean isClosed;
private @Nullable SentryOptions options;

Expand Down Expand Up @@ -73,9 +74,6 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
return;
}

networkCallback =
new NetworkBreadcrumbsNetworkCallback(hub, buildInfoProvider, options.getDateProvider());

try {
options
.getExecutorService()
Expand All @@ -84,21 +82,26 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
@Override
public void run() {
// in case integration is closed before the task is executed, simply return
final @Nullable NetworkBreadcrumbsNetworkCallback callback = networkCallback;
if (isClosed || callback == null) {
networkCallback = null;
if (isClosed) {
return;
}

final boolean registered =
AndroidConnectionStatusProvider.registerNetworkCallback(
context, logger, buildInfoProvider, callback);
if (registered) {
logger.log(SentryLevel.DEBUG, "NetworkBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion(getClass());
} else {
logger.log(SentryLevel.DEBUG, "NetworkBreadcrumbsIntegration not installed.");
// The specific error is logged by AndroidConnectionStatusProvider
synchronized (lock) {
networkCallback =
new NetworkBreadcrumbsNetworkCallback(
hub, buildInfoProvider, options.getDateProvider());

final boolean registered =
AndroidConnectionStatusProvider.registerNetworkCallback(
context, logger, buildInfoProvider, networkCallback);
if (registered) {
logger.log(SentryLevel.DEBUG, "NetworkBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion(getClass());
} else {
logger.log(
SentryLevel.DEBUG, "NetworkBreadcrumbsIntegration not installed.");
// The specific error is logged by AndroidConnectionStatusProvider
}
}
}
});
Expand All @@ -117,13 +120,14 @@ public void close() throws IOException {
.getExecutorService()
.submit(
() -> {
final @Nullable NetworkBreadcrumbsNetworkCallback callback = networkCallback;
if (callback != null) {
AndroidConnectionStatusProvider.unregisterNetworkCallback(
context, logger, buildInfoProvider, callback);
logger.log(SentryLevel.DEBUG, "NetworkBreadcrumbsIntegration removed.");
synchronized (lock) {
if (networkCallback != null) {
AndroidConnectionStatusProvider.unregisterNetworkCallback(
context, logger, buildInfoProvider, networkCallback);
logger.log(SentryLevel.DEBUG, "NetworkBreadcrumbsIntegration removed.");
}
networkCallback = null;
}
networkCallback = null;
});
} catch (Throwable t) {
logger.log(SentryLevel.ERROR, "Error submitting NetworkBreadcrumbsIntegration task.", t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ public void onReceive(final Context context, final @NotNull Intent intent) {
return;
}

final long now = System.currentTimeMillis();
try {
options
.getExecutorService()
.submit(
() -> {
final Breadcrumb breadcrumb = createBreadcrumb(intent, action, isBatteryChanged);
final Breadcrumb breadcrumb =
createBreadcrumb(now, intent, action, isBatteryChanged);
final Hint hint = new Hint();
hint.set(ANDROID_INTENT, intent);
hub.addBreadcrumb(breadcrumb, hint);
Expand All @@ -248,8 +250,11 @@ public void onReceive(final Context context, final @NotNull Intent intent) {
}

private @NotNull Breadcrumb createBreadcrumb(
final @NotNull Intent intent, final @Nullable String action, boolean isBatteryChanged) {
final Breadcrumb breadcrumb = new Breadcrumb();
final long timeMs,
final @NotNull Intent intent,
final @Nullable String action,
boolean isBatteryChanged) {
final Breadcrumb breadcrumb = new Breadcrumb(timeMs);
breadcrumb.setType("system");
breadcrumb.setCategory("device.event");
final String shortAction = StringUtils.getStringAfterDot(action);
Expand Down