From 77751ab9ae59bd2adabe5fe21de4c25ca1890ec4 Mon Sep 17 00:00:00 2001 From: pkar Date: Mon, 29 Mar 2021 14:05:34 +0200 Subject: [PATCH] chore: ConnectionProfile de-obsoleting --- .../Connectivity/ConnectionProfile.Android.cs | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Uno.UWP/Networking/Connectivity/ConnectionProfile.Android.cs b/src/Uno.UWP/Networking/Connectivity/ConnectionProfile.Android.cs index 9c7a5a4cf049..001e2910c93a 100644 --- a/src/Uno.UWP/Networking/Connectivity/ConnectionProfile.Android.cs +++ b/src/Uno.UWP/Networking/Connectivity/ConnectionProfile.Android.cs @@ -30,21 +30,45 @@ private ConnectionProfile() { NetworkInformation.VerifyNetworkStateAccess(); _connectivityManager = (AndroidConnectivityManager)ContextHelper.Current.GetSystemService(Context.ConnectivityService); -#pragma warning disable CS0618 // Type or member is obsolete - NetworkInfo info = _connectivityManager.ActiveNetworkInfo; - if (info?.IsConnected == true) + + if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.LollipopMr1) { - IsWwanConnectionProfile = IsConnectionWwan(info.Type); - IsWlanConnectionProfile = IsConnectionWlan(info.Type); + var activeNetwork = _connectivityManager.ActiveNetwork; + if (activeNetwork != null) + { + var netCaps = _connectivityManager.GetNetworkCapabilities(activeNetwork); + if (netCaps != null) + { + IsWlanConnectionProfile = netCaps.HasTransport(Android.Net.TransportType.Wifi); + IsWwanConnectionProfile = netCaps.HasTransport(Android.Net.TransportType.Cellular); + } + } } + else + { +#pragma warning disable CS0618 // Type or member is obsolete + NetworkInfo info = _connectivityManager.ActiveNetworkInfo; + if (info?.IsConnected == true) + { + IsWwanConnectionProfile = IsConnectionWwan(info.Type); + IsWlanConnectionProfile = IsConnectionWlan(info.Type); + } #pragma warning restore CS0618 // Type or member is obsolete + } } - public ConnectionCost GetConnectionCost() => - new ConnectionCost( + public ConnectionCost GetConnectionCost() + { + if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.JellyBean) + { // below API 16, we don't have IsActiveNetworkMetered method + return new ConnectionCost(NetworkCostType.Unknown); + } + + return new ConnectionCost( _connectivityManager.IsActiveNetworkMetered ? NetworkCostType.Fixed : // we currently don't make distinction between variable and fixed metered connection on Android NetworkCostType.Unrestricted); + } /// /// Code based on Xamarin.Essentials implementation with some modifications.