3535 * details
3636 */
3737@ ApiStatus .Internal
38- public final class AndroidConnectionStatusProvider implements IConnectionStatusProvider ,
39- AppState .AppStateListener {
38+ public final class AndroidConnectionStatusProvider
39+ implements IConnectionStatusProvider , AppState .AppStateListener {
4040
4141 private final @ NotNull Context context ;
4242 private final @ NotNull SentryOptions options ;
@@ -67,7 +67,7 @@ public final class AndroidConnectionStatusProvider implements IConnectionStatusP
6767 private volatile long lastCacheUpdateTime = 0 ;
6868 private static final long CACHE_TTL_MS = 2 * 60 * 1000L ; // 2 minutes
6969 private final @ NotNull AtomicBoolean isConnected = new AtomicBoolean (false );
70-
70+
7171 @ SuppressLint ("InlinedApi" )
7272 public AndroidConnectionStatusProvider (
7373 @ NotNull Context context ,
@@ -172,7 +172,8 @@ private void ensureNetworkCallbackRegistered() {
172172 public void onAvailable (final @ NotNull Network network ) {
173173 currentNetwork = network ;
174174
175- // have to only dispatch this on first registration + when the connection got re-established
175+ // have to only dispatch this on first registration + when the connection got
176+ // re-established
176177 // otherwise it would've been dispatched on every foreground
177178 if (!isConnected .getAndSet (true )) {
178179 try (final @ NotNull ISentryLifecycleToken ignored = childCallbacksLock .acquire ()) {
@@ -452,12 +453,11 @@ private void unregisterNetworkCallback(final boolean clearObservers) {
452453 public void close () {
453454 submitSafe (
454455 () -> {
455- unregisterNetworkCallback (/* clearObservers = */ true );
456+ unregisterNetworkCallback (/* clearObservers= */ true );
456457 try (final @ NotNull ISentryLifecycleToken ignored = childCallbacksLock .acquire ()) {
457458 childCallbacks .clear ();
458459 }
459- try (final @ NotNull ISentryLifecycleToken ignored =
460- connectivityManagerLock .acquire ()) {
460+ try (final @ NotNull ISentryLifecycleToken ignored = connectivityManagerLock .acquire ()) {
461461 connectivityManager = null ;
462462 }
463463 AppState .getInstance ().removeAppStateListener (this );
@@ -470,30 +470,32 @@ public void onForeground() {
470470 return ;
471471 }
472472
473- submitSafe (() -> {
474- // proactively update cache and notify observers on foreground to ensure connectivity state is not stale
475- updateCache (null );
476-
477- final @ NotNull ConnectionStatus status = getConnectionStatusFromCache ();
478- if (status == ConnectionStatus .DISCONNECTED ) {
479- // onLost is not called retroactively when we registerNetworkCallback (as opposed to onAvailable), so we have to do it manually for the DISCONNECTED case
480- isConnected .set (false );
481- try (final @ NotNull ISentryLifecycleToken ignored = childCallbacksLock .acquire ()) {
482- for (final @ NotNull NetworkCallback cb : childCallbacks ) {
483- //noinspection DataFlowIssue
484- cb .onLost (null );
473+ submitSafe (
474+ () -> {
475+ // proactively update cache and notify observers on foreground to ensure connectivity
476+ // state is not stale
477+ updateCache (null );
478+
479+ final @ NotNull ConnectionStatus status = getConnectionStatusFromCache ();
480+ if (status == ConnectionStatus .DISCONNECTED ) {
481+ // onLost is not called retroactively when we registerNetworkCallback (as opposed to
482+ // onAvailable), so we have to do it manually for the DISCONNECTED case
483+ isConnected .set (false );
484+ try (final @ NotNull ISentryLifecycleToken ignored = childCallbacksLock .acquire ()) {
485+ for (final @ NotNull NetworkCallback cb : childCallbacks ) {
486+ //noinspection DataFlowIssue
487+ cb .onLost (null );
488+ }
489+ }
490+ }
491+ try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
492+ for (final @ NotNull IConnectionStatusObserver observer : connectionStatusObservers ) {
493+ observer .onConnectionStatusChanged (status );
494+ }
485495 }
486- }
487- }
488- try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
489- for (final @ NotNull IConnectionStatusObserver observer :
490- connectionStatusObservers ) {
491- observer .onConnectionStatusChanged (status );
492- }
493- }
494496
495- ensureNetworkCallbackRegistered ();
496- });
497+ ensureNetworkCallbackRegistered ();
498+ });
497499 }
498500
499501 @ Override
@@ -502,10 +504,11 @@ public void onBackground() {
502504 return ;
503505 }
504506
505- submitSafe (() -> {
506- //noinspection Convert2MethodRef
507- unregisterNetworkCallback (/* clearObservers = */ false );
508- });
507+ submitSafe (
508+ () -> {
509+ //noinspection Convert2MethodRef
510+ unregisterNetworkCallback (/* clearObservers= */ false );
511+ });
509512 }
510513
511514 /**
@@ -796,7 +799,8 @@ private void submitSafe(@NotNull Runnable r) {
796799 try {
797800 options .getExecutorService ().submit (r );
798801 } catch (Throwable e ) {
799- options .getLogger ()
802+ options
803+ .getLogger ()
800804 .log (SentryLevel .ERROR , "AndroidConnectionStatusProvider submit failed" , e );
801805 }
802806 }
0 commit comments