Merge pull request #2108 from xamarin/api34-compat

Add API 34/Android 14 compatibility for Connectivity & Battery
This commit is contained in:
Gerald Versluis 2023-12-05 00:30:44 -08:00 коммит произвёл GitHub
Родитель 6f527dbeba 6e72571251
Коммит 6ec2124ec4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 21 добавлений и 8 удалений

Просмотреть файл

@ -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" />

Просмотреть файл

@ -15,7 +15,7 @@ namespace Xamarin.Essentials
return;
powerReceiver = new EnergySaverBroadcastReceiver(OnEnergySaverChanged);
Platform.AppContext.RegisterReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged));
Platform.RegisterBroadcastReceiver(powerReceiver, new IntentFilter(PowerManager.ActionPowerSaveModeChanged), false);
}
static void StopEnergySaverListeners()
@ -52,7 +52,7 @@ namespace Xamarin.Essentials
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()
@ -76,7 +76,7 @@ namespace Xamarin.Essentials
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);
@ -96,7 +96,7 @@ namespace Xamarin.Essentials
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)
@ -123,7 +123,7 @@ namespace Xamarin.Essentials
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);

Просмотреть файл

@ -34,7 +34,7 @@ namespace Xamarin.Essentials
conectivityReceiver = new ConnectivityBroadcastReceiver(OnConnectivityChanged);
Platform.AppContext.RegisterReceiver(conectivityReceiver, filter);
Platform.RegisterBroadcastReceiver(conectivityReceiver, filter, false);
}
static void StopListeners()

Просмотреть файл

@ -122,6 +122,19 @@ namespace Xamarin.Essentials
}
}
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;