Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

GH-8: Add Connectivity API #6

Merged
merged 28 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
92507c8
Add base connectivity
jamesmontemagno Feb 23, 2018
d99ed76
Merge branch 'master' into connectivity
jamesmontemagno Feb 24, 2018
cd1fd26
Update naming and put in Caboodle folder
jamesmontemagno Feb 24, 2018
632105d
Add: NetworkAccess and Profiles instead of simple bools
jamesmontemagno Feb 24, 2018
c941871
Merge branch 'master' into connectivity
jamesmontemagno Mar 1, 2018
75fc42a
update sample and test device runner
jamesmontemagno Mar 1, 2018
9b1a64e
Merge branch 'master' into connectivity
jamesmontemagno Mar 1, 2018
761eab8
Merge branch 'master' into connectivity
jamesmontemagno Mar 14, 2018
1803e29
Implement Connection profiles and connection change events.
jamesmontemagno Mar 14, 2018
2c15703
Additional cleanup for compile
jamesmontemagno Mar 14, 2018
1c654da
Merge branch 'master' into connectivity
jamesmontemagno Mar 15, 2018
bc8febc
Add tests and samples
jamesmontemagno Mar 15, 2018
0be0f67
Fix saving list to not be reference type.
jamesmontemagno Mar 15, 2018
fb6d465
Android dont' show non-connected profiles
jamesmontemagno Mar 15, 2018
bc949d4
Refactor some code
jamesmontemagno Mar 15, 2018
532dffa
Merge branch 'master' into connectivity
jamesmontemagno Mar 15, 2018
995f1e7
Update exception names for compile
jamesmontemagno Mar 15, 2018
77278dd
Add connectivity documentation
jamesmontemagno Mar 15, 2018
81df608
Add docs
jamesmontemagno Mar 15, 2018
c6eb47a
Merge branch 'master' into connectivity
jamesmontemagno Mar 15, 2018
6ba6553
Fix build
jamesmontemagno Mar 15, 2018
38c2332
Delete unit test 1 from build
jamesmontemagno Mar 15, 2018
17a0045
Fix tests! woops
jamesmontemagno Mar 15, 2018
0d3b97a
Merge branch 'master' into connectivity
jamesmontemagno Mar 17, 2018
55f531b
Merge branch 'master' into connectivity
jamesmontemagno Mar 20, 2018
77ed605
Merge branch 'master' into connectivity
jamesmontemagno Mar 26, 2018
b20996c
Merge branch 'master' into connectivity
jamesmontemagno Mar 26, 2018
fdb1984
Cleanup connectivity API checks and add remarks for exceptions on And…
jamesmontemagno Mar 26, 2018
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
24 changes: 24 additions & 0 deletions Caboodle.Tests/Connectivity_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Caboodle;
using Xunit;

