Android LocationService refactoring.
This commit is contained in:
parent
04296e7a14
commit
1fa48de667
@ -1,10 +0,0 @@
|
||||
namespace eShopOnContainers.Core.Models.Location
|
||||
{
|
||||
public enum ActivityType
|
||||
{
|
||||
Other,
|
||||
AutomotiveNavigation,
|
||||
Fitness,
|
||||
OtherNavigation
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace eShopOnContainers.Core.Models.Location
|
||||
{
|
||||
public class Address
|
||||
{
|
||||
public double Latitude { get; set; }
|
||||
public double Longitude { get; set; }
|
||||
public string CountryCode { get; set; }
|
||||
public string CountryName { get; set; }
|
||||
public string FeatureName { get; set; }
|
||||
public string PostalCode { get; set; }
|
||||
public string SubLocality { get; set; }
|
||||
public string Thoroughfare { get; set; }
|
||||
public string SubThoroughfare { get; set; }
|
||||
public string Locality { get; set; }
|
||||
public string AdminArea { get; set; }
|
||||
public string SubAdminArea { get; set; }
|
||||
|
||||
public Address(Address address)
|
||||
{
|
||||
if (address == null)
|
||||
throw new ArgumentNullException(nameof(address));
|
||||
|
||||
CountryCode = address.CountryCode;
|
||||
CountryName = address.CountryName;
|
||||
Latitude = address.Latitude;
|
||||
Longitude = address.Longitude;
|
||||
FeatureName = address.FeatureName;
|
||||
PostalCode = address.PostalCode;
|
||||
SubLocality = address.SubLocality;
|
||||
Thoroughfare = address.Thoroughfare;
|
||||
SubThoroughfare = address.SubThoroughfare;
|
||||
SubAdminArea = address.SubAdminArea;
|
||||
AdminArea = address.AdminArea;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace eShopOnContainers.Core.Models.Location
|
||||
{
|
||||
public class ListenerSettings
|
||||
{
|
||||
public bool AllowBackgroundUpdates { get; set; } = false;
|
||||
public bool PauseLocationUpdatesAutomatically { get; set; } = true;
|
||||
public ActivityType ActivityType { get; set; } = ActivityType.Other;
|
||||
public bool ListenForSignificantChanges { get; set; } = false;
|
||||
public bool DeferLocationUpdates { get; set; } = false;
|
||||
public double? DeferralDistanceMeters { get; set; } = 500;
|
||||
public TimeSpan? DeferralTime { get; set; } = TimeSpan.FromMinutes(5);
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace eShopOnContainers.Core.Models.Location
|
||||
{
|
||||
public class PositionErrorEventArgs : EventArgs
|
||||
{
|
||||
public GeolocationError Error { get; private set; }
|
||||
|
||||
public PositionErrorEventArgs(GeolocationError error)
|
||||
{
|
||||
Error = error;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace eShopOnContainers.Core.Models.Location
|
||||
{
|
||||
public class PositionEventArgs : EventArgs
|
||||
{
|
||||
public Position Position { get; private set; }
|
||||
|
||||
public PositionEventArgs(Position position)
|
||||
{
|
||||
if (position == null)
|
||||
throw new ArgumentNullException("position");
|
||||
|
||||
Position = position;
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,6 @@ namespace eShopOnContainers.Core.Services.Location
|
||||
bool IsGeolocationAvailable { get; }
|
||||
bool IsGeolocationEnabled { get; }
|
||||
|
||||
Task<Position> GetPositionAsync(TimeSpan? timeout = null, CancellationToken? token = null, bool includeHeading = false);
|
||||
Task<Position> GetPositionAsync(TimeSpan? timeout = null, CancellationToken? token = null);
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ using FFImageLoading.Forms.Droid;
|
||||
using System;
|
||||
using Xamarin.Forms.Platform.Android;
|
||||
using eShopOnContainers.Droid.Services;
|
||||
using eShopOnContainers.Core.Services.Permissions;
|
||||
|
||||
namespace eShopOnContainers.Droid.Activities
|
||||
{
|
||||
@ -20,6 +21,8 @@ namespace eShopOnContainers.Droid.Activities
|
||||
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
|
||||
public class MainActivity : FormsAppCompatActivity
|
||||
{
|
||||
public static IPermissionsService PermissionsService = new PermissionsService();
|
||||
|
||||
protected override void OnCreate(Bundle bundle)
|
||||
{
|
||||
FormsAppCompatActivity.ToolbarResource = Resource.Layout.Toolbar;
|
||||
@ -57,7 +60,7 @@ namespace eShopOnContainers.Droid.Activities
|
||||
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
|
||||
{
|
||||
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
PermissionsService.Instance.OnRequestPermissionResult(requestCode, permissions, grantResults);
|
||||
((PermissionsService)PermissionsService).OnRequestPermissionResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -59,7 +59,6 @@ namespace eShopOnContainers.Droid.Extensions
|
||||
var isLessAccurate = accuracyDelta > 0;
|
||||
var isMoreAccurate = accuracyDelta < 0;
|
||||
var isSignificantlyLessAccurage = accuracyDelta > 200;
|
||||
|
||||
var isFromSameProvider = IsSameProvider(location.Provider, bestLocation.Provider);
|
||||
|
||||
if (isMoreAccurate)
|
||||
|
@ -26,7 +26,6 @@ namespace eShopOnContainers.Droid.Services
|
||||
{
|
||||
_desiredAccuracy = desiredAccuracy;
|
||||
_finishedCallback = finishedCallback;
|
||||
|
||||
_activeProviders = new HashSet<string>(activeProviders);
|
||||
|
||||
foreach (var provider in activeProviders)
|
||||
@ -36,7 +35,6 @@ namespace eShopOnContainers.Droid.Services
|
||||
_bestLocation = location;
|
||||
}
|
||||
|
||||
|
||||
if (timeout != Timeout.Infinite)
|
||||
_timer = new Timer(TimesUp, null, timeout, 0);
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ using eShopOnContainers.Droid.Services;
|
||||
using System;
|
||||
using eShopOnContainers.Core.Services.Location;
|
||||
using eShopOnContainers.Core.Models.Location;
|
||||
using eShopOnContainers.Core.Services.Permissions;
|
||||
using eShopOnContainers.Core.Models.Permissions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -12,6 +11,7 @@ using Android.Locations;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using Android.OS;
|
||||
using eShopOnContainers.Droid.Activities;
|
||||
|
||||
[assembly: Xamarin.Forms.Dependency(typeof(LocationServiceImplementation))]
|
||||
namespace eShopOnContainers.Droid.Services
|
||||
@ -45,20 +45,18 @@ namespace eShopOnContainers.Droid.Services
|
||||
|
||||
async Task<bool> CheckPermissionsAsync()
|
||||
{
|
||||
IPermissionsService permissionsService = new PermissionsService();
|
||||
var status = await permissionsService.CheckPermissionStatusAsync(Permission.Location);
|
||||
var status = await MainActivity.PermissionsService.CheckPermissionStatusAsync(Permission.Location);
|
||||
if (status != PermissionStatus.Granted)
|
||||
{
|
||||
Console.WriteLine("Currently do not have Location permissions, requesting permissions");
|
||||
Console.WriteLine("Currently do not have Location permissions, requesting permissions.");
|
||||
|
||||
var request = await permissionsService.RequestPermissionsAsync(Permission.Location);
|
||||
var request = await MainActivity.PermissionsService.RequestPermissionsAsync(Permission.Location);
|
||||
if (request[Permission.Location] != PermissionStatus.Granted)
|
||||
{
|
||||
Console.WriteLine("Location permission denied.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -72,7 +70,7 @@ namespace eShopOnContainers.Droid.Services
|
||||
|
||||
public bool IsGeolocationEnabled => Providers.Any(p => !IgnoredProviders.Contains(p) && Manager.IsProviderEnabled(p));
|
||||
|
||||
public async Task<Position> GetPositionAsync(TimeSpan? timeout = null, CancellationToken? cancelToken = null, bool includeHeading = false)
|
||||
public async Task<Position> GetPositionAsync(TimeSpan? timeout = null, CancellationToken? cancelToken = null)
|
||||
{
|
||||
var timeoutMilliseconds = timeout.HasValue ? (int)timeout.Value.TotalMilliseconds : Timeout.Infinite;
|
||||
if (timeoutMilliseconds <= 0 && timeoutMilliseconds != Timeout.Infinite)
|
||||
|
@ -20,8 +20,6 @@ namespace eShopOnContainers.Droid.Services
|
||||
Dictionary<Permission, PermissionStatus> _results;
|
||||
IList<string> _requestedPermissions;
|
||||
|
||||
internal static PermissionsService Instance;
|
||||
|
||||
#region Internal Implementation
|
||||
|
||||
List<string> GetManifestNames(Permission permission)
|
||||
@ -83,7 +81,7 @@ namespace eShopOnContainers.Droid.Services
|
||||
return false;
|
||||
}
|
||||
|
||||
static Permission GetPermissionForManifestName(string permission)
|
||||
Permission GetPermissionForManifestName(string permission)
|
||||
{
|
||||
switch (permission)
|
||||
{
|
||||
@ -95,7 +93,6 @@ namespace eShopOnContainers.Droid.Services
|
||||
return Permission.Unknown;
|
||||
}
|
||||
|
||||
|
||||
public void OnRequestPermissionResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
|
||||
{
|
||||
if (requestCode != _permissionCode)
|
||||
@ -135,8 +132,6 @@ namespace eShopOnContainers.Droid.Services
|
||||
|
||||
public Task<PermissionStatus> CheckPermissionStatusAsync(Permission permission)
|
||||
{
|
||||
Instance = this;
|
||||
|
||||
var names = GetManifestNames(permission);
|
||||
if (names == null)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user