Skip to content
This repository has been archived by the owner on May 15, 2024. It is now read-only.

Commit

Permalink
Rename method to EnsureGrantedOrRestrictedAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Apr 4, 2022
1 parent afff2ed commit a3006b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Xamarin.Essentials/Geolocation/Geolocation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class Geolocation

static async Task<Location> PlatformLastKnownLocationAsync()
{
await Permissions.EnsureGrantedAsync<Permissions.LocationWhenInUse>(true);
await Permissions.EnsureGrantedOrRestrictedAsync<Permissions.LocationWhenInUse>();

var lm = Platform.LocationManager;
AndroidLocation bestLocation = null;
Expand All @@ -37,7 +37,7 @@ static async Task<Location> PlatformLastKnownLocationAsync()

static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
{
await Permissions.EnsureGrantedAsync<Permissions.LocationWhenInUse>(true);
await Permissions.EnsureGrantedOrRestrictedAsync<Permissions.LocationWhenInUse>();

var locationManager = Platform.LocationManager;

Expand Down
13 changes: 11 additions & 2 deletions Xamarin.Essentials/Permissions/Permissions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,24 @@ internal static void EnsureDeclared<TPermission>()
where TPermission : BasePermission, new() =>
new TPermission().EnsureDeclared();

internal static async Task EnsureGrantedAsync<TPermission>(bool allowRestricted = false)
internal static async Task EnsureGrantedAsync<TPermission>()
where TPermission : BasePermission, new()
{
var status = await RequestAsync<TPermission>();

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<TPermission>()
where TPermission : BasePermission, new()
{
var status = await RequestAsync<TPermission>();

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]
Expand Down

0 comments on commit a3006b0

Please sign in to comment.