Skip to content

Commit

Permalink
Android is pinning native references - if another lib disposes of the…
Browse files Browse the repository at this point in the history
… system service ref, MAUI cannot recover (#22281)

Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
  • Loading branch information
aritchie and jsuarezruiz authored May 27, 2024
1 parent 320bcb4 commit 37b875a
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 31 deletions.
4 changes: 1 addition & 3 deletions src/Essentials/src/Barometer/Barometer.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ namespace Microsoft.Maui.Devices.Sensors
{
partial class BarometerImplementation : IBarometer
{
static SensorManager? _sensorManager;
static Sensor? _sensor;

static SensorManager? SensorManager =>
_sensorManager ??= Application.Context.GetSystemService(Context.SensorService) as SensorManager;
static SensorManager? SensorManager => Application.Context.GetSystemService(Context.SensorService) as SensorManager;

static Sensor? Sensor =>
_sensor ??= SensorManager?.GetDefaultSensor(SensorType.Pressure);
Expand Down
5 changes: 1 addition & 4 deletions src/Essentials/src/Battery/Battery.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ namespace Microsoft.Maui.Devices
{
partial class BatteryImplementation : IBattery
{
static PowerManager? powerManager;

static PowerManager? PowerManager =>
powerManager ??= Application.Context.GetSystemService(Context.PowerService) as PowerManager;
static PowerManager? PowerManager => Application.Context.GetSystemService(Context.PowerService) as PowerManager;

BatteryBroadcastReceiver? batteryReceiver;
EnergySaverBroadcastReceiver? powerReceiver;
Expand Down
5 changes: 2 additions & 3 deletions src/Essentials/src/Compass/Compass.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace Microsoft.Maui.Devices.Sensors
{
partial class CompassImplementation : ICompass
{
static SensorManager _sensorManager;
static Sensor _accelerometer;
static Sensor _magnetic;

static SensorManager SensorManager =>
_sensorManager ??= Application.Context.GetSystemService(Context.SensorService) as SensorManager;
static SensorManager SensorManager =>
Application.Context.GetSystemService(Context.SensorService) as SensorManager;

static Sensor Accelerometer =>
_accelerometer ??= SensorManager?.GetDefaultSensor(SensorType.Accelerometer);
Expand Down
7 changes: 2 additions & 5 deletions src/Essentials/src/Connectivity/Connectivity.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ 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 ConnectivityManager connectivityManager;

static ConnectivityManager ConnectivityManager =>
connectivityManager ??= Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;
static ConnectivityManager ConnectivityManager =>
Application.Context.GetSystemService(Context.ConnectivityService) as ConnectivityManager;

ConnectivityBroadcastReceiver conectivityReceiver;
EssentialsNetworkCallback networkCallback;
Expand Down
6 changes: 2 additions & 4 deletions src/Essentials/src/Flashlight/Flashlight.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ namespace Microsoft.Maui.Devices
{
class FlashlightImplementation : IFlashlight
{
static CameraManager cameraManager;

static CameraManager CameraManager =>
cameraManager ??= Application.Context.GetSystemService(Context.CameraService) as CameraManager;
static CameraManager CameraManager =>
Application.Context.GetSystemService(Context.CameraService) as CameraManager;

static readonly object locker = new object();

Expand Down
4 changes: 1 addition & 3 deletions src/Essentials/src/Geolocation/Geolocation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ partial class GeolocationImplementation : IGeolocation
static ContinuousLocationListener? continuousListener;
static List<string>? listeningProviders;

static LocationManager? locationManager;

static LocationManager? LocationManager =>
locationManager ??= Application.Context.GetSystemService(Context.LocationService) as LocationManager;
Application.Context.GetSystemService(Context.LocationService) as LocationManager;

/// <summary>
/// Indicates if currently listening to location updates while the app is in foreground.
Expand Down
4 changes: 2 additions & 2 deletions src/Essentials/src/Gyroscope/Gyroscope.android.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#nullable enable
using System;
using Android.App;
Expand All @@ -8,11 +9,10 @@ namespace Microsoft.Maui.Devices.Sensors
{
partial class GyroscopeImplementation : IGyroscope
{
static SensorManager? _sensorManager;
static Sensor? gyroscope;

static SensorManager? SensorManager =>
_sensorManager ??= Application.Context.GetSystemService(Context.SensorService) as SensorManager;
Application.Context.GetSystemService(Context.SensorService) as SensorManager;

static Sensor? Sensor =>
gyroscope ??= SensorManager?.GetDefaultSensor(SensorType.Gyroscope);
Expand Down
3 changes: 1 addition & 2 deletions src/Essentials/src/Magnetometer/Magnetometer.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace Microsoft.Maui.Devices.Sensors
{
partial class MagnetometerImplementation : IMagnetometer
{
static SensorManager? _sensorManager;
static Sensor? magnetometer;

static SensorManager? SensorManager =>
_sensorManager ??= Application.Context.GetSystemService(Context.SensorService) as SensorManager;
Application.Context.GetSystemService(Context.SensorService) as SensorManager;

static Sensor? Sensor =>
magnetometer ??= SensorManager?.GetDefaultSensor(SensorType.MagneticField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ namespace Microsoft.Maui.Devices.Sensors
{
partial class OrientationSensorImplementation : IOrientationSensor
{
static SensorManager? _sensorManager;
static Sensor? orientationSensor;

static SensorManager? SensorManager =>
_sensorManager ??= Application.Context.GetSystemService(Context.SensorService) as SensorManager;
Application.Context.GetSystemService(Context.SensorService) as SensorManager;

static Sensor? Sensor =>
orientationSensor ??= SensorManager?.GetDefaultSensor(SensorType.RotationVector);
Expand Down
4 changes: 1 addition & 3 deletions src/Essentials/src/Screenshot/Screenshot.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ namespace Microsoft.Maui.Media
{
partial class ScreenshotImplementation : IPlatformScreenshot, IScreenshot
{
static IWindowManager windowManager;

static IWindowManager WindowManager =>
windowManager ??= Application.Context.GetSystemService(Context.WindowService) as IWindowManager;
Application.Context.GetSystemService(Context.WindowService) as IWindowManager;

public bool IsCaptureSupported => true;

Expand Down

0 comments on commit 37b875a

Please sign in to comment.