From e86a19c5c19e829e19b70460a5d8971e80b59083 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 10:11:36 +0000 Subject: [PATCH 1/4] Initial plan From 6d1ecb474d976cdf6eb979f927e57b8d6891b0fd Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 1 Jul 2025 10:27:39 +0000 Subject: [PATCH 2/4] Update Android Connectivity implementation to use modern APIs Co-authored-by: jfversluis <939291+jfversluis@users.noreply.github.com> --- .../src/Connectivity/Connectivity.android.cs | 132 +++++++++++++----- 1 file changed, 98 insertions(+), 34 deletions(-) diff --git a/src/Essentials/src/Connectivity/Connectivity.android.cs b/src/Essentials/src/Connectivity/Connectivity.android.cs index b2e936bda75b..986efde6279b 100644 --- a/src/Essentials/src/Connectivity/Connectivity.android.cs +++ b/src/Essentials/src/Connectivity/Connectivity.android.cs @@ -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)) @@ -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)) { @@ -196,7 +181,8 @@ public NetworkAccess NetworkAccess continue; } - ProcessNetworkInfo(info); + // Use modern NetworkCapabilities instead of obsolete NetworkInfo + ProcessNetworkCapabilities(capabilities); } catch { @@ -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); + } #pragma warning restore CS0618 // Type or member is obsolete + } + catch { - ProcessNetworkInfo(info); + // If even the active network fails, assume no connectivity + currentAccess = NetworkAccess.None; } } @@ -230,9 +227,42 @@ void ProcessNetworkInfo(NetworkInfo info) { 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) + 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) + currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet); + } + } + else + { + // For API 21-22, assume internet capability means connected + currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet); + } + } } return currentAccess; @@ -253,50 +283,84 @@ public IEnumerable ConnectionProfiles Permissions.EnsureDeclared(); 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 + 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; + } + + return ConnectionProfile.Unknown; + } + internal static ConnectionProfile GetConnectionType(ConnectivityType connectivityType, string typeName) { switch (connectivityType) From a7ffb94c7d649a12e51cd99d6a5315a18fb40c52 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Thu, 5 Mar 2026 18:49:41 +0100 Subject: [PATCH 3/4] Address review comments and improve Android Connectivity code - Remove redundant HasCapability(NetCapability.Internet) check in ProcessNetworkCapabilities; caller already verifies this before calling - Fix misleading comments: clarify NetCapability.Validated semantics (captive portal detection, not equivalent to IsConnectedOrConnecting) - Document behavioral change in ProcessAllNetworkInfo fallback (ActiveNetworkInfo vs GetAllNetworkInfo for API 21-22) - Document multi-transport ordering in GetConnectionTypeFromCapabilities (physical links prioritised over VPN overlay) - Remove redundant VPN-specific branch that already fell through to Unknown - Replace empty catch blocks with Debug.WriteLine for diagnosability Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/Connectivity/Connectivity.android.cs | 71 ++++++++----------- 1 file changed, 31 insertions(+), 40 deletions(-) diff --git a/src/Essentials/src/Connectivity/Connectivity.android.cs b/src/Essentials/src/Connectivity/Connectivity.android.cs index 986efde6279b..ecb8483b1c69 100644 --- a/src/Essentials/src/Connectivity/Connectivity.android.cs +++ b/src/Essentials/src/Connectivity/Connectivity.android.cs @@ -184,16 +184,19 @@ public NetworkAccess NetworkAccess // Use modern NetworkCapabilities instead of obsolete NetworkInfo ProcessNetworkCapabilities(capabilities); } - catch + catch (Exception ex) { - // there is a possibility, but don't worry + Debug.WriteLine("Failed to get network capabilities: {0}", ex); } } void ProcessAllNetworkInfo() { - // For devices that don't return networks from GetAllNetworks() - // (some API 21-22 devices), fall back to active network only + // Fallback for API 21-22 devices where GetAllNetworks() returns empty. + // NOTE: The original code used GetAllNetworkInfo() which enumerated all + // network interfaces; ActiveNetworkInfo returns only the current default + // network. On these older API levels multi-network support is limited, + // so this is an acceptable trade-off. try { #pragma warning disable CS0618 // Type or member is obsolete @@ -204,9 +207,9 @@ void ProcessAllNetworkInfo() } #pragma warning restore CS0618 // Type or member is obsolete } - catch + catch (Exception ex) { - // If even the active network fails, assume no connectivity + Debug.WriteLine("Failed to query active network info: {0}", ex); currentAccess = NetworkAccess.None; } } @@ -232,37 +235,30 @@ void ProcessNetworkInfo(NetworkInfo info) #pragma warning restore CA1416 // Validate platform compatibility #pragma warning restore CS0618 // Type or member is obsolete + // Caller guarantees capabilities is non-null and has NetCapability.Internet. void ProcessNetworkCapabilities(NetworkCapabilities capabilities) { - if (capabilities == null) + // NetCapability.Validated (API 23+) means the system has verified the + // network can actually reach the internet (passes Google's connectivity check). + // Without it, the network may be behind a captive portal or otherwise unusable. + if (OperatingSystem.IsAndroidVersionAtLeast(23)) { - return; - } - - // Modern approach: Use NetworkCapabilities instead of NetworkInfo - // Check for validated internet connectivity (similar to info.IsConnected) - 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)) { - if (capabilities.HasCapability(NetCapability.Validated)) - { - currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet); - } - else - { - // Has internet capability but not validated (similar to IsConnectedOrConnecting) - currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet); - } + currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet); } else { - // For API 21-22, assume internet capability means connected - currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet); + // Has internet capability but not validated (e.g. captive portal) + currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet); } } + else + { + // NetCapability.Validated is unavailable on API 21-22; + // treat Internet capability alone as a connected state. + currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet); + } } return currentAccess; @@ -293,9 +289,9 @@ public IEnumerable ConnectionProfiles { capabilities = manager.GetNetworkCapabilities(network); } - catch + catch (Exception ex) { - // there is a possibility, but don't worry about it + Debug.WriteLine("Failed to get network capabilities for profile: {0}", ex); } var p = ProcessNetworkCapabilities(capabilities); @@ -312,10 +308,10 @@ public IEnumerable ConnectionProfiles return null; } - // Check if network has internet or local connectivity + // Only include networks that declare internet capability if (!capabilities.HasCapability(NetCapability.Internet)) { - return null; // Skip networks without internet capability + return null; } return GetConnectionTypeFromCapabilities(capabilities); @@ -330,7 +326,9 @@ internal static ConnectionProfile GetConnectionTypeFromCapabilities(NetworkCapab return ConnectionProfile.Unknown; } - // Use modern NetworkCapabilities.HasTransport() instead of obsolete NetworkInfo.Type + // A network may advertise multiple transports (e.g. WiFi + VPN). + // We return the first "underlying" transport match, prioritising the + // physical link type over overlay transports like VPN. if (capabilities.HasTransport(TransportType.Wifi)) { return ConnectionProfile.WiFi; @@ -351,13 +349,6 @@ internal static ConnectionProfile GetConnectionTypeFromCapabilities(NetworkCapab 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; - } - return ConnectionProfile.Unknown; } From 7ab4a35bb5557da8b58fee7cb877106bf8745797 Mon Sep 17 00:00:00 2001 From: kubaflo Date: Sat, 7 Mar 2026 00:22:29 +0100 Subject: [PATCH 4/4] Apply review suggestions: fix stale pragmas, clarify behavioral change - Remove stale #pragma warning restore for CA1422 and CA1416 that were never disabled in ProcessNetworkInfo scope - Clarify comment in ProcessAllNetworkInfo to explicitly document the behavioral change from GetAllNetworkInfo to ActiveNetworkInfo Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../src/Connectivity/Connectivity.android.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Essentials/src/Connectivity/Connectivity.android.cs b/src/Essentials/src/Connectivity/Connectivity.android.cs index ecb8483b1c69..6112f7770fef 100644 --- a/src/Essentials/src/Connectivity/Connectivity.android.cs +++ b/src/Essentials/src/Connectivity/Connectivity.android.cs @@ -193,9 +193,11 @@ public NetworkAccess NetworkAccess void ProcessAllNetworkInfo() { // Fallback for API 21-22 devices where GetAllNetworks() returns empty. - // NOTE: The original code used GetAllNetworkInfo() which enumerated all - // network interfaces; ActiveNetworkInfo returns only the current default - // network. On these older API levels multi-network support is limited, + // BEHAVIORAL CHANGE: The original code used GetAllNetworkInfo() which + // enumerated all network interfaces and picked the best access level. + // ActiveNetworkInfo returns only the current default network, so a + // better-connected secondary interface will no longer be considered. + // On these older API levels multi-network support is limited, // so this is an acceptable trade-off. try { @@ -231,8 +233,6 @@ void ProcessNetworkInfo(NetworkInfo info) 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 // Caller guarantees capabilities is non-null and has NetCapability.Internet.