From 120ce41cd963ade79723afb421658984cafadd4a Mon Sep 17 00:00:00 2001 From: Michael Fink Date: Sun, 22 Jan 2023 15:37:20 +0100 Subject: [PATCH 1/7] enabled nullable checks for GeolocationRequest class --- src/Essentials/src/Geolocation/GeolocationRequest.shared.cs | 1 + src/Essentials/src/Geolocation/GeolocationRequest.uwp.cs | 2 ++ .../src/PublicAPI/net-android/PublicAPI.Unshipped.txt | 1 + src/Essentials/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt | 1 + .../src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt | 1 + src/Essentials/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt | 1 + .../src/PublicAPI/net-windows/PublicAPI.Unshipped.txt | 1 + src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt | 1 + .../src/PublicAPI/netstandard/PublicAPI.Unshipped.txt | 1 + 9 files changed, 10 insertions(+) diff --git a/src/Essentials/src/Geolocation/GeolocationRequest.shared.cs b/src/Essentials/src/Geolocation/GeolocationRequest.shared.cs index 4715414620d2..7708eed46de5 100644 --- a/src/Essentials/src/Geolocation/GeolocationRequest.shared.cs +++ b/src/Essentials/src/Geolocation/GeolocationRequest.shared.cs @@ -1,3 +1,4 @@ +#nullable enable using System; namespace Microsoft.Maui.Devices.Sensors diff --git a/src/Essentials/src/Geolocation/GeolocationRequest.uwp.cs b/src/Essentials/src/Geolocation/GeolocationRequest.uwp.cs index c35c2605fad3..240d0668444b 100644 --- a/src/Essentials/src/Geolocation/GeolocationRequest.uwp.cs +++ b/src/Essentials/src/Geolocation/GeolocationRequest.uwp.cs @@ -1,3 +1,5 @@ +#nullable enable + namespace Microsoft.Maui.Devices.Sensors { public partial class GeolocationRequest diff --git a/src/Essentials/src/PublicAPI/net-android/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/net-android/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/net-android/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/net-android/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? diff --git a/src/Essentials/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? diff --git a/src/Essentials/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? diff --git a/src/Essentials/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? diff --git a/src/Essentials/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? diff --git a/src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/net/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? diff --git a/src/Essentials/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt b/src/Essentials/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt index c64aedf4601a..f4e82ef15b8e 100644 --- a/src/Essentials/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt +++ b/src/Essentials/src/PublicAPI/netstandard/PublicAPI.Unshipped.txt @@ -16,6 +16,7 @@ Microsoft.Maui.Devices.Sensors.GeolocationListeningFailedEventArgs.GeolocationLi Microsoft.Maui.Devices.Sensors.IGeolocation.ListeningFailed -> System.EventHandler? Microsoft.Maui.Storage.ISecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! static Microsoft.Maui.Devices.Sensors.Geolocation.ListeningFailed -> System.EventHandler! +override Microsoft.Maui.Devices.Sensors.GeolocationRequest.ToString() -> string! static Microsoft.Maui.Storage.SecureStorage.GetAsync(string! key) -> System.Threading.Tasks.Task! Microsoft.Maui.Devices.Sensors.IGeolocation.IsListeningForeground.get -> bool Microsoft.Maui.Devices.Sensors.IGeolocation.LocationChanged -> System.EventHandler? From e38f5340cfabb3c81d0fd71d87a15cabe73901d2 Mon Sep 17 00:00:00 2001 From: Michael Fink Date: Sun, 22 Jan 2023 15:38:35 +0100 Subject: [PATCH 2/7] enabled nullable checks for more Geolocation source files --- .../src/Geolocation/Geolocation.netstandard.tvos.watchos.cs | 5 +++-- src/Essentials/src/Geolocation/Geolocation.tizen.cs | 1 + .../Geolocation/GeolocationAccuracyExtensionMethods.uwp.cs | 4 +++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Essentials/src/Geolocation/Geolocation.netstandard.tvos.watchos.cs b/src/Essentials/src/Geolocation/Geolocation.netstandard.tvos.watchos.cs index 4fc89b6e5a1e..5f6d413a3da2 100644 --- a/src/Essentials/src/Geolocation/Geolocation.netstandard.tvos.watchos.cs +++ b/src/Essentials/src/Geolocation/Geolocation.netstandard.tvos.watchos.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Threading; using System.Threading.Tasks; @@ -7,10 +8,10 @@ namespace Microsoft.Maui.Devices.Sensors { partial class GeolocationImplementation : IGeolocation { - public Task GetLastKnownLocationAsync() => + public Task GetLastKnownLocationAsync() => throw ExceptionUtils.NotSupportedOrImplementedException; - public Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) => + public Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) => throw ExceptionUtils.NotSupportedOrImplementedException; public bool IsListeningForeground { get => false; } diff --git a/src/Essentials/src/Geolocation/Geolocation.tizen.cs b/src/Essentials/src/Geolocation/Geolocation.tizen.cs index 9ed480844860..cc136395f6fb 100644 --- a/src/Essentials/src/Geolocation/Geolocation.tizen.cs +++ b/src/Essentials/src/Geolocation/Geolocation.tizen.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Threading; using System.Threading.Tasks; diff --git a/src/Essentials/src/Geolocation/GeolocationAccuracyExtensionMethods.uwp.cs b/src/Essentials/src/Geolocation/GeolocationAccuracyExtensionMethods.uwp.cs index aa5bad7d6e05..ce9461fc1a58 100644 --- a/src/Essentials/src/Geolocation/GeolocationAccuracyExtensionMethods.uwp.cs +++ b/src/Essentials/src/Geolocation/GeolocationAccuracyExtensionMethods.uwp.cs @@ -1,4 +1,6 @@ -namespace Microsoft.Maui.Devices.Sensors +#nullable enable + +namespace Microsoft.Maui.Devices.Sensors { static class GeolocationAccuracyExtensionMethods { From 70a8e18757f0cbaebfd59e88910f67e40abef195 Mon Sep 17 00:00:00 2001 From: Michael Fink Date: Sun, 22 Jan 2023 15:39:30 +0100 Subject: [PATCH 3/7] enabled nullable checks for Geolocation Android and iOS implementation --- .../src/Geolocation/Geolocation.android.cs | 70 ++++++++++++------- .../src/Geolocation/Geolocation.ios.macos.cs | 22 +++--- 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/Essentials/src/Geolocation/Geolocation.android.cs b/src/Essentials/src/Geolocation/Geolocation.android.cs index b7035227b163..4a20242165c4 100644 --- a/src/Essentials/src/Geolocation/Geolocation.android.cs +++ b/src/Essentials/src/Geolocation/Geolocation.android.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Collections.Generic; using System.Linq; @@ -18,12 +19,12 @@ partial class GeolocationImplementation : IGeolocation const long twoMinutes = 120000; static readonly string[] ignoredProviders = new string[] { LocationManager.PassiveProvider, "local_database" }; - static ContinuousLocationListener continuousListener; - static List listeningProviders; + static ContinuousLocationListener? continuousListener; + static List? listeningProviders; - static LocationManager locationManager; + static LocationManager? locationManager; - static LocationManager LocationManager => + static LocationManager? LocationManager => locationManager ??= Application.Context.GetSystemService(Context.LocationService) as LocationManager; /// @@ -31,11 +32,14 @@ partial class GeolocationImplementation : IGeolocation /// public bool IsListeningForeground { get => continuousListener != null; } - public async Task GetLastKnownLocationAsync() + public async Task GetLastKnownLocationAsync() { + if (LocationManager == null) + throw new FeatureNotSupportedException("Android LocationManager is not available"); + await Permissions.EnsureGrantedOrRestrictedAsync(); - AndroidLocation bestLocation = null; + AndroidLocation? bestLocation = null; foreach (var provider in LocationManager.GetProviders(true)) { @@ -48,10 +52,13 @@ public async Task GetLastKnownLocationAsync() return bestLocation?.ToLocation(); } - public async Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) + public async Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(request); + if (LocationManager == null) + throw new FeatureNotSupportedException("Android LocationManager is not available"); + await Permissions.EnsureGrantedOrRestrictedAsync(); var enabledProviders = LocationManager.GetProviders(true); @@ -68,7 +75,7 @@ public async Task GetLocationAsync(GeolocationRequest request, Cancell if (string.IsNullOrEmpty(providerInfo.Provider)) return await GetLastKnownLocationAsync(); - var tcs = new TaskCompletionSource(); + var tcs = new TaskCompletionSource(); var allProviders = LocationManager.GetProviders(false); @@ -110,11 +117,14 @@ void HandleLocation(AndroidLocation location) void Cancel() { RemoveUpdates(); - tcs.TrySetResult(listener.BestLocation); + tcs.TrySetResult(listener?.BestLocation); } void RemoveUpdates() { + if (LocationManager == null) + return; + for (var i = 0; i < providers.Count; i++) LocationManager.RemoveUpdates(listener); } @@ -135,13 +145,17 @@ public async Task StartListeningForegroundAsync(GeolocationListeningReques { ArgumentNullException.ThrowIfNull(request); + if (LocationManager == null) + throw new FeatureNotSupportedException("Android LocationManager is not available"); + if (IsListeningForeground) throw new InvalidOperationException("Already listening to location changes."); await Permissions.EnsureGrantedOrRestrictedAsync(); var enabledProviders = LocationManager.GetProviders(true); - var hasProviders = enabledProviders.Any(p => !ignoredProviders.Contains(p)); + var hasProviders = enabledProviders != null && + enabledProviders.Any(p => !ignoredProviders.Contains(p)); if (!hasProviders) throw new FeatureNotEnabledException("Location services are not enabled on device."); @@ -204,7 +218,8 @@ public void StopListeningForeground() continuousListener.LocationHandler = null; continuousListener.ErrorHandler = null; - if (listeningProviders == null) + if (listeningProviders == null || + LocationManager == null) return; for (var i = 0; i < listeningProviders.Count; i++) @@ -215,7 +230,7 @@ public void StopListeningForeground() continuousListener = null; } - static (string Provider, float Accuracy) GetBestProvider(LocationManager locationManager, GeolocationAccuracy accuracy) + static (string? Provider, float Accuracy) GetBestProvider(LocationManager locationManager, GeolocationAccuracy accuracy) { // Criteria: https://developer.android.com/reference/android/location/Criteria @@ -268,7 +283,7 @@ public void StopListeningForeground() return (provider, accuracyDistance); } - internal static bool IsBetterLocation(AndroidLocation location, AndroidLocation bestLocation) + internal static bool IsBetterLocation(AndroidLocation location, AndroidLocation? bestLocation) { if (bestLocation == null) return true; @@ -311,15 +326,15 @@ class SingleLocationListener : Java.Lang.Object, ILocationListener float desiredAccuracy; - internal AndroidLocation BestLocation { get; set; } + internal AndroidLocation? BestLocation { get; set; } HashSet activeProviders = new HashSet(); bool wasRaised = false; - internal Action LocationHandler { get; set; } + internal Action? LocationHandler { get; set; } - internal SingleLocationListener(LocationManager manager, float desiredAccuracy, IEnumerable activeProviders) + internal SingleLocationListener(LocationManager? manager, float desiredAccuracy, IEnumerable activeProviders) { this.desiredAccuracy = desiredAccuracy; @@ -327,7 +342,7 @@ internal SingleLocationListener(LocationManager manager, float desiredAccuracy, foreach (var provider in activeProviders) { - var location = manager.GetLastKnownLocation(provider); + var location = manager?.GetLastKnownLocation(provider); if (location != null && GeolocationImplementation.IsBetterLocation(location, BestLocation)) BestLocation = location; } @@ -365,8 +380,11 @@ void ILocationListener.OnProviderEnabled(string provider) activeProviders.Add(provider); } - void ILocationListener.OnStatusChanged(string provider, Availability status, Bundle extras) + void ILocationListener.OnStatusChanged(string? provider, Availability status, Bundle? extras) { + if (provider == null) + return; + switch (status) { case Availability.Available: @@ -382,24 +400,21 @@ void ILocationListener.OnStatusChanged(string provider, Availability status, Bun class ContinuousLocationListener : Java.Lang.Object, ILocationListener { - readonly LocationManager manager; - float desiredAccuracy; HashSet activeProviders = new HashSet(); - internal Action LocationHandler { get; set; } + internal Action? LocationHandler { get; set; } - internal Action ErrorHandler { get; set; } + internal Action? ErrorHandler { get; set; } - internal ContinuousLocationListener(LocationManager manager, float desiredAccuracy, IEnumerable providers) + internal ContinuousLocationListener(LocationManager? manager, float desiredAccuracy, IEnumerable providers) { - this.manager = manager; this.desiredAccuracy = desiredAccuracy; foreach (var provider in providers) { - if (manager.IsProviderEnabled(provider)) + if (manager != null && manager.IsProviderEnabled(provider)) activeProviders.Add(provider); } } @@ -429,8 +444,11 @@ void ILocationListener.OnProviderEnabled(string provider) activeProviders.Add(provider); } - void ILocationListener.OnStatusChanged(string provider, Availability status, Bundle extras) + void ILocationListener.OnStatusChanged(string? provider, Availability status, Bundle? extras) { + if (provider == null) + return; + switch (status) { case Availability.Available: diff --git a/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs b/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs index e2c93bace244..f3e6a2f9bf20 100644 --- a/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs +++ b/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs @@ -1,3 +1,4 @@ +#nullable enable using System; using System.Linq; using System.Threading; @@ -10,14 +11,14 @@ namespace Microsoft.Maui.Devices.Sensors { partial class GeolocationImplementation : IGeolocation { - CLLocationManager listeningManager; + CLLocationManager? listeningManager; /// /// Indicates if currently listening to location updates while the app is in foreground. /// public bool IsListeningForeground { get => listeningManager != null; } - public async Task GetLastKnownLocationAsync() + public async Task GetLastKnownLocationAsync() { if (!CLLocationManager.LocationServicesEnabled) throw new FeatureNotEnabledException("Location services are not enabled on device."); @@ -37,7 +38,7 @@ public async Task GetLastKnownLocationAsync() return location?.ToLocation(reducedAccuracy); } - public async Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) + public async Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(request); @@ -50,7 +51,7 @@ public async Task GetLocationAsync(GeolocationRequest request, Cancell // so just use the main loop var manager = MainThread.InvokeOnMainThread(() => new CLLocationManager()); - var tcs = new TaskCompletionSource(manager); + var tcs = new TaskCompletionSource(manager); var listener = new SingleLocationListener(); listener.LocationHandler += HandleLocation; @@ -155,7 +156,7 @@ public async Task StartListeningForegroundAsync(GeolocationListeningReques void HandleLocation(CLLocation clLocation) { - OnLocationChanged(clLocation?.ToLocation(reducedAccuracy)); + OnLocationChanged(clLocation.ToLocation(reducedAccuracy)); } void HandleError(GeolocationError error) @@ -172,7 +173,8 @@ void HandleError(GeolocationError error) /// public void StopListeningForeground() { - if (!IsListeningForeground) + if (!IsListeningForeground || + listeningManager == null) return; listeningManager.StopUpdatingLocation(); @@ -183,7 +185,7 @@ public void StopListeningForeground() listener.ErrorHandler = null; } - listeningManager.Delegate = null; + listeningManager.WeakDelegate = null; listeningManager = null; } @@ -193,7 +195,7 @@ class SingleLocationListener : CLLocationManagerDelegate { bool wasRaised = false; - internal Action LocationHandler { get; set; } + internal Action? LocationHandler { get; set; } /// public override void LocationsUpdated(CLLocationManager manager, CLLocation[] locations) @@ -217,9 +219,9 @@ public override void LocationsUpdated(CLLocationManager manager, CLLocation[] lo class ContinuousLocationListener : CLLocationManagerDelegate { - internal Action LocationHandler { get; set; } + internal Action? LocationHandler { get; set; } - internal Action ErrorHandler { get; set; } + internal Action? ErrorHandler { get; set; } /// public override void LocationsUpdated(CLLocationManager manager, CLLocation[] locations) From 877c80860ec2941f418229932da473ea53667ca9 Mon Sep 17 00:00:00 2001 From: Michael Fink Date: Thu, 16 Feb 2023 08:39:01 +0100 Subject: [PATCH 4/7] changed null checks as suggested in review --- .../src/Geolocation/Geolocation.android.cs | 14 +++++++------- .../src/Geolocation/Geolocation.ios.macos.cs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Essentials/src/Geolocation/Geolocation.android.cs b/src/Essentials/src/Geolocation/Geolocation.android.cs index 4a20242165c4..06367fcae1cf 100644 --- a/src/Essentials/src/Geolocation/Geolocation.android.cs +++ b/src/Essentials/src/Geolocation/Geolocation.android.cs @@ -34,7 +34,7 @@ partial class GeolocationImplementation : IGeolocation public async Task GetLastKnownLocationAsync() { - if (LocationManager == null) + if (LocationManager is null) throw new FeatureNotSupportedException("Android LocationManager is not available"); await Permissions.EnsureGrantedOrRestrictedAsync(); @@ -56,7 +56,7 @@ partial class GeolocationImplementation : IGeolocation { ArgumentNullException.ThrowIfNull(request); - if (LocationManager == null) + if (LocationManager is null) throw new FeatureNotSupportedException("Android LocationManager is not available"); await Permissions.EnsureGrantedOrRestrictedAsync(); @@ -122,7 +122,7 @@ void Cancel() void RemoveUpdates() { - if (LocationManager == null) + if (LocationManager is null) return; for (var i = 0; i < providers.Count; i++) @@ -145,7 +145,7 @@ public async Task StartListeningForegroundAsync(GeolocationListeningReques { ArgumentNullException.ThrowIfNull(request); - if (LocationManager == null) + if (LocationManager is null) throw new FeatureNotSupportedException("Android LocationManager is not available"); if (IsListeningForeground) @@ -218,8 +218,8 @@ public void StopListeningForeground() continuousListener.LocationHandler = null; continuousListener.ErrorHandler = null; - if (listeningProviders == null || - LocationManager == null) + if (listeningProviders is null || + LocationManager is null) return; for (var i = 0; i < listeningProviders.Count; i++) @@ -382,7 +382,7 @@ void ILocationListener.OnProviderEnabled(string provider) void ILocationListener.OnStatusChanged(string? provider, Availability status, Bundle? extras) { - if (provider == null) + if (provider is null) return; switch (status) diff --git a/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs b/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs index f3e6a2f9bf20..1f3bb82c18ac 100644 --- a/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs +++ b/src/Essentials/src/Geolocation/Geolocation.ios.macos.cs @@ -174,7 +174,7 @@ void HandleError(GeolocationError error) public void StopListeningForeground() { if (!IsListeningForeground || - listeningManager == null) + listeningManager is null) return; listeningManager.StopUpdatingLocation(); From 2bc9e6dcf7df08850cc970efabf8da7e3f511fc6 Mon Sep 17 00:00:00 2001 From: Michael Fink Date: Thu, 16 Feb 2023 21:32:07 +0100 Subject: [PATCH 5/7] fixed nullable errors for Tizen --- src/Essentials/src/Geolocation/Geolocation.tizen.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Essentials/src/Geolocation/Geolocation.tizen.cs b/src/Essentials/src/Geolocation/Geolocation.tizen.cs index cc136395f6fb..d8491022fd0e 100644 --- a/src/Essentials/src/Geolocation/Geolocation.tizen.cs +++ b/src/Essentials/src/Geolocation/Geolocation.tizen.cs @@ -13,15 +13,15 @@ partial class GeolocationImplementation : IGeolocation public bool IsListeningForeground { get => false; } - public Task GetLastKnownLocationAsync() => Task.FromResult(lastKnownLocation); + public Task GetLastKnownLocationAsync() => Task.FromResult(lastKnownLocation); - public async Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) + public async Task GetLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { ArgumentNullException.ThrowIfNull(request); await Permissions.EnsureGrantedAsync(); - Locator service = null; + Locator? service = null; var gps = PlatformUtils.GetFeatureInfo("location.gps"); var wps = PlatformUtils.GetFeatureInfo("location.wps"); if (gps) From cdf87027c976fed34581b48e9ed5f7d5a81ba766 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Fri, 17 Feb 2023 11:10:24 -0500 Subject: [PATCH 6/7] Fix some forgotte == null --- src/Essentials/src/Geolocation/Geolocation.android.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Essentials/src/Geolocation/Geolocation.android.cs b/src/Essentials/src/Geolocation/Geolocation.android.cs index 06367fcae1cf..dba5b20ec542 100644 --- a/src/Essentials/src/Geolocation/Geolocation.android.cs +++ b/src/Essentials/src/Geolocation/Geolocation.android.cs @@ -103,7 +103,7 @@ partial class GeolocationImplementation : IGeolocation var androidLocation = await tcs.Task; - if (androidLocation == null) + if (androidLocation is null) return null; return androidLocation.ToLocation(); @@ -212,7 +212,7 @@ void HandleError(GeolocationError geolocationError) /// public void StopListeningForeground() { - if (continuousListener == null) + if (continuousListener is null) return; continuousListener.LocationHandler = null; @@ -285,7 +285,7 @@ public void StopListeningForeground() internal static bool IsBetterLocation(AndroidLocation location, AndroidLocation? bestLocation) { - if (bestLocation == null) + if (bestLocation is null) return true; var timeDelta = location.Time - bestLocation.Time; @@ -446,7 +446,7 @@ void ILocationListener.OnProviderEnabled(string provider) void ILocationListener.OnStatusChanged(string? provider, Availability status, Bundle? extras) { - if (provider == null) + if (provider is null) return; switch (status) From c6387da76f3c3b779d9cb63d8ed8cd8cc0151fed Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Date: Fri, 17 Feb 2023 11:12:06 -0500 Subject: [PATCH 7/7] Fix some forgotte != null --- src/Essentials/src/Geolocation/Geolocation.android.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Essentials/src/Geolocation/Geolocation.android.cs b/src/Essentials/src/Geolocation/Geolocation.android.cs index dba5b20ec542..e26925a01b37 100644 --- a/src/Essentials/src/Geolocation/Geolocation.android.cs +++ b/src/Essentials/src/Geolocation/Geolocation.android.cs @@ -30,7 +30,7 @@ partial class GeolocationImplementation : IGeolocation /// /// Indicates if currently listening to location updates while the app is in foreground. /// - public bool IsListeningForeground { get => continuousListener != null; } + public bool IsListeningForeground { get => continuousListener is not null; } public async Task GetLastKnownLocationAsync() { @@ -45,7 +45,7 @@ partial class GeolocationImplementation : IGeolocation { var location = LocationManager.GetLastKnownLocation(provider); - if (location != null && IsBetterLocation(location, bestLocation)) + if (location is not null && IsBetterLocation(location, bestLocation)) bestLocation = location; } @@ -154,7 +154,7 @@ public async Task StartListeningForegroundAsync(GeolocationListeningReques await Permissions.EnsureGrantedOrRestrictedAsync(); var enabledProviders = LocationManager.GetProviders(true); - var hasProviders = enabledProviders != null && + var hasProviders = enabledProviders is not null && enabledProviders.Any(p => !ignoredProviders.Contains(p)); if (!hasProviders) @@ -343,7 +343,7 @@ internal SingleLocationListener(LocationManager? manager, float desiredAccuracy, foreach (var provider in activeProviders) { var location = manager?.GetLastKnownLocation(provider); - if (location != null && GeolocationImplementation.IsBetterLocation(location, BestLocation)) + if (location is not null && GeolocationImplementation.IsBetterLocation(location, BestLocation)) BestLocation = location; } } @@ -414,7 +414,7 @@ internal ContinuousLocationListener(LocationManager? manager, float desiredAccur foreach (var provider in providers) { - if (manager != null && manager.IsProviderEnabled(provider)) + if (manager is not null && manager.IsProviderEnabled(provider)) activeProviders.Add(provider); } }