diff --git a/Xamarin.Essentials/Geolocation/Geolocation.android.cs b/Xamarin.Essentials/Geolocation/Geolocation.android.cs index 8f0c33c99..420b26147 100644 --- a/Xamarin.Essentials/Geolocation/Geolocation.android.cs +++ b/Xamarin.Essentials/Geolocation/Geolocation.android.cs @@ -19,7 +19,7 @@ public static partial class Geolocation static async Task PlatformLastKnownLocationAsync() { - await Permissions.EnsureGrantedAsync(); + await Permissions.EnsureGrantedOrRestrictedAsync(); var lm = Platform.LocationManager; AndroidLocation bestLocation = null; @@ -37,7 +37,7 @@ static async Task PlatformLastKnownLocationAsync() static async Task PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken) { - await Permissions.EnsureGrantedAsync(); + await Permissions.EnsureGrantedOrRestrictedAsync(); var locationManager = Platform.LocationManager; diff --git a/Xamarin.Essentials/Permissions/Permissions.shared.cs b/Xamarin.Essentials/Permissions/Permissions.shared.cs index e9a718827..dae9d236b 100644 --- a/Xamarin.Essentials/Permissions/Permissions.shared.cs +++ b/Xamarin.Essentials/Permissions/Permissions.shared.cs @@ -29,6 +29,15 @@ internal static async Task EnsureGrantedAsync() throw new PermissionException($"{typeof(TPermission).Name} permission was not granted: {status}"); } + internal static async Task EnsureGrantedOrRestrictedAsync() + where TPermission : BasePermission, new() + { + var status = await RequestAsync(); + + if (status != PermissionStatus.Granted && status != PermissionStatus.Restricted) + throw new PermissionException($"{typeof(TPermission).Name} permission was not granted or restricted: {status}"); + } + public abstract partial class BasePermission { [Preserve]