1414import org .jetbrains .annotations .NotNull ;
1515import org .jetbrains .annotations .Nullable ;
1616
17+ /**
18+ * Note: ConnectivityManager sometimes throws SecurityExceptions on Android 11. Hence all relevant
19+ * calls are guarded with try/catch. see https://issuetracker.google.com/issues/175055271 for more
20+ * details
21+ */
1722@ ApiStatus .Internal
1823public final class ConnectivityChecker {
1924
@@ -62,13 +67,18 @@ private ConnectivityChecker() {}
6267 logger .log (SentryLevel .INFO , "No permission (ACCESS_NETWORK_STATE) to check network status." );
6368 return Status .NO_PERMISSION ;
6469 }
65- final android .net .NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo ();
6670
67- if (activeNetworkInfo == null ) {
68- logger .log (SentryLevel .INFO , "NetworkInfo is null, there's no active network." );
69- return Status .NOT_CONNECTED ;
71+ try {
72+ final android .net .NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo ();
73+ if (activeNetworkInfo == null ) {
74+ logger .log (SentryLevel .INFO , "NetworkInfo is null, there's no active network." );
75+ return Status .NOT_CONNECTED ;
76+ }
77+ return activeNetworkInfo .isConnected () ? Status .CONNECTED : Status .NOT_CONNECTED ;
78+ } catch (Throwable t ) {
79+ logger .log (SentryLevel .ERROR , "Could not retrieve Connection Status" , t );
80+ return Status .UNKNOWN ;
7081 }
71- return activeNetworkInfo .isConnected () ? Status .CONNECTED : Status .NOT_CONNECTED ;
7282 }
7383
7484 /**
@@ -93,79 +103,86 @@ private ConnectivityChecker() {}
93103 return null ;
94104 }
95105
96- boolean ethernet = false ;
97- boolean wifi = false ;
98- boolean cellular = false ;
106+ try {
107+ boolean ethernet = false ;
108+ boolean wifi = false ;
109+ boolean cellular = false ;
99110
100- if (buildInfoProvider .getSdkInfoVersion () >= Build .VERSION_CODES .M ) {
101- final Network activeNetwork = connectivityManager .getActiveNetwork ();
102- if (activeNetwork == null ) {
103- logger .log (SentryLevel .INFO , "Network is null and cannot check network status" );
104- return null ;
105- }
106- final NetworkCapabilities networkCapabilities =
107- connectivityManager .getNetworkCapabilities (activeNetwork );
108- if (networkCapabilities == null ) {
109- logger .log (SentryLevel .INFO , "NetworkCapabilities is null and cannot check network type" );
110- return null ;
111- }
112- if (networkCapabilities .hasTransport (NetworkCapabilities .TRANSPORT_ETHERNET )) {
113- ethernet = true ;
114- }
115- if (networkCapabilities .hasTransport (NetworkCapabilities .TRANSPORT_WIFI )) {
116- wifi = true ;
117- }
118- if (networkCapabilities .hasTransport (NetworkCapabilities .TRANSPORT_CELLULAR )) {
119- cellular = true ;
120- }
121- } else {
122- // ideally using connectivityManager.getAllNetworks(), but its >= Android L only
111+ if (buildInfoProvider .getSdkInfoVersion () >= Build .VERSION_CODES .M ) {
123112
124- // for some reason linting didn't allow a single @SuppressWarnings("deprecation") on method
125- // signature
126- @ SuppressWarnings ("deprecation" )
127- final android .net .NetworkInfo activeNetworkInfo = connectivityManager .getActiveNetworkInfo ();
113+ final Network activeNetwork = connectivityManager .getActiveNetwork ();
114+ if (activeNetwork == null ) {
115+ logger .log (SentryLevel .INFO , "Network is null and cannot check network status" );
116+ return null ;
117+ }
118+ final NetworkCapabilities networkCapabilities =
119+ connectivityManager .getNetworkCapabilities (activeNetwork );
120+ if (networkCapabilities == null ) {
121+ logger .log (SentryLevel .INFO , "NetworkCapabilities is null and cannot check network type" );
122+ return null ;
123+ }
124+ if (networkCapabilities .hasTransport (NetworkCapabilities .TRANSPORT_ETHERNET )) {
125+ ethernet = true ;
126+ }
127+ if (networkCapabilities .hasTransport (NetworkCapabilities .TRANSPORT_WIFI )) {
128+ wifi = true ;
129+ }
130+ if (networkCapabilities .hasTransport (NetworkCapabilities .TRANSPORT_CELLULAR )) {
131+ cellular = true ;
132+ }
133+ } else {
134+ // ideally using connectivityManager.getAllNetworks(), but its >= Android L only
128135
129- if (activeNetworkInfo == null ) {
130- logger .log (SentryLevel .INFO , "NetworkInfo is null, there's no active network." );
131- return null ;
132- }
136+ // for some reason linting didn't allow a single @SuppressWarnings("deprecation") on method
137+ // signature
138+ @ SuppressWarnings ("deprecation" )
139+ final android .net .NetworkInfo activeNetworkInfo =
140+ connectivityManager .getActiveNetworkInfo ();
133141
134- @ SuppressWarnings ("deprecation" )
135- final int type = activeNetworkInfo .getType ();
142+ if (activeNetworkInfo == null ) {
143+ logger .log (SentryLevel .INFO , "NetworkInfo is null, there's no active network." );
144+ return null ;
145+ }
136146
137- @ SuppressWarnings ("deprecation" )
138- final int TYPE_ETHERNET = ConnectivityManager . TYPE_ETHERNET ;
147+ @ SuppressWarnings ("deprecation" )
148+ final int type = activeNetworkInfo . getType () ;
139149
140- @ SuppressWarnings ("deprecation" )
141- final int TYPE_WIFI = ConnectivityManager .TYPE_WIFI ;
150+ @ SuppressWarnings ("deprecation" )
151+ final int TYPE_ETHERNET = ConnectivityManager .TYPE_ETHERNET ;
142152
143- @ SuppressWarnings ("deprecation" )
144- final int TYPE_MOBILE = ConnectivityManager .TYPE_MOBILE ;
153+ @ SuppressWarnings ("deprecation" )
154+ final int TYPE_WIFI = ConnectivityManager .TYPE_WIFI ;
145155
146- switch (type ) {
147- case TYPE_ETHERNET :
148- ethernet = true ;
149- break ;
150- case TYPE_WIFI :
151- wifi = true ;
152- break ;
153- case TYPE_MOBILE :
154- cellular = true ;
155- break ;
156+ @ SuppressWarnings ("deprecation" )
157+ final int TYPE_MOBILE = ConnectivityManager .TYPE_MOBILE ;
158+
159+ switch (type ) {
160+ case TYPE_ETHERNET :
161+ ethernet = true ;
162+ break ;
163+ case TYPE_WIFI :
164+ wifi = true ;
165+ break ;
166+ case TYPE_MOBILE :
167+ cellular = true ;
168+ break ;
169+ }
156170 }
157- }
158171
159- // TODO: change the protocol to be a list of transports as a device may have the capability of
160- // multiple
161- if (ethernet ) {
162- return "ethernet" ;
163- }
164- if (wifi ) {
165- return "wifi" ;
166- }
167- if (cellular ) {
168- return "cellular" ;
172+ // TODO: change the protocol to be a list of transports as a device may have the capability of
173+ // multiple
174+ if (ethernet ) {
175+ return "ethernet" ;
176+ }
177+ if (wifi ) {
178+ return "wifi" ;
179+ }
180+ if (cellular ) {
181+ return "cellular" ;
182+ }
183+
184+ } catch (Throwable exception ) {
185+ logger .log (SentryLevel .ERROR , "Failed to retrieve network info" , exception );
169186 }
170187
171188 return null ;
@@ -228,7 +245,12 @@ public static boolean registerNetworkCallback(
228245 logger .log (SentryLevel .INFO , "No permission (ACCESS_NETWORK_STATE) to check network status." );
229246 return false ;
230247 }
231- connectivityManager .registerDefaultNetworkCallback (networkCallback );
248+ try {
249+ connectivityManager .registerDefaultNetworkCallback (networkCallback );
250+ } catch (Throwable t ) {
251+ logger .log (SentryLevel .ERROR , "registerDefaultNetworkCallback failed" , t );
252+ return false ;
253+ }
232254 return true ;
233255 }
234256
@@ -245,6 +267,10 @@ public static void unregisterNetworkCallback(
245267 if (connectivityManager == null ) {
246268 return ;
247269 }
248- connectivityManager .unregisterNetworkCallback (networkCallback );
270+ try {
271+ connectivityManager .unregisterNetworkCallback (networkCallback );
272+ } catch (Throwable t ) {
273+ logger .log (SentryLevel .ERROR , "unregisterNetworkCallback failed" , t );
274+ }
249275 }
250276}
0 commit comments