namespace Caboodle.Tests
{
public class Connectivity_Tests
{
[Fact]
public void Network_Access_On_NetStandard() =>
Assert.Throws<NotImplementedInReferenceAssemblyException>(() => Connectivity.NetworkAccess);

[Fact]
public void Profiles_On_NetStandard() =>
Assert.Throws<NotImplementedInReferenceAssemblyException>(() => Connectivity.Profiles);

[Fact]
public void Connectivity_Changed_Event_On_NetStandard() =>
Assert.Throws<NotImplementedInReferenceAssemblyException>(() => Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged);

void Connectivity_ConnectivityChanged(ConnectivityChangedEventArgs e)
{
}
}
}
4 changes: 4 additions & 0 deletions Caboodle/Caboodle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<Compile Include="**\*.netstandard.cs" />
<Compile Include="**\*.netstandard.*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('uap10.0')) ">
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.0.6" />
<Compile Include="**\*.uwp.cs" />
<Compile Include="**\*.uwp.*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('MonoAndroid')) ">
<PackageReference Include="Xamarin.Android.Support.CustomTabs" Version="26.1.0.1" />
<Compile Include="**\*.android.cs" />
<Compile Include="**\*.android.*.cs" />
</ItemGroup>
<ItemGroup Condition=" $(TargetFramework.StartsWith('Xamarin.iOS')) ">
<Compile Include="**\*.ios.cs" />
<Compile Include="**\*.ios.*.cs" />
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
<Import Project="$(MSBuildThisFileDirectory)mdoc.targets" />
Expand Down
9 changes: 3 additions & 6 deletions Caboodle/Clipboard/Clipboard.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ namespace Microsoft.Caboodle
{
public static partial class Clipboard
{
static ClipboardManager ClipboardManager
=> (ClipboardManager)Application.Context.GetSystemService(Context.ClipboardService);

public static void SetText(string text)
=> ClipboardManager.PrimaryClip = ClipData.NewPlainText("Text", text);
=> Platform.ClipboardManager.PrimaryClip = ClipData.NewPlainText("Text", text);

public static bool HasText
=> ClipboardManager.HasPrimaryClip;
=> Platform.ClipboardManager.HasPrimaryClip;

public static Task<string> GetTextAsync()
=> Task.FromResult(ClipboardManager.PrimaryClip?.GetItemAt(0)?.Text);
=> Task.FromResult(Platform.ClipboardManager.PrimaryClip?.GetItemAt(0)?.Text);
}
}
226 changes: 226 additions & 0 deletions Caboodle/Connectivity/Connectivity.android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Android;
using Android.App;
using Android.Content;
using Android.Net;
using Android.Net.Wifi;
using Android.OS;

namespace Microsoft.Caboodle
{
public partial class Connectivity
{
static ConnectivityBroadcastReceiver conectivityReceiver;
static bool hasPermission;

static void ValidatePermission()
{
if (hasPermission)
return;

var permission = Manifest.Permission.AccessNetworkState;
if (!Platform.HasPermissionInManifest(permission))
throw new PermissionException(permission);

hasPermission = true;
}

static void StartListeners()
{
ValidatePermission();
conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);
Platform.CurrentContext.RegisterReceiver(conectivityReceiver, new IntentFilter(ConnectivityManager.ConnectivityAction));
}

static void StopListeners()
{
Platform.CurrentContext.UnregisterReceiver(conectivityReceiver);
conectivityReceiver?.Dispose();
conectivityReceiver = null;
}

static NetworkAccess IsBetterAccess(NetworkAccess currentAccess, NetworkAccess newAccess) =>
newAccess > currentAccess ? newAccess : currentAccess;

public static NetworkAccess NetworkAccess
{
get
{
ValidatePermission();
Copy link
Member

Choose a reason for hiding this comment

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

Are we ok with potentially throwing here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed that we are only because it is a manifest check :)

try
{
var currentAccess = NetworkAccess.None;
var manager = Platform.ConnectivityManager;

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Copy link
Member

Choose a reason for hiding this comment

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

Use the new platform.hasapi here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated!

{
foreach (var network in manager.GetAllNetworks())
{
try
{
var capabilities = manager.GetNetworkCapabilities(network);

if (capabilities == null)
continue;

// Check to see if it has the internet capability
if (!capabilities.HasCapability(NetCapability.Internet))
{
// Doesn't have internet, but local is possible
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Local);
continue;
}

var info = manager.GetNetworkInfo(network);

ProcessNetworkInfo(info);
}
catch
{
// there is a possibility, but don't worry
}
}
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
{
ProcessNetworkInfo(info);
}
}

void ProcessNetworkInfo(NetworkInfo info)
{
if (info == null || !info.IsAvailable)
return;

if (info.IsConnected)
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.Internet);
else if (info.IsConnectedOrConnecting)
currentAccess = IsBetterAccess(currentAccess, NetworkAccess.ConstrainedInternet);
}

return currentAccess;
}
catch (Exception e)
{
Console.WriteLine("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
return NetworkAccess.Unknown;
}
}
}

