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 #2108 from xamarin/api34-compat
Browse files Browse the repository at this point in the history
Add API 34/Android 14 compatibility for Connectivity & Battery
  • Loading branch information
jfversluis authored Dec 5, 2023
2 parents 6f527db + 6e72571 commit 6ec2124
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
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

0 comments on commit 6ec2124

Please sign in to comment.