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

Commit 70ae013

Browse files
authored
Merge pull request #1976 from aritchie/issue-1972
Fix #1972 - allow restricted permissions for geolocation on android
2 parents 101971f + 1d09035 commit 70ae013

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Xamarin.Essentials/Geolocation/Geolocation.android.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static partial class Geolocation
1919

2020
static async Task<Location> PlatformLastKnownLocationAsync()
2121
{
22-
await Permissions.EnsureGrantedAsync<Permissions.LocationWhenInUse>();
22+
await Permissions.EnsureGrantedOrRestrictedAsync<Permissions.LocationWhenInUse>();
2323

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

3838
static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
3939
{
40-
await Permissions.EnsureGrantedAsync<Permissions.LocationWhenInUse>();
40+
await Permissions.EnsureGrantedOrRestrictedAsync<Permissions.LocationWhenInUse>();
4141

4242
var locationManager = Platform.LocationManager;
4343

Xamarin.Essentials/Permissions/Permissions.shared.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ internal static async Task EnsureGrantedAsync<TPermission>()
2929
throw new PermissionException($"{typeof(TPermission).Name} permission was not granted: {status}");
3030
}
3131

32+
internal static async Task EnsureGrantedOrRestrictedAsync<TPermission>()
33+
where TPermission : BasePermission, new()
34+
{
35+
var status = await RequestAsync<TPermission>();
36+
37+
if (status != PermissionStatus.Granted && status != PermissionStatus.Restricted)
38+
throw new PermissionException($"{typeof(TPermission).Name} permission was not granted or restricted: {status}");
39+
}
40+
3241
public abstract partial class BasePermission
3342
{
3443
[Preserve]

0 commit comments

Comments
 (0)