diff --git a/Xamarin.Essentials/Geolocation/Geolocation.android.cs b/Xamarin.Essentials/Geolocation/Geolocation.android.cs index cc196fe8c..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(true); + 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(true); + await Permissions.EnsureGrantedOrRestrictedAsync(); var locationManager = Platform.LocationManager; diff --git a/Xamarin.Essentials/Permissions/Permissions.shared.cs b/Xamarin.Essentials/Permissions/Permissions.shared.cs index dd30e9bc4..dae9d236b 100644 --- a/Xamarin.Essentials/Permissions/Permissions.shared.cs +++ b/Xamarin.Essentials/Permissions/Permissions.shared.cs @@ -20,15 +20,24 @@ internal static void EnsureDeclared() where TPermission : BasePermission, new() => new TPermission().EnsureDeclared(); - internal static async Task EnsureGrantedAsync(bool allowRestricted = false) + internal static async Task EnsureGrantedAsync() where TPermission : BasePermission, new() { var status = await RequestAsync(); - if (status != PermissionStatus.Granted || (allowRestricted && status == PermissionStatus.Restricted)) + if (status != PermissionStatus.Granted) 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]