Skip to content

Commit

Permalink
Set package on connectivity intent
Browse files Browse the repository at this point in the history
With changes in android 14, we need to scope the connectivity intent we use to trigger our own internal broadcast receiver to the current app package (which we get from the application context).
  • Loading branch information
Redth authored and mattleibow committed Feb 16, 2024
1 parent 8335ee2 commit 94d42d2
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Essentials/src/Connectivity/Connectivity.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ partial class ConnectivityImplementation : IConnectivity
/// Unique identifier for the connectivity changed action on Android.
/// </summary>
public const string ConnectivityChangedAction = "com.maui.essentials.ESSENTIALS_CONNECTIVITY_CHANGED";
static Intent connectivityIntent = new Intent(ConnectivityChangedAction);


static ConnectivityManager connectivityManager;

static ConnectivityManager ConnectivityManager =>
Expand Down Expand Up @@ -51,7 +50,9 @@ void StartListeners()
void StopListeners()
{
if (conectivityReceiver == null)
{
return;
}

try
{
Expand All @@ -77,11 +78,15 @@ void StopListeners()
void RegisterNetworkCallback()
{
if (!OperatingSystem.IsAndroidVersionAtLeast(24))
{
return;
}

var manager = ConnectivityManager;
if (manager == null)
{
return;
}

var request = new NetworkRequest.Builder().Build();
networkCallback = new EssentialsNetworkCallback();
Expand All @@ -91,11 +96,15 @@ void RegisterNetworkCallback()
void UnregisterNetworkCallback()
{
if (!OperatingSystem.IsAndroidVersionAtLeast(24))
{
return;
}

var manager = ConnectivityManager;
if (manager == null || networkCallback == null)
{
return;
}

manager.UnregisterNetworkCallback(networkCallback);

Expand All @@ -104,6 +113,14 @@ void UnregisterNetworkCallback()

class EssentialsNetworkCallback : ConnectivityManager.NetworkCallback
{
readonly Intent connectivityIntent;

public EssentialsNetworkCallback()
{
connectivityIntent = new Intent(ConnectivityChangedAction);
connectivityIntent.SetPackage(Application.Context.PackageName);
}

public override void OnAvailable(Network network) =>
Application.Context.SendBroadcast(connectivityIntent);

Expand Down Expand Up @@ -159,15 +176,19 @@ public NetworkAccess NetworkAccess
var capabilities = manager.GetNetworkCapabilities(network);

if (capabilities == null)
{
continue;
}

#pragma warning disable CS0618 // Type or member is obsolete
#pragma warning disable CA1416 // Validate platform compatibility
#pragma warning disable CA1422 // Validate platform compatibility
var info = manager.GetNetworkInfo(network);

if (info == null || !info.IsAvailable)
{
continue;
}
#pragma warning restore CS0618 // Type or member is obsolete

// Check to see if it has the internet capability
Expand Down Expand Up @@ -200,12 +221,18 @@ void ProcessAllNetworkInfo()
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);
}
#pragma warning restore CA1422 // Validate platform compatibility
#pragma warning restore CA1416 // Validate platform compatibility
#pragma warning restore CS0618 // Type or member is obsolete
Expand Down Expand Up @@ -250,15 +277,19 @@ public IEnumerable<ConnectionProfile> ConnectionProfiles

var p = ProcessNetworkInfo(info);
if (p.HasValue)
{
yield return p.Value;
}
}

#pragma warning disable CS0618 // Type or member is obsolete
static ConnectionProfile? ProcessNetworkInfo(NetworkInfo info)
{

if (info == null || !info.IsAvailable || !info.IsConnectedOrConnecting)
{
return null;
}


return GetConnectionType(info.Type, info.TypeName);
Expand Down Expand Up @@ -289,22 +320,34 @@ internal static ConnectionProfile GetConnectionType(ConnectivityType connectivit
return ConnectionProfile.Unknown;
default:
if (string.IsNullOrWhiteSpace(typeName))
{
return ConnectionProfile.Unknown;
}

if (typeName.Contains("mobile", StringComparison.OrdinalIgnoreCase))
{
return ConnectionProfile.Cellular;
}

if (typeName.Contains("wimax", StringComparison.OrdinalIgnoreCase))
{
return ConnectionProfile.Cellular;
}

if (typeName.Contains("wifi", StringComparison.OrdinalIgnoreCase))
{
return ConnectionProfile.WiFi;
}

if (typeName.Contains("ethernet", StringComparison.OrdinalIgnoreCase))
{
return ConnectionProfile.Ethernet;
}

if (typeName.Contains("bluetooth", StringComparison.OrdinalIgnoreCase))
{
return ConnectionProfile.Bluetooth;
}

return ConnectionProfile.Unknown;
}
Expand Down Expand Up @@ -335,7 +378,9 @@ public override async void OnReceive(Context context, Intent intent)
#pragma warning disable CS0618 // Type or member is obsolete
if (intent.Action != ConnectivityManager.ConnectivityAction && intent.Action != ConnectivityImplementation.ConnectivityChangedAction)
#pragma warning restore CS0618 // Type or member is obsolete
{
return;
}

// await 1500ms to ensure that the the connection manager updates
await Task.Delay(1500);
Expand Down

0 comments on commit 94d42d2

Please sign in to comment.