From 25e709785d35b39aeb8fabab6ea0c5d138e0989a Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Tue, 7 May 2024 15:39:38 -0400 Subject: [PATCH] Android is pinning native references - if another lib disposes of the system service ref, MAUI cannot recover --- src/Essentials/src/Barometer/Barometer.android.cs | 4 +--- src/Essentials/src/Battery/Battery.android.cs | 5 +---- src/Essentials/src/Compass/Compass.android.cs | 5 ++--- src/Essentials/src/Connectivity/Connectivity.android.cs | 7 ++----- src/Essentials/src/Flashlight/Flashlight.android.cs | 6 ++---- src/Essentials/src/Geolocation/Geolocation.android.cs | 4 +--- src/Essentials/src/Gyroscope/Gyroscope.android.cs | 4 ++-- src/Essentials/src/Magnetometer/Magnetometer.android.cs | 3 +-- .../src/OrientationSensor/OrientationSensor.android.cs | 3 +-- src/Essentials/src/Screenshot/Screenshot.android.cs | 4 +--- 10 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/Essentials/src/Barometer/Barometer.android.cs b/src/Essentials/src/Barometer/Barometer.android.cs index e606814c056a..22057351bf1d 100644 --- a/src/Essentials/src/Barometer/Barometer.android.cs +++ b/src/Essentials/src/Barometer/Barometer.android.cs @@ -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); diff --git a/src/Essentials/src/Battery/Battery.android.cs b/src/Essentials/src/Battery/Battery.android.cs index c8bb39bf7416..244b2fdbbaa6 100755 --- a/src/Essentials/src/Battery/Battery.android.cs +++ b/src/Essentials/src/Battery/Battery.android.cs @@ -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; diff --git a/src/Essentials/src/Compass/Compass.android.cs b/src/Essentials/src/Compass/Compass.android.cs index e4812170e1be..5ba23295e288 100644 --- a/src/Essentials/src/Compass/Compass.android.cs +++ b/src/Essentials/src/Compass/Compass.android.cs @@ -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); diff --git a/src/Essentials/src/Connectivity/Connectivity.android.cs b/src/Essentials/src/Connectivity/Connectivity.android.cs index 72b2a9a55401..9eb0edc9490f 100644 --- a/src/Essentials/src/Connectivity/Connectivity.android.cs +++ b/src/Essentials/src/Connectivity/Connectivity.android.cs @@ -15,11 +15,8 @@ partial class ConnectivityImplementation : IConnectivity /// Unique identifier for the connectivity changed action on Android. /// 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; diff --git a/src/Essentials/src/Flashlight/Flashlight.android.cs b/src/Essentials/src/Flashlight/Flashlight.android.cs index f80f21685555..8f6433daa6c9 100644 --- a/src/Essentials/src/Flashlight/Flashlight.android.cs +++ b/src/Essentials/src/Flashlight/Flashlight.android.cs @@ -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(); diff --git a/src/Essentials/src/Geolocation/Geolocation.android.cs b/src/Essentials/src/Geolocation/Geolocation.android.cs index 6aac99ad2b1e..f69b1d1f2a47 100644 --- a/src/Essentials/src/Geolocation/Geolocation.android.cs +++ b/src/Essentials/src/Geolocation/Geolocation.android.cs @@ -22,10 +22,8 @@ partial class GeolocationImplementation : IGeolocation static ContinuousLocationListener? continuousListener; static List? listeningProviders; - static LocationManager? locationManager; - static LocationManager? LocationManager => - locationManager ??= Application.Context.GetSystemService(Context.LocationService) as LocationManager; + Application.Context.GetSystemService(Context.LocationService) as LocationManager; /// /// Indicates if currently listening to location updates while the app is in foreground. diff --git a/src/Essentials/src/Gyroscope/Gyroscope.android.cs b/src/Essentials/src/Gyroscope/Gyroscope.android.cs index 2ef22215b5a0..89ca320389b6 100644 --- a/src/Essentials/src/Gyroscope/Gyroscope.android.cs +++ b/src/Essentials/src/Gyroscope/Gyroscope.android.cs @@ -1,3 +1,4 @@ + #nullable enable using System; using Android.App; @@ -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); diff --git a/src/Essentials/src/Magnetometer/Magnetometer.android.cs b/src/Essentials/src/Magnetometer/Magnetometer.android.cs index 860e78390e95..f3e100f85528 100644 --- a/src/Essentials/src/Magnetometer/Magnetometer.android.cs +++ b/src/Essentials/src/Magnetometer/Magnetometer.android.cs @@ -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); diff --git a/src/Essentials/src/OrientationSensor/OrientationSensor.android.cs b/src/Essentials/src/OrientationSensor/OrientationSensor.android.cs index 75c9d1d7734b..2226b89fa43c 100644 --- a/src/Essentials/src/OrientationSensor/OrientationSensor.android.cs +++ b/src/Essentials/src/OrientationSensor/OrientationSensor.android.cs @@ -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); diff --git a/src/Essentials/src/Screenshot/Screenshot.android.cs b/src/Essentials/src/Screenshot/Screenshot.android.cs index e9593d662269..0b8f1a32fc38 100644 --- a/src/Essentials/src/Screenshot/Screenshot.android.cs +++ b/src/Essentials/src/Screenshot/Screenshot.android.cs @@ -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;