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

Commit

Permalink
Merge pull request #1976 from aritchie/issue-1972
Browse files Browse the repository at this point in the history
Fix #1972 - allow restricted permissions for geolocation on android
  • Loading branch information
jfversluis authored Apr 5, 2022
2 parents 101971f + 1d09035 commit 70ae013
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 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>();
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>();
await Permissions.EnsureGrantedOrRestrictedAsync<Permissions.LocationWhenInUse>();

var locationManager = Platform.LocationManager;

Expand Down
9 changes: 9 additions & 0 deletions Xamarin.Essentials/Permissions/Permissions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ internal static async Task EnsureGrantedAsync<TPermission>()
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 70ae013

Please sign in to comment.