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

Add API 34/Android 14 compatibility for Connectivity & Battery #2108

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0.1.0" package="com.xamarin.essentials.devicetests" android:installLocation="auto">
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="33" />
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
Expand Down
10 changes: 5 additions & 5 deletions Xamarin.Essentials/Battery/Battery.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static void StartEnergySaverListeners()
return;

powerReceiver = new EnergySaverBroadcastReceiver(OnEnergySaverChanged);
Platform.AppContext.RegisterReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged));
Platform.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged), false);
}

static void StopEnergySaverListeners()
Expand Down Expand Up @@ -52,7 +52,7 @@ static void StartBatteryListeners()
Permissions.EnsureDeclared<Permissions.Battery>();

batteryReceiver = new BatteryBroadcastReceiver(OnBatteryInfoChanged);
Platform.AppContext.RegisterReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged));
Platform.RegisterBroadcastReceiver(batteryReceiver, new IntentFilter(Intent.ActionBatteryChanged), false);
}

static void StopBatteryListeners()
Expand All @@ -76,7 +76,7 @@ static double PlatformChargeLevel
Permissions.EnsureDeclared<Permissions.Battery>();

using (var filter = new IntentFilter(Intent.ActionBatteryChanged))
using (var battery = Platform.AppContext.RegisterReceiver(null, filter))
using (var battery = Platform.RegisterBroadcastReceiver(null, filter, false))
{
var level = battery.GetIntExtra(BatteryManager.ExtraLevel, -1);
var scale = battery.GetIntExtra(BatteryManager.ExtraScale, -1);
Expand All @@ -96,7 +96,7 @@ static BatteryState PlatformState
Permissions.EnsureDeclared<Permissions.Battery>();

using (var filter = new IntentFilter(Intent.ActionBatteryChanged))
using (var battery = Platform.AppContext.RegisterReceiver(null, filter))
using (var battery = Platform.RegisterBroadcastReceiver(null, filter, false))
{
var status = battery.GetIntExtra(BatteryManager.ExtraStatus, -1);
switch (status)
Expand All @@ -123,7 +123,7 @@ static BatteryPowerSource PlatformPowerSource
Permissions.EnsureDeclared<Permissions.Battery>();

using (var filter = new IntentFilter(Intent.ActionBatteryChanged))
using (var battery = Platform.AppContext.RegisterReceiver(null, filter))
using (var battery = Platform.RegisterBroadcastReceiver(null, filter, false))
{
var chargePlug = battery.GetIntExtra(BatteryManager.ExtraPlugged, -1);

Expand Down
2 changes: 1 addition & 1 deletion Xamarin.Essentials/Connectivity/Connectivity.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void StartListeners()

conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);

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

static void StopListeners()
Expand Down
13 changes: 13 additions & 0 deletions Xamarin.Essentials/Platform/Platform.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ static void CheckAppActions(AndroidIntent intent)
}
}

internal static AndroidIntent RegisterBroadcastReceiver(BroadcastReceiver receiver, IntentFilter filter, bool exported)
{
if (HasApiLevel(34))
{
var flags = exported ? ReceiverFlags.Exported : ReceiverFlags.NotExported;

// Explicit cast of flags because of: https://github.com/xamarin/xamarin-android/issues/7503
return AppContext.RegisterReceiver(receiver, filter, (ActivityFlags)flags);
}

return AppContext.RegisterReceiver(receiver, filter);
}

internal static bool HasSystemFeature(string systemFeature)
{
var packageManager = AppContext.PackageManager;
Expand Down