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

Commit

Permalink
GH-476: Put in checks to see if location services are enabled. (#488)
Browse files Browse the repository at this point in the history
* Put in checks to see if location services are enabled.

* iOS check to see if location services are enabled.
  • Loading branch information
jamesmontemagno authored and Redth committed Aug 24, 2018
1 parent a4f59ed commit c10ccc1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Xamarin.Essentials/Geolocation/Geolocation.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Xamarin.Essentials
public static partial class Geolocation
{
const long twoMinutes = 120000;
static readonly string[] ignoredProviders = new string[] { LocationManager.PassiveProvider, "local_database" };

static async Task<Location> PlatformLastKnownLocationAsync()
{
Expand All @@ -40,6 +41,12 @@ static async Task<Location> PlatformLocationAsync(GeolocationRequest request, Ca

var locationManager = Platform.LocationManager;

var enabledProviders = locationManager.GetProviders(true);
var hasProviders = enabledProviders.Any(p => !ignoredProviders.Contains(p));

if (!hasProviders)
throw new FeatureNotEnabledException("Location services are not enabled on device.");

// get the best possible provider for the requested accuracy
var providerInfo = GetBestProvider(locationManager, request.DesiredAccuracy);

Expand Down
9 changes: 6 additions & 3 deletions Xamarin.Essentials/Geolocation/Geolocation.ios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace Xamarin.Essentials
{
public static partial class Geolocation
{
internal static bool IsSupported
=> CLLocationManager.LocationServicesEnabled;

static async Task<Location> PlatformLastKnownLocationAsync()
{
if (!CLLocationManager.LocationServicesEnabled)
throw new FeatureNotEnabledException("Location services are not enabled on device.");

await Permissions.RequireAsync(PermissionType.LocationWhenInUse);

var manager = new CLLocationManager();
Expand All @@ -24,6 +24,9 @@ static async Task<Location> PlatformLastKnownLocationAsync()

static async Task<Location> PlatformLocationAsync(GeolocationRequest request, CancellationToken cancellationToken)
{
if (!CLLocationManager.LocationServicesEnabled)
throw new FeatureNotEnabledException("Location services are not enabled on device.");

await Permissions.RequireAsync(PermissionType.LocationWhenInUse);

// the location manager requires an active run loop
Expand Down
12 changes: 12 additions & 0 deletions Xamarin.Essentials/Geolocation/Geolocation.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,23 @@ static async Task<Location> PlatformLocationAsync(GeolocationRequest request, Ca
DesiredAccuracyInMeters = request.PlatformDesiredAccuracy
};

CheckStatus(geolocator.LocationStatus);

cancellationToken = Utils.TimeoutToken(cancellationToken, request.Timeout);

var location = await geolocator.GetGeopositionAsync().AsTask(cancellationToken);

return location?.Coordinate?.ToLocation();

void CheckStatus(PositionStatus status)
{
switch (status)
{
case PositionStatus.Disabled:
case PositionStatus.NotAvailable:
throw new FeatureNotEnabledException("Location services are not enabled on device.");
}
}
}
}
}
17 changes: 17 additions & 0 deletions Xamarin.Essentials/Types/Shared/Exceptions.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public FeatureNotSupportedException(string message, Exception innerException)
{
}
}

public class FeatureNotEnabledException : InvalidOperationException
{
public FeatureNotEnabledException()
{
}

public FeatureNotEnabledException(string message)
: base(message)
{
}

public FeatureNotEnabledException(string message, Exception innerException)
: base(message, innerException)
{
}
}
}

0 comments on commit c10ccc1

Please sign in to comment.