From 8335ee247e469fceeb71f1644cb9988e595fbe01 Mon Sep 17 00:00:00 2001 From: redth Date: Fri, 16 Feb 2024 11:14:53 -0500 Subject: [PATCH 1/2] No need to export app specific broadcast receivers We are the only ones invoking these, so there's no need to export. The export made things 'work' on android 14 because of changes to that api level, but that wasn't the most correct fix. --- src/Essentials/src/Battery/Battery.android.cs | 10 +++++----- .../src/Connectivity/Connectivity.android.cs | 2 +- src/Essentials/src/Platform/PlatformUtils.android.cs | 5 ++--- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/Essentials/src/Battery/Battery.android.cs b/src/Essentials/src/Battery/Battery.android.cs index ae62b86ba3a8..c8bb39bf7416 100755 --- a/src/Essentials/src/Battery/Battery.android.cs +++ b/src/Essentials/src/Battery/Battery.android.cs @@ -21,7 +21,7 @@ partial class BatteryImplementation : IBattery void StartEnergySaverListeners() { powerReceiver = new EnergySaverBroadcastReceiver(OnEnergySaverChanged); - PlatformUtils.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged), false); + PlatformUtils.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged)); } void StopEnergySaverListeners() @@ -52,7 +52,7 @@ void StartBatteryListeners() Permissions.EnsureDeclared(); batteryReceiver = new BatteryBroadcastReceiver(OnBatteryInfoChanged); - PlatformUtils.RegisterBroadcastReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged), false); + PlatformUtils.RegisterBroadcastReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged)); } void StopBatteryListeners() @@ -76,7 +76,7 @@ public double ChargeLevel Permissions.EnsureDeclared(); using (var filter = new IntentFilter(Intent.ActionBatteryChanged)) - using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter, false)) + using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter)) { if (battery is null) return -1; // Unknown @@ -99,7 +99,7 @@ public BatteryState State Permissions.EnsureDeclared(); using (var filter = new IntentFilter(Intent.ActionBatteryChanged)) - using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter, false)) + using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter)) { if (battery is null) return BatteryState.Unknown; @@ -129,7 +129,7 @@ public BatteryPowerSource PowerSource Permissions.EnsureDeclared(); using (var filter = new IntentFilter(Intent.ActionBatteryChanged)) - using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter, false)) + using (var battery = PlatformUtils.RegisterBroadcastReceiver(null, filter)) { if (battery is null) return BatteryPowerSource.Unknown; diff --git a/src/Essentials/src/Connectivity/Connectivity.android.cs b/src/Essentials/src/Connectivity/Connectivity.android.cs index 4eaba170f6c6..bb8cdef410cb 100644 --- a/src/Essentials/src/Connectivity/Connectivity.android.cs +++ b/src/Essentials/src/Connectivity/Connectivity.android.cs @@ -45,7 +45,7 @@ void StartListeners() conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged); - PlatformUtils.RegisterBroadcastReceiver(conectivityReceiver, filter, true); + PlatformUtils.RegisterBroadcastReceiver(conectivityReceiver, filter); } void StopListeners() diff --git a/src/Essentials/src/Platform/PlatformUtils.android.cs b/src/Essentials/src/Platform/PlatformUtils.android.cs index 3afe2b78e4c8..c37b5609492c 100644 --- a/src/Essentials/src/Platform/PlatformUtils.android.cs +++ b/src/Essentials/src/Platform/PlatformUtils.android.cs @@ -26,13 +26,12 @@ internal static int NextRequestCode() return requestCode; } - internal static Intent? RegisterBroadcastReceiver(BroadcastReceiver? receiver, IntentFilter filter, bool exported) + internal static Intent? RegisterBroadcastReceiver(BroadcastReceiver? receiver, IntentFilter filter) { #if ANDROID34_0_OR_GREATER if (OperatingSystem.IsAndroidVersionAtLeast(34)) { - var flags = exported ? ReceiverFlags.Exported : ReceiverFlags.NotExported; - return Application.Context.RegisterReceiver(receiver, filter, flags); + return Application.Context.RegisterReceiver(receiver, filter, ReceiverFlags.NotExported); } #endif return Application.Context.RegisterReceiver(receiver, filter); From 94d42d2b99a63a5a0ad8723c402b6e2cef5f8adf Mon Sep 17 00:00:00 2001 From: redth Date: Fri, 16 Feb 2024 11:17:37 -0500 Subject: [PATCH 2/2] Set package on connectivity intent 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). --- .../src/Connectivity/Connectivity.android.cs | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Essentials/src/Connectivity/Connectivity.android.cs b/src/Essentials/src/Connectivity/Connectivity.android.cs index bb8cdef410cb..72b2a9a55401 100644 --- a/src/Essentials/src/Connectivity/Connectivity.android.cs +++ b/src/Essentials/src/Connectivity/Connectivity.android.cs @@ -15,8 +15,7 @@ partial class ConnectivityImplementation : IConnectivity /// Unique identifier for the connectivity changed action on Android. /// public const string ConnectivityChangedAction = "com.maui.essentials.ESSENTIALS_CONNECTIVITY_CHANGED"; - static Intent connectivityIntent = new Intent(ConnectivityChangedAction); - + static ConnectivityManager connectivityManager; static ConnectivityManager ConnectivityManager => @@ -51,7 +50,9 @@ void StartListeners() void StopListeners() { if (conectivityReceiver == null) + { return; + } try { @@ -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(); @@ -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); @@ -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); @@ -159,7 +176,9 @@ 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 @@ -167,7 +186,9 @@ public NetworkAccess NetworkAccess 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 @@ -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 @@ -250,7 +277,9 @@ public IEnumerable ConnectionProfiles var p = ProcessNetworkInfo(info); if (p.HasValue) + { yield return p.Value; + } } #pragma warning disable CS0618 // Type or member is obsolete @@ -258,7 +287,9 @@ public IEnumerable ConnectionProfiles { if (info == null || !info.IsAvailable || !info.IsConnectedOrConnecting) + { return null; + } return GetConnectionType(info.Type, info.TypeName); @@ -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; } @@ -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);