@@ -380,36 +380,15 @@ private void updateCache() {
380380 return ;
381381 }
382382
383- // Fallback: query current active network in the background
384- submitSafe (
385- () -> {
386- // Avoid concurrent updates
387- if (!isUpdatingCache .getAndSet (true )) {
388- final ConnectivityManager connectivityManager =
389- getConnectivityManager (context , options .getLogger ());
390- if (connectivityManager != null ) {
391- final @ Nullable NetworkCapabilities capabilities =
392- getNetworkCapabilities (connectivityManager );
393-
394- try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
395- cachedNetworkCapabilities = capabilities ;
396- lastCacheUpdateTime = timeProvider .getCurrentTimeMillis ();
397-
398- if (capabilities != null ) {
399- options
400- .getLogger ()
401- .log (
402- SentryLevel .DEBUG ,
403- "Cache updated - Status: "
404- + getConnectionStatusFromCache ()
405- + ", Type: "
406- + getConnectionTypeFromCache ());
407- }
408- }
409- }
410- isUpdatingCache .set (false );
411- }
412- });
383+ // Avoid concurrent updates
384+ if (!isUpdatingCache .getAndSet (true )) {
385+ // Fallback: query current active network in the background
386+ if (options .getThreadChecker ().isMainThread ()) {
387+ submitSafe (() -> updateCacheFromConnectivityManager ());
388+ } else {
389+ updateCacheFromConnectivityManager ();
390+ }
391+ }
413392
414393 } catch (Throwable t ) {
415394 options .getLogger ().log (SentryLevel .WARNING , "Failed to update connection status cache" , t );
@@ -420,6 +399,33 @@ private void updateCache() {
420399 }
421400 }
422401
402+ @ RequiresApi (api = Build .VERSION_CODES .M )
403+ private void updateCacheFromConnectivityManager () {
404+ final ConnectivityManager connectivityManager =
405+ getConnectivityManager (context , options .getLogger ());
406+ if (connectivityManager != null ) {
407+ final @ Nullable NetworkCapabilities capabilities =
408+ getNetworkCapabilities (connectivityManager );
409+
410+ try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
411+ cachedNetworkCapabilities = capabilities ;
412+ lastCacheUpdateTime = timeProvider .getCurrentTimeMillis ();
413+
414+ if (capabilities != null ) {
415+ options
416+ .getLogger ()
417+ .log (
418+ SentryLevel .DEBUG ,
419+ "Cache updated - Status: "
420+ + getConnectionStatusFromCache ()
421+ + ", Type: "
422+ + getConnectionTypeFromCache ());
423+ }
424+ }
425+ }
426+ isUpdatingCache .set (false );
427+ }
428+
423429 private boolean isCacheValid () {
424430 return (timeProvider .getCurrentTimeMillis () - lastCacheUpdateTime ) < CACHE_TTL_MS ;
425431 }
0 commit comments