This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 505
GH-8: Add Connectivity API #6
Merged
Merged
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
92507c8
Add base connectivity
jamesmontemagno d99ed76
Merge branch 'master' into connectivity
jamesmontemagno cd1fd26
Update naming and put in Caboodle folder
jamesmontemagno 632105d
Add: NetworkAccess and Profiles instead of simple bools
jamesmontemagno c941871
Merge branch 'master' into connectivity
jamesmontemagno 75fc42a
update sample and test device runner
jamesmontemagno 9b1a64e
Merge branch 'master' into connectivity
jamesmontemagno 761eab8
Merge branch 'master' into connectivity
jamesmontemagno 1803e29
Implement Connection profiles and connection change events.
jamesmontemagno 2c15703
Additional cleanup for compile
jamesmontemagno 1c654da
Merge branch 'master' into connectivity
jamesmontemagno bc8febc
Add tests and samples
jamesmontemagno 0be0f67
Fix saving list to not be reference type.
jamesmontemagno fb6d465
Android dont' show non-connected profiles
jamesmontemagno bc949d4
Refactor some code
jamesmontemagno 532dffa
Merge branch 'master' into connectivity
jamesmontemagno 995f1e7
Update exception names for compile
jamesmontemagno 77278dd
Add connectivity documentation
jamesmontemagno 81df608
Add docs
jamesmontemagno c6eb47a
Merge branch 'master' into connectivity
jamesmontemagno 6ba6553
Fix build
jamesmontemagno 38c2332
Delete unit test 1 from build
jamesmontemagno 17a0045
Fix tests! woops
jamesmontemagno 0d3b97a
Merge branch 'master' into connectivity
jamesmontemagno 55f531b
Merge branch 'master' into connectivity
jamesmontemagno 77ed605
Merge branch 'master' into connectivity
jamesmontemagno b20996c
Merge branch 'master' into connectivity
jamesmontemagno fdb1984
Cleanup connectivity API checks and add remarks for exceptions on And…
jamesmontemagno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
try | ||
{ | ||
var currentAccess = NetworkAccess.None; | ||
var manager = Platform.ConnectivityManager; | ||
|
||
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the new platform.hasapi here? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Platform.HasApiLevel There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)