12
12
import io .sentry .Breadcrumb ;
13
13
import io .sentry .DateUtils ;
14
14
import io .sentry .Hint ;
15
- import io .sentry .ILogger ;
16
15
import io .sentry .IScopes ;
17
16
import io .sentry .ISentryLifecycleToken ;
18
17
import io .sentry .Integration ;
@@ -33,22 +32,17 @@ public final class NetworkBreadcrumbsIntegration implements Integration, Closeab
33
32
34
33
private final @ NotNull Context context ;
35
34
private final @ NotNull BuildInfoProvider buildInfoProvider ;
36
- private final @ NotNull ILogger logger ;
37
35
private final @ NotNull AutoClosableReentrantLock lock = new AutoClosableReentrantLock ();
38
- private volatile boolean isClosed ;
39
36
private @ Nullable SentryOptions options ;
40
37
41
38
@ TestOnly @ Nullable volatile NetworkBreadcrumbsNetworkCallback networkCallback ;
42
39
43
40
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 ) {
47
42
this .context =
48
43
Objects .requireNonNull (ContextUtils .getApplicationContext (context ), "Context is required" );
49
44
this .buildInfoProvider =
50
45
Objects .requireNonNull (buildInfoProvider , "BuildInfoProvider is required" );
51
- this .logger = Objects .requireNonNull (logger , "ILogger is required" );
52
46
}
53
47
54
48
@ Override
@@ -59,87 +53,61 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
59
53
(options instanceof SentryAndroidOptions ) ? (SentryAndroidOptions ) options : null ,
60
54
"SentryAndroidOptions is required" );
61
55
62
- logger .log (
63
- SentryLevel .DEBUG ,
64
- "NetworkBreadcrumbsIntegration enabled: %s" ,
65
- androidOptions .isEnableNetworkEventBreadcrumbs ());
66
-
67
56
this .options = options ;
68
57
58
+ options
59
+ .getLogger ()
60
+ .log (
61
+ SentryLevel .DEBUG ,
62
+ "NetworkBreadcrumbsIntegration enabled: %s" ,
63
+ androidOptions .isEnableNetworkEventBreadcrumbs ());
64
+
69
65
if (androidOptions .isEnableNetworkEventBreadcrumbs ()) {
70
66
71
67
// The specific error is logged in the ConnectivityChecker method
72
68
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+." );
74
70
return ;
75
71
}
76
72
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
+ }
110
90
}
111
91
}
112
92
}
113
93
114
94
@ Override
115
95
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
+ }
117
101
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 );
134
104
}
135
105
}
136
106
137
107
static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager .NetworkCallback {
138
108
final @ NotNull IScopes scopes ;
139
109
final @ NotNull BuildInfoProvider buildInfoProvider ;
140
110
141
- @ Nullable Network currentNetwork = null ;
142
-
143
111
@ Nullable NetworkCapabilities lastCapabilities = null ;
144
112
long lastCapabilityNanos = 0 ;
145
113
final @ NotNull SentryDateProvider dateProvider ;
@@ -156,21 +124,14 @@ static final class NetworkBreadcrumbsNetworkCallback extends ConnectivityManager
156
124
157
125
@ Override
158
126
public void onAvailable (final @ NonNull Network network ) {
159
- if (network .equals (currentNetwork )) {
160
- return ;
161
- }
162
127
final Breadcrumb breadcrumb = createBreadcrumb ("NETWORK_AVAILABLE" );
163
128
scopes .addBreadcrumb (breadcrumb );
164
- currentNetwork = network ;
165
129
lastCapabilities = null ;
166
130
}
167
131
168
132
@ Override
169
133
public void onCapabilitiesChanged (
170
134
final @ NonNull Network network , final @ NonNull NetworkCapabilities networkCapabilities ) {
171
- if (!network .equals (currentNetwork )) {
172
- return ;
173
- }
174
135
final long nowNanos = dateProvider .now ().nanoTimestamp ();
175
136
final @ Nullable NetworkBreadcrumbConnectionDetail connectionDetail =
176
137
getNewConnectionDetails (
@@ -195,12 +156,8 @@ public void onCapabilitiesChanged(
195
156
196
157
@ Override
197
158
public void onLost (final @ NonNull Network network ) {
198
- if (!network .equals (currentNetwork )) {
199
- return ;
200
- }
201
159
final Breadcrumb breadcrumb = createBreadcrumb ("NETWORK_LOST" );
202
160
scopes .addBreadcrumb (breadcrumb );
203
- currentNetwork = null ;
204
161
lastCapabilities = null ;
205
162
}
206
163
0 commit comments