@@ -15,8 +15,7 @@ partial class ConnectivityImplementation : IConnectivity
15
15
/// Unique identifier for the connectivity changed action on Android.
16
16
/// </summary>
17
17
public const string ConnectivityChangedAction = "com.maui.essentials.ESSENTIALS_CONNECTIVITY_CHANGED" ;
18
- static Intent connectivityIntent = new Intent ( ConnectivityChangedAction ) ;
19
-
18
+
20
19
static ConnectivityManager connectivityManager ;
21
20
22
21
static ConnectivityManager ConnectivityManager =>
@@ -45,13 +44,15 @@ void StartListeners()
45
44
46
45
conectivityReceiver = new ConnectivityBroadcastReceiver ( OnConnectivityChanged ) ;
47
46
48
- PlatformUtils . RegisterBroadcastReceiver ( conectivityReceiver , filter , true ) ;
47
+ PlatformUtils . RegisterBroadcastReceiver ( conectivityReceiver , filter ) ;
49
48
}
50
49
51
50
void StopListeners ( )
52
51
{
53
52
if ( conectivityReceiver == null )
53
+ {
54
54
return ;
55
+ }
55
56
56
57
try
57
58
{
@@ -77,11 +78,15 @@ void StopListeners()
77
78
void RegisterNetworkCallback ( )
78
79
{
79
80
if ( ! OperatingSystem . IsAndroidVersionAtLeast ( 24 ) )
81
+ {
80
82
return ;
83
+ }
81
84
82
85
var manager = ConnectivityManager ;
83
86
if ( manager == null )
87
+ {
84
88
return ;
89
+ }
85
90
86
91
var request = new NetworkRequest . Builder ( ) . Build ( ) ;
87
92
networkCallback = new EssentialsNetworkCallback ( ) ;
@@ -91,11 +96,15 @@ void RegisterNetworkCallback()
91
96
void UnregisterNetworkCallback ( )
92
97
{
93
98
if ( ! OperatingSystem . IsAndroidVersionAtLeast ( 24 ) )
99
+ {
94
100
return ;
101
+ }
95
102
96
103
var manager = ConnectivityManager ;
97
104
if ( manager == null || networkCallback == null )
105
+ {
98
106
return ;
107
+ }
99
108
100
109
manager . UnregisterNetworkCallback ( networkCallback ) ;
101
110
@@ -104,6 +113,14 @@ void UnregisterNetworkCallback()
104
113
105
114
class EssentialsNetworkCallback : ConnectivityManager . NetworkCallback
106
115
{
116
+ readonly Intent connectivityIntent ;
117
+
118
+ public EssentialsNetworkCallback ( )
119
+ {
120
+ connectivityIntent = new Intent ( ConnectivityChangedAction ) ;
121
+ connectivityIntent . SetPackage ( Application . Context . PackageName ) ;
122
+ }
123
+
107
124
public override void OnAvailable ( Network network ) =>
108
125
Application . Context . SendBroadcast ( connectivityIntent ) ;
109
126
@@ -159,15 +176,19 @@ public NetworkAccess NetworkAccess
159
176
var capabilities = manager . GetNetworkCapabilities ( network ) ;
160
177
161
178
if ( capabilities == null )
179
+ {
162
180
continue ;
181
+ }
163
182
164
183
#pragma warning disable CS0618 // Type or member is obsolete
165
184
#pragma warning disable CA1416 // Validate platform compatibility
166
185
#pragma warning disable CA1422 // Validate platform compatibility
167
186
var info = manager . GetNetworkInfo ( network ) ;
168
187
169
188
if ( info == null || ! info . IsAvailable )
189
+ {
170
190
continue ;
191
+ }
171
192
#pragma warning restore CS0618 // Type or member is obsolete
172
193
173
194
// Check to see if it has the internet capability
@@ -200,12 +221,18 @@ void ProcessAllNetworkInfo()
200
221
void ProcessNetworkInfo ( NetworkInfo info )
201
222
{
202
223
if ( info == null || ! info . IsAvailable )
224
+ {
203
225
return ;
226
+ }
204
227
205
228
if ( info . IsConnected )
229
+ {
206
230
currentAccess = IsBetterAccess ( currentAccess , NetworkAccess . Internet ) ;
231
+ }
207
232
else if ( info . IsConnectedOrConnecting )
233
+ {
208
234
currentAccess = IsBetterAccess ( currentAccess , NetworkAccess . ConstrainedInternet ) ;
235
+ }
209
236
#pragma warning restore CA1422 // Validate platform compatibility
210
237
#pragma warning restore CA1416 // Validate platform compatibility
211
238
#pragma warning restore CS0618 // Type or member is obsolete
@@ -250,15 +277,19 @@ public IEnumerable<ConnectionProfile> ConnectionProfiles
250
277
251
278
var p = ProcessNetworkInfo ( info ) ;
252
279
if ( p . HasValue )
280
+ {
253
281
yield return p . Value ;
282
+ }
254
283
}
255
284
256
285
#pragma warning disable CS0618 // Type or member is obsolete
257
286
static ConnectionProfile ? ProcessNetworkInfo ( NetworkInfo info )
258
287
{
259
288
260
289
if ( info == null || ! info . IsAvailable || ! info . IsConnectedOrConnecting )
290
+ {
261
291
return null ;
292
+ }
262
293
263
294
264
295
return GetConnectionType ( info . Type , info . TypeName ) ;
@@ -289,22 +320,34 @@ internal static ConnectionProfile GetConnectionType(ConnectivityType connectivit
289
320
return ConnectionProfile . Unknown ;
290
321
default :
291
322
if ( string . IsNullOrWhiteSpace ( typeName ) )
323
+ {
292
324
return ConnectionProfile . Unknown ;
325
+ }
293
326
294
327
if ( typeName . Contains ( "mobile" , StringComparison . OrdinalIgnoreCase ) )
328
+ {
295
329
return ConnectionProfile . Cellular ;
330
+ }
296
331
297
332
if ( typeName . Contains ( "wimax" , StringComparison . OrdinalIgnoreCase ) )
333
+ {
298
334
return ConnectionProfile . Cellular ;
335
+ }
299
336
300
337
if ( typeName . Contains ( "wifi" , StringComparison . OrdinalIgnoreCase ) )
338
+ {
301
339
return ConnectionProfile . WiFi ;
340
+ }
302
341
303
342
if ( typeName . Contains ( "ethernet" , StringComparison . OrdinalIgnoreCase ) )
343
+ {
304
344
return ConnectionProfile . Ethernet ;
345
+ }
305
346
306
347
if ( typeName . Contains ( "bluetooth" , StringComparison . OrdinalIgnoreCase ) )
348
+ {
307
349
return ConnectionProfile . Bluetooth ;
350
+ }
308
351
309
352
return ConnectionProfile . Unknown ;
310
353
}
@@ -335,7 +378,9 @@ public override async void OnReceive(Context context, Intent intent)
335
378
#pragma warning disable CS0618 // Type or member is obsolete
336
379
if ( intent . Action != ConnectivityManager . ConnectivityAction && intent . Action != ConnectivityImplementation . ConnectivityChangedAction )
337
380
#pragma warning restore CS0618 // Type or member is obsolete
381
+ {
338
382
return ;
383
+ }
339
384
340
385
// await 1500ms to ensure that the the connection manager updates
341
386
await Task . Delay ( 1500 ) ;
0 commit comments