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

Fix #1972 - allow restricted permissions for geolocation on android #1976

Merged
merged 3 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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