2023-04-05 15:02:10 +02:00
|
|
|
using System;
|
2021-08-28 10:04:10 +02:00
|
|
|
using System.Threading;
|
2021-05-13 20:03:07 +02:00
|
|
|
using System.Threading.Tasks;
|
2022-08-30 15:42:25 +02:00
|
|
|
using Serilog;
|
2021-05-13 20:03:07 +02:00
|
|
|
using TINK.Model.Device;
|
|
|
|
using Xamarin.Essentials;
|
|
|
|
|
2022-04-10 17:38:34 +02:00
|
|
|
namespace TINK.Services.Geolocation
|
2021-05-13 20:03:07 +02:00
|
|
|
{
|
2023-04-05 15:02:10 +02:00
|
|
|
public abstract class GeolocationService : IGeolocationService
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
|
|
|
/// <summary> Timeout for geolocation request operations.</summary>
|
|
|
|
private const int GEOLOCATIONREQUEST_TIMEOUT_MS = 5000;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
private IGeolodationDependent Dependent { get; }
|
2022-08-30 15:42:25 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
private GeolocationAccuracy Accuracy { get; }
|
2022-04-10 17:38:34 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public GeolocationService(
|
|
|
|
IGeolodationDependent dependent,
|
|
|
|
GeolocationAccuracy accuracy = GeolocationAccuracy.Default)
|
|
|
|
{
|
|
|
|
Dependent = dependent;
|
|
|
|
Accuracy = accuracy;
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public bool IsSimulation => false;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public bool IsGeolcationEnabled => Dependent.IsGeolcationEnabled;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Gets the current location.</summary>
|
|
|
|
/// <param name="cancellationToken">Token to cancel request for geolocation.</param>
|
|
|
|
/// <param name="timeStamp">Time when geolocation is of interest. Is used to determine whether cached geoloation can be used or not.</param>
|
2023-04-05 15:02:10 +02:00
|
|
|
public async Task<IGeolocation> GetAsync(CancellationToken? cancellationToken = null, DateTime? timeStamp = null)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var request = new GeolocationRequest(Accuracy, TimeSpan.FromMilliseconds(GEOLOCATIONREQUEST_TIMEOUT_MS));
|
|
|
|
return cancellationToken.HasValue
|
2023-04-05 15:02:10 +02:00
|
|
|
? (await Xamarin.Essentials.Geolocation.GetLocationAsync(request, cancellationToken.Value)).ToGeolocation()
|
|
|
|
: (await Xamarin.Essentials.Geolocation.GetLocationAsync(request)).ToGeolocation();
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
catch (FeatureNotSupportedException fnsEx)
|
|
|
|
{
|
|
|
|
// Handle not supported on device exception
|
|
|
|
Log.ForContext<GeolocationService>().Error("Retrieving Geolocation not supported on device. {Exception}", fnsEx);
|
|
|
|
throw new Exception("Abfrage Standort nicht möglich auf Gerät.", fnsEx);
|
|
|
|
}
|
|
|
|
catch (FeatureNotEnabledException fneEx)
|
|
|
|
{
|
|
|
|
// Handle not enabled on device exception
|
|
|
|
Log.ForContext<GeolocationService>().Error("Retrieving Geolocation not enabled on device. {Exception}", fneEx);
|
|
|
|
throw new Exception("Abfrage Standort nicht aktiviert auf Gerät.", fneEx);
|
|
|
|
}
|
|
|
|
catch (PermissionException pEx)
|
|
|
|
{
|
|
|
|
// Handle permission exception
|
|
|
|
Log.ForContext<GeolocationService>().Error("Retrieving Geolocation not permitted on device. {Exception}", pEx);
|
|
|
|
throw new Exception("Berechtiung für Abfrage Standort nicht erteilt für App.", pEx);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
// Unable to get location
|
|
|
|
Log.ForContext<GeolocationService>().Error("Retrieving Geolocation failed. {Exception}", ex);
|
|
|
|
throw new Exception("Abfrage Standort fehlgeschlagen.", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
}
|