public static IEnumerable<ConnectionProfile> Profiles
{
get
{
ValidatePermission();
var manager = Platform.ConnectivityManager;
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
Copy link
Member

Choose a reason for hiding this comment

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

Platform.HasApiLevel

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated!

{
foreach (var network in manager.GetAllNetworks())
{
NetworkInfo info = null;
try
{
info = manager.GetNetworkInfo(network);
}
catch
{
// there is a possibility, but don't worry about it
}

var p = ProcessNetworkInfo(info);
if (p.HasValue)
yield return p.Value;
}
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
{
var p = ProcessNetworkInfo(info);
if (p.HasValue)
yield return p.Value;
}
}

ConnectionProfile? ProcessNetworkInfo(NetworkInfo info)
{
if (info == null || !info.IsAvailable || !info.IsConnectedOrConnecting)
return null;

return GetConnectionType(info.Type, info.TypeName);
}
}
}

internal static ConnectionProfile GetConnectionType(ConnectivityType connectivityType, string typeName)
{
switch (connectivityType)
{
case ConnectivityType.Ethernet:
return ConnectionProfile.Ethernet;
case ConnectivityType.Wimax:
return ConnectionProfile.WiMAX;
case ConnectivityType.Wifi:
return ConnectionProfile.WiFi;
case ConnectivityType.Bluetooth:
return ConnectionProfile.Bluetooth;
case ConnectivityType.Mobile:
case ConnectivityType.MobileDun:
case ConnectivityType.MobileHipri:
case ConnectivityType.MobileMms:
return ConnectionProfile.Cellular;
case ConnectivityType.Dummy:
return ConnectionProfile.Other;
default:
if (string.IsNullOrWhiteSpace(typeName))
return ConnectionProfile.Other;

var typeNameLower = typeName.ToLowerInvariant();
if (typeNameLower.Contains("mobile"))
return ConnectionProfile.Cellular;

if (typeNameLower.Contains("wifi"))
return ConnectionProfile.WiFi;

if (typeNameLower.Contains("wimax"))
return ConnectionProfile.WiMAX;

if (typeNameLower.Contains("ethernet"))
return ConnectionProfile.Ethernet;

if (typeNameLower.Contains("bluetooth"))
return ConnectionProfile.Bluetooth;

return ConnectionProfile.Other;
}
}
}

class ConnectivityBroadcastReceiver : BroadcastReceiver
{
Action onChanged;

public ConnectivityBroadcastReceiver(Action onChanged) =>
this.onChanged = onChanged;

public override async void OnReceive(Context context, Intent intent)
{
if (intent.Action != ConnectivityManager.ConnectivityAction)
return;

// await 500ms to ensure that the the connection manager updates
await Task.Delay(500);
onChanged?.Invoke();
}
}
}
61 changes: 61 additions & 0 deletions Caboodle/Connectivity/Connectivity.ios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.Caboodle
{
public static partial class Connectivity
{
static void StartListeners() =>
Reachability.ReachabilityChanged += ReachabilityChanged;

static async void ReachabilityChanged(object sender, EventArgs e)
{
await Task.Delay(100);
OnConnectivityChanged();
}

static void StopListeners() =>
Reachability.ReachabilityChanged -= ReachabilityChanged;

public static NetworkAccess NetworkAccess
{
get
{
var remoteHostStatus = Reachability.RemoteHostStatus();
var internetStatus = Reachability.InternetConnectionStatus();

var isConnected = (internetStatus == NetworkStatus.ReachableViaCarrierDataNetwork ||
internetStatus == NetworkStatus.ReachableViaWiFiNetwork) ||
(remoteHostStatus == NetworkStatus.ReachableViaCarrierDataNetwork ||
remoteHostStatus == NetworkStatus.ReachableViaWiFiNetwork);

return isConnected ? NetworkAccess.Internet : NetworkAccess.None;
}
}

public static IEnumerable<ConnectionProfile> Profiles
{
get
{
var statuses = Reachability.GetActiveConnectionType();
foreach (var status in statuses)
{
switch (status)
{
case NetworkStatus.ReachableViaCarrierDataNetwork:
yield return ConnectionProfile.Cellular;
break;
case NetworkStatus.ReachableViaWiFiNetwork:
yield return ConnectionProfile.WiFi;
break;
default:
yield return ConnectionProfile.Other;
break;
}
}
}
}
}
}
Loading