Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 98 additions & 34 deletions src/Essentials/src/Connectivity/Connectivity.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,9 @@ public NetworkAccess NetworkAccess
var currentAccess = NetworkAccess.None;
var manager = ConnectivityManager;

#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1422 // Validate platform compatibility
var networks = manager.GetAllNetworks();
#pragma warning restore CA1422 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CS0618 // Type or member is obsolete

// some devices running 21 and 22 only use the older api.
if (networks.Length == 0 && !OperatingSystem.IsAndroidVersionAtLeast(23))
Expand All @@ -177,17 +173,6 @@ public NetworkAccess NetworkAccess
continue;
}

#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1422 // Validate platform compatibility
var info = manager.GetNetworkInfo(network);

if (info == null || !info.IsAvailable)
{
continue;
}
#pragma warning restore CS0618 // Type or member is obsolete

// Check to see if it has the internet capability
if (!capabilities.HasCapability(NetCapability.Internet))
{
Expand All @@ -196,7 +181,8 @@ public NetworkAccess NetworkAccess
continue;
}

ProcessNetworkInfo(info);
// Use modern NetworkCapabilities instead of obsolete NetworkInfo
ProcessNetworkCapabilities(capabilities);
}
catch
{
Expand All @@ -206,11 +192,22 @@ public NetworkAccess NetworkAccess

void ProcessAllNetworkInfo()
{
// For devices that don't return networks from GetAllNetworks()
// (some API 21-22 devices), fall back to active network only
try
{
#pragma warning disable CS0618 // Type or member is obsolete
foreach (var info in manager.GetAllNetworkInfo())
var activeInfo = manager.ActiveNetworkInfo;
if (activeInfo != null)
{
ProcessNetworkInfo(activeInfo);
}
Comment thread
kubaflo marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behavioral change: The old code used GetAllNetworkInfo() which returned info for every network interface. ActiveNetworkInfo only returns the currently active/default network.

On API 21–22 devices with multiple network connections, the old code would evaluate all of them via IsBetterAccess and report the highest NetworkAccess level. The new code only evaluates the active one, which could report a lower access level if a secondary network had better connectivity.

The comment acknowledges the trade-off, but consider noting explicitly that this is a behavioral change from the original implementation, not just a different API surface.

#pragma warning restore CS0618 // Type or member is obsolete
}
catch
{
ProcessNetworkInfo(info);
// If even the active network fails, assume no connectivity
currentAccess = NetworkAccess.None;
}
}

Expand All @@ -230,9 +227,42 @@ void ProcessNetworkInfo(NetworkInfo info)
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale pragma restores: CA1422 and CA1416 are restored here but were never #pragma warning disable'd in this scope. These are leftovers from the old code's nesting structure. Only CS0618 needs to be restored here.

Suggested change
{
}
#pragma warning restore CS0618 // Type or member is obsolete

currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet);
}
}
#pragma warning restore CA1422 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CS0618 // Type or member is obsolete

void ProcessNetworkCapabilities(NetworkCapabilities capabilities)
{
if (capabilities == null)
{
return;
}

// Modern approach: Use NetworkCapabilities instead of NetworkInfo
// Check for validated internet connectivity (similar to info.IsConnected)
Comment thread
kubaflo marked this conversation as resolved.
Outdated
if (capabilities.HasCapability(NetCapability.Internet))
{
// For API 23+, also check if network is validated for better accuracy
// For API 21-22, just having Internet capability is sufficient
if (OperatingSystem.IsAndroidVersionAtLeast(23))
{
if (capabilities.HasCapability(NetCapability.Validated))
{
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet);
}
else
{
// Has internet capability but not validated (similar to IsConnectedOrConnecting)
Comment thread
kubaflo marked this conversation as resolved.
Outdated
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet);
}
}
else
{
// For API 21-22, assume internet capability means connected
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet);
}
}
Comment thread
kubaflo marked this conversation as resolved.
Outdated
}

return currentAccess;
Expand All @@ -253,50 +283,84 @@ public IEnumerable<ConnectionProfile> ConnectionProfiles
Permissions.EnsureDeclared<Permissions.NetworkState>();

var manager = ConnectivityManager;
#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1422 // Validate platform compatibility
var networks = manager.GetAllNetworks();
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CA1422 // Validate platform compatibility
foreach (var network in networks)
{
#pragma warning disable CS0618 // Type or member is obsolete
NetworkInfo info = null;
NetworkCapabilities capabilities = null;
try
{
info = manager.GetNetworkInfo(network);
capabilities = manager.GetNetworkCapabilities(network);
}
catch
{
// there is a possibility, but don't worry about it
}
#pragma warning restore CS0618 // Type or member is obsolete

var p = ProcessNetworkInfo(info);
var p = ProcessNetworkCapabilities(capabilities);
if (p.HasValue)
{
yield return p.Value;
}
}

#pragma warning disable CS0618 // Type or member is obsolete
static ConnectionProfile? ProcessNetworkInfo(NetworkInfo info)
static ConnectionProfile? ProcessNetworkCapabilities(NetworkCapabilities capabilities)
{

if (info == null || !info.IsAvailable || !info.IsConnectedOrConnecting)
if (capabilities == null)
{
return null;
}

// Check if network has internet or local connectivity
Comment thread
kubaflo marked this conversation as resolved.
Outdated
if (!capabilities.HasCapability(NetCapability.Internet))
{
return null; // Skip networks without internet capability
}

return GetConnectionType(info.Type, info.TypeName);
return GetConnectionTypeFromCapabilities(capabilities);
}
#pragma warning restore CA1422 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CS0618 // Type or member is obsolete
}
}

internal static ConnectionProfile GetConnectionTypeFromCapabilities(NetworkCapabilities capabilities)
{
if (capabilities == null)
{
return ConnectionProfile.Unknown;
}

// Use modern NetworkCapabilities.HasTransport() instead of obsolete NetworkInfo.Type
if (capabilities.HasTransport(TransportType.Wifi))
{
return ConnectionProfile.WiFi;
}

if (capabilities.HasTransport(TransportType.Cellular))
{
return ConnectionProfile.Cellular;
}

if (capabilities.HasTransport(TransportType.Ethernet))
{
return ConnectionProfile.Ethernet;
}

if (capabilities.HasTransport(TransportType.Bluetooth))
{
return ConnectionProfile.Bluetooth;
}

// Additional transport types for completeness
if (capabilities.HasTransport(TransportType.Vpn))
{
// VPN typically runs over another transport, but classify as Unknown for now
return ConnectionProfile.Unknown;
}
Comment thread
kubaflo marked this conversation as resolved.
Outdated

return ConnectionProfile.Unknown;
}

internal static ConnectionProfile GetConnectionType(ConnectivityType connectivityType, string typeName)
{
switch (connectivityType)
Expand Down
Loading