Skip to content

Commit

Permalink
Merge pull request #1682 from Microsoft/HD-NetworkHelper
Browse files Browse the repository at this point in the history
NetworkHelper updates to fix crashes logged
  • Loading branch information
hermitdave authored Dec 6, 2017
2 parents fa00d94 + a00452a commit a56391f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
39 changes: 26 additions & 13 deletions Microsoft.Toolkit.Uwp.Connectivity/Network/ConnectionInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,20 @@ public class ConnectionInformation
/// Updates the current object based on profile passed.
/// </summary>
/// <param name="profile">instance of <see cref="ConnectionProfile"/></param>
public virtual void UpdateConnectionInformation(ConnectionProfile profile)
public void UpdateConnectionInformation(ConnectionProfile profile)
{
networkNames.Clear();

if (profile == null)
{
ConnectionType = ConnectionType.Unknown;
ConnectivityLevel = NetworkConnectivityLevel.None;
IsInternetAvailable = false;
ConnectionCost = null;
SignalStrength = null;
Reset();

return;
}

switch (profile.NetworkAdapter.IanaInterfaceType)
networkNames.Clear();

uint ianaInterfaceType = profile.NetworkAdapter?.IanaInterfaceType ?? 0;

switch (ianaInterfaceType)
{
case 6:
ConnectionType = ConnectionType.Ethernet;
Expand All @@ -65,16 +63,14 @@ public virtual void UpdateConnectionInformation(ConnectionProfile profile)
break;
}

ConnectivityLevel = profile.GetNetworkConnectivityLevel();
ConnectionCost = profile.GetConnectionCost();
SignalStrength = profile.GetSignalBars();

var names = profile.GetNetworkNames();
if (names?.Count > 0)
{
networkNames.AddRange(names);
}

ConnectivityLevel = profile.GetNetworkConnectivityLevel();

switch (ConnectivityLevel)
{
case NetworkConnectivityLevel.None:
Expand All @@ -86,6 +82,23 @@ public virtual void UpdateConnectionInformation(ConnectionProfile profile)
IsInternetAvailable = true;
break;
}

ConnectionCost = profile.GetConnectionCost();
SignalStrength = profile.GetSignalBars();
}

/// <summary>
/// Resets the current object to default values.
/// </summary>
internal void Reset()
{
networkNames.Clear();

ConnectionType = ConnectionType.Unknown;
ConnectivityLevel = NetworkConnectivityLevel.None;
IsInternetAvailable = false;
ConnectionCost = null;
SignalStrength = null;
}

/// <summary>
Expand Down
28 changes: 17 additions & 11 deletions Microsoft.Toolkit.Uwp.Connectivity/Network/NetworkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected NetworkHelper()
{
ConnectionInformation = new ConnectionInformation();

OnNetworkStatusChanged(null);
UpdateConnectionInformation();

NetworkInformation.NetworkStatusChanged += OnNetworkStatusChanged;
}
Expand All @@ -60,20 +60,26 @@ protected NetworkHelper()
NetworkInformation.NetworkStatusChanged -= OnNetworkStatusChanged;
}

private void OnNetworkStatusChanged(object sender)
private void UpdateConnectionInformation()
{
ConnectionProfile profile = null;
try
{
profile = NetworkInformation.GetInternetConnectionProfile();
}
catch
lock (ConnectionInformation)
{
}
try
{
ConnectionInformation.UpdateConnectionInformation(NetworkInformation.GetInternetConnectionProfile());

ConnectionInformation.UpdateConnectionInformation(profile);
NetworkChanged?.Invoke(this, EventArgs.Empty);
}
catch
{
ConnectionInformation.Reset();
}
}
}

NetworkChanged?.Invoke(this, EventArgs.Empty);
private void OnNetworkStatusChanged(object sender)
{
UpdateConnectionInformation();
}
}
}

0 comments on commit a56391f

Please sign in to comment.