From afff2edd00ef875f30fc546082c45ed722ad08c0 Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Tue, 1 Mar 2022 09:42:52 -0500 Subject: [PATCH 1/2] Fix #1972 - allow restricted permissions for geolocation on android --- Xamarin.Essentials/Geolocation/Geolocation.android.cs | 4 ++-- Xamarin.Essentials/Permissions/Permissions.shared.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Xamarin.Essentials/Geolocation/Geolocation.android.cs b/Xamarin.Essentials/Geolocation/Geolocation.android.cs index 8f0c33c99..cc196fe8c 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.EnsureGrantedAsync(true); 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.EnsureGrantedAsync(true); var locationManager = Platform.LocationManager; diff --git a/Xamarin.Essentials/Permissions/Permissions.shared.cs b/Xamarin.Essentials/Permissions/Permissions.shared.cs index e9a718827..dd30e9bc4 100644 --- a/Xamarin.Essentials/Permissions/Permissions.shared.cs +++ b/Xamarin.Essentials/Permissions/Permissions.shared.cs @@ -20,12 +20,12 @@ internal static void EnsureDeclared() where TPermission : BasePermission, new() => new TPermission().EnsureDeclared(); - internal static async Task EnsureGrantedAsync() + internal static async Task EnsureGrantedAsync(bool allowRestricted = false) where TPermission : BasePermission, new() { var status = await RequestAsync(); - if (status != PermissionStatus.Granted) + if (status != PermissionStatus.Granted || (allowRestricted && status == PermissionStatus.Restricted)) throw new PermissionException($"{typeof(TPermission).Name} permission was not granted: {status}"); } From a3006b0b7bdf06af7e33ff4fcf76e273a05ebd4c Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Mon, 4 Apr 2022 15:53:58 -0400 Subject: [PATCH 2/2] Rename method to EnsureGrantedOrRestrictedAsync --- .../Geolocation/Geolocation.android.cs | 4 ++-- .../Permissions/Permissions.shared.cs | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) 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]