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

Commit

Permalink
Merge pull request #1568 from xamarin/gh-512-spike
Browse files Browse the repository at this point in the history
Fixes #512 - Implement the new ConnectivityManager.NetworkCallback for N+
  • Loading branch information
jfversluis authored Jan 18, 2022
2 parents c6bc848 + 7dfb78f commit 20d627e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
74 changes: 71 additions & 3 deletions Xamarin.Essentials/Connectivity/Connectivity.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,46 @@ namespace Xamarin.Essentials
public partial class Connectivity
{
static ConnectivityBroadcastReceiver conectivityReceiver;
static Intent connectivityIntent = new Intent(Platform.EssentialsConnectivityChanged);
static EssentialsNetworkCallback networkCallback;

static void StartListeners()
{
Permissions.EnsureDeclared<Permissions.NetworkState>();

conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);
var filter = new IntentFilter();

if (Platform.HasApiLevelN)
{
RegisterNetworkCallback();
filter.AddAction(Platform.EssentialsConnectivityChanged);
}
else
{
#pragma warning disable CS0618 // Type or member is obsolete
Platform.AppContext.RegisterReceiver(conectivityReceiver, new IntentFilter(ConnectivityManager.ConnectivityAction));
filter.AddAction(ConnectivityManager.ConnectivityAction);
#pragma warning restore CS0618 // Type or member is obsolete
}

conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);

Platform.AppContext.RegisterReceiver(conectivityReceiver, filter);
}

static void StopListeners()
{
if (conectivityReceiver == null)
return;

try
{
UnregisterNetworkCallback();
}
catch
{
Debug.WriteLine("Connectivity receiver already unregistered. Disposing of it.");
}

try
{
Platform.AppContext.UnregisterReceiver(conectivityReceiver);
Expand All @@ -39,6 +63,50 @@ static void StopListeners()
conectivityReceiver = null;
}

static void RegisterNetworkCallback()
{
if (!Platform.HasApiLevelN)
return;

var manager = Platform.ConnectivityManager;
if (manager == null)
return;

var request = new NetworkRequest.Builder().Build();
networkCallback = new EssentialsNetworkCallback();
manager.RegisterNetworkCallback(request, networkCallback);
}

static void UnregisterNetworkCallback()
{
if (!Platform.HasApiLevelN)
return;

var manager = Platform.ConnectivityManager;
if (manager == null || networkCallback == null)
return;

manager.UnregisterNetworkCallback(networkCallback);

networkCallback?.Dispose();
networkCallback = null;
}

class EssentialsNetworkCallback : ConnectivityManager.NetworkCallback
{
public override void OnAvailable(Network network) => Platform.AppContext.SendBroadcast(connectivityIntent);

public override void OnLost(Network network) => Platform.AppContext.SendBroadcast(connectivityIntent);

public override void OnCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) => Platform.AppContext.SendBroadcast(connectivityIntent);

public override void OnUnavailable() => Platform.AppContext.SendBroadcast(connectivityIntent);

public override void OnLinkPropertiesChanged(Network network, LinkProperties linkProperties) => Platform.AppContext.SendBroadcast(connectivityIntent);

public override void OnLosing(Network network, int maxMsToLive) => Platform.AppContext.SendBroadcast(connectivityIntent);
}

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

Expand Down Expand Up @@ -245,7 +313,7 @@ public ConnectivityBroadcastReceiver(Action onChanged) =>
public override async void OnReceive(Context context, Intent intent)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (intent.Action != ConnectivityManager.ConnectivityAction)
if (intent.Action != ConnectivityManager.ConnectivityAction && intent.Action != Platform.EssentialsConnectivityChanged)
#pragma warning restore CS0618 // Type or member is obsolete
return;

Expand Down
2 changes: 2 additions & 0 deletions Xamarin.Essentials/Platform/Platform.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public static partial class Platform

public static event EventHandler<ActivityStateChangedEventArgs> ActivityStateChanged;

internal const string EssentialsConnectivityChanged = "com.xamarin.essentials.ESSENTIALS_CONNECTIVITY_CHANGED";

internal const int requestCodeFilePicker = 11001;
internal const int requestCodeMediaPicker = 11002;
internal const int requestCodeMediaCapture = 11003;
Expand Down

0 comments on commit 20d627e

Please sign in to comment.