1212import io .sentry .Breadcrumb ;
1313import io .sentry .DateUtils ;
1414import io .sentry .Hint ;
15- import io .sentry .ILogger ;
1615import io .sentry .IScopes ;
1716import io .sentry .ISentryLifecycleToken ;
1817import io .sentry .Integration ;
@@ -33,22 +32,17 @@ public final class NetworkBreadcrumbsIntegration implements Integration, Closeab
3332
3433 private final @ NotNull Context context ;
3534 private final @ NotNull BuildInfoProvider buildInfoProvider ;
36- private final @ NotNull ILogger logger ;
3735 private final @ NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock ();
38- private volatile boolean isClosed ;
3936 private @ Nullable SentryOptions options ;
4037
4138 @ TestOnly @ Nullable volatile NetworkBreadcrumbsNetworkCallback networkCallback ;
4239
4340 public NetworkBreadcrumbsIntegration (
44- final @ NotNull Context context ,
45- final @ NotNull BuildInfoProvider buildInfoProvider ,
46- final @ NotNull ILogger logger ) {
41+ final @ NotNull Context context , final @ NotNull BuildInfoProvider buildInfoProvider ) {
4742 this .context =
4843 Objects .requireNonNull (ContextUtils .getApplicationContext (context ), "Context is required" );
4944 this .buildInfoProvider =
5045 Objects .requireNonNull (buildInfoProvider , "BuildInfoProvider is required" );
51- this .logger = Objects .requireNonNull (logger , "ILogger is required" );
5246 }
5347
5448 @ Override
@@ -59,87 +53,61 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
5953 (options instanceof SentryAndroidOptions ) ? (SentryAndroidOptions ) options : null ,
6054 "SentryAndroidOptions is required" );
6155
62- logger .log (
63- SentryLevel .DEBUG ,
64- "NetworkBreadcrumbsIntegration enabled: %s" ,
65- androidOptions .isEnableNetworkEventBreadcrumbs ());
66-
6756 this .options = options ;
6857
58+ options
59+ .getLogger ()
60+ .log (
61+ SentryLevel .DEBUG ,
62+ "NetworkBreadcrumbsIntegration enabled: %s" ,
63+ androidOptions .isEnableNetworkEventBreadcrumbs ());
64+
6965 if (androidOptions .isEnableNetworkEventBreadcrumbs ()) {
7066
7167 // The specific error is logged in the ConnectivityChecker method
7268 if (buildInfoProvider .getSdkInfoVersion () < Build .VERSION_CODES .N ) {
73- logger .log (SentryLevel .DEBUG , "NetworkCallbacks need Android N+." );
69+ options . getLogger () .log (SentryLevel .DEBUG , "NetworkCallbacks need Android N+." );
7470 return ;
7571 }
7672
77- try {
78- options
79- .getExecutorService ()
80- .submit (
81- new Runnable () {
82- @ Override
83- public void run () {
84- // in case integration is closed before the task is executed, simply return
85- if (isClosed ) {
86- return ;
87- }
88-
89- try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
90- networkCallback =
91- new NetworkBreadcrumbsNetworkCallback (
92- scopes , buildInfoProvider , options .getDateProvider ());
93-
94- final boolean registered =
95- AndroidConnectionStatusProvider .registerNetworkCallback (
96- context , logger , buildInfoProvider , networkCallback );
97- if (registered ) {
98- logger .log (SentryLevel .DEBUG , "NetworkBreadcrumbsIntegration installed." );
99- addIntegrationToSdkVersion ("NetworkBreadcrumbs" );
100- } else {
101- logger .log (
102- SentryLevel .DEBUG , "NetworkBreadcrumbsIntegration not installed." );
103- // The specific error is logged by AndroidConnectionStatusProvider
104- }
105- }
106- }
107- });
108- } catch (Throwable t ) {
109- logger .log (SentryLevel .ERROR , "Error submitting NetworkBreadcrumbsIntegration task." , t );
73+ try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
74+ networkCallback =
75+ new NetworkBreadcrumbsNetworkCallback (
76+ scopes , buildInfoProvider , options .getDateProvider ());
77+
78+ final boolean registered =
79+ AndroidConnectionStatusProvider .addNetworkCallback (
80+ context , options .getLogger (), buildInfoProvider , networkCallback );
81+ if (registered ) {
82+ options .getLogger ().log (SentryLevel .DEBUG , "NetworkBreadcrumbsIntegration installed." );
83+ addIntegrationToSdkVersion ("NetworkBreadcrumbs" );
84+ } else {
85+ options
86+ .getLogger ()
87+ .log (SentryLevel .DEBUG , "NetworkBreadcrumbsIntegration not installed." );
88+ // The specific error is logged by AndroidConnectionStatusProvider
89+ }
11090 }
11191 }
11292 }
11393
11494 @ Override
11595 public void close () throws IOException {
116- isClosed = true ;
96+ final @ Nullable ConnectivityManager .NetworkCallback callbackRef ;
97+ try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
98+ callbackRef = networkCallback ;
99+ networkCallback = null ;
100+ }
117101
118- try {
119- Objects .requireNonNull (options , "Options is required" )
120- .getExecutorService ()
121- .submit (
122- () -> {
123- try (final @ NotNull ISentryLifecycleToken ignored = lock .acquire ()) {
124- if (networkCallback != null ) {
125- AndroidConnectionStatusProvider .unregisterNetworkCallback (
126- context , logger , networkCallback );
127- logger .log (SentryLevel .DEBUG , "NetworkBreadcrumbsIntegration removed." );
128- }
129- networkCallback = null ;
130- }
131- });
132- } catch (Throwable t ) {
133- logger .log (SentryLevel .ERROR , "Error submitting NetworkBreadcrumbsIntegration task." , t );
102+ if (callbackRef != null ) {
103+ AndroidConnectionStatusProvider .removeNetworkCallback (callbackRef );
134104 }
135105 }
136106
137107 static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager .NetworkCallback {
138108 final @ NotNull IScopes scopes ;
139109 final @ NotNull BuildInfoProvider buildInfoProvider ;
140110
141- @ Nullable Network currentNetwork = null ;
142-
143111 @ Nullable NetworkCapabilities lastCapabilities = null ;
144112 long lastCapabilityNanos = 0 ;
145113 final @ NotNull SentryDateProvider dateProvider ;
@@ -156,21 +124,14 @@ static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager
156124
157125 @ Override
158126 public void onAvailable (final @ NonNull Network network ) {
159- if (network .equals (currentNetwork )) {
160- return ;
161- }
162127 final Breadcrumb breadcrumb = createBreadcrumb ("NETWORK_AVAILABLE" );
163128 scopes .addBreadcrumb (breadcrumb );
164- currentNetwork = network ;
165129 lastCapabilities = null ;
166130 }
167131
168132 @ Override
169133 public void onCapabilitiesChanged (
170134 final @ NonNull Network network , final @ NonNull NetworkCapabilities networkCapabilities ) {
171- if (!network .equals (currentNetwork )) {
172- return ;
173- }
174135 final long nowNanos = dateProvider .now ().nanoTimestamp ();
175136 final @ Nullable NetworkBreadcrumbConnectionDetail connectionDetail =
176137 getNewConnectionDetails (
@@ -195,12 +156,8 @@ public void onCapabilitiesChanged(
195156
196157 @ Override
197158 public void onLost (final @ NonNull Network network ) {
198- if (!network .equals (currentNetwork )) {
199- return ;
200- }
201159 final Breadcrumb breadcrumb = createBreadcrumb ("NETWORK_LOST" );
202160 scopes .addBreadcrumb (breadcrumb );
203- currentNetwork = null ;
204161 lastCapabilities = null ;
205162 }
206163
0 commit comments