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 IsGeolocationAvailable { get; }
|
||||||
bool IsGeolocationEnabled { 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 System;
|
||||||
using Xamarin.Forms.Platform.Android;
|
using Xamarin.Forms.Platform.Android;
|
||||||
using eShopOnContainers.Droid.Services;
|
using eShopOnContainers.Droid.Services;
|
||||||
|
using eShopOnContainers.Core.Services.Permissions;
|
||||||
|
|
||||||
namespace eShopOnContainers.Droid.Activities
|
namespace eShopOnContainers.Droid.Activities
|
||||||
{
|
{
|
||||||
@ -20,6 +21,8 @@ namespace eShopOnContainers.Droid.Activities
|
|||||||
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
|
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
|
||||||
public class MainActivity : FormsAppCompatActivity
|
public class MainActivity : FormsAppCompatActivity
|
||||||
{
|
{
|
||||||
|
public static IPermissionsService PermissionsService = new PermissionsService();
|
||||||
|
|
||||||
protected override void OnCreate(Bundle bundle)
|
protected override void OnCreate(Bundle bundle)
|
||||||
{
|
{
|
||||||
FormsAppCompatActivity.ToolbarResource = Resource.Layout.Toolbar;
|
FormsAppCompatActivity.ToolbarResource = Resource.Layout.Toolbar;
|
||||||
@ -57,7 +60,7 @@ namespace eShopOnContainers.Droid.Activities
|
|||||||
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
|
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
|
||||||
{
|
{
|
||||||
base.OnRequestPermissionsResult(requestCode, permissions, 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 isLessAccurate = accuracyDelta > 0;
|
||||||
var isMoreAccurate = accuracyDelta < 0;
|
var isMoreAccurate = accuracyDelta < 0;
|
||||||
var isSignificantlyLessAccurage = accuracyDelta > 200;
|
var isSignificantlyLessAccurage = accuracyDelta > 200;
|
||||||
|
|
||||||
var isFromSameProvider = IsSameProvider(location.Provider, bestLocation.Provider);
|
var isFromSameProvider = IsSameProvider(location.Provider, bestLocation.Provider);
|
||||||
|
|
||||||
if (isMoreAccurate)
|
if (isMoreAccurate)
|
||||||
|
@ -26,7 +26,6 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
{
|
{
|
||||||
_desiredAccuracy = desiredAccuracy;
|
_desiredAccuracy = desiredAccuracy;
|
||||||
_finishedCallback = finishedCallback;
|
_finishedCallback = finishedCallback;
|
||||||
|
|
||||||
_activeProviders = new HashSet<string>(activeProviders);
|
_activeProviders = new HashSet<string>(activeProviders);
|
||||||
|
|
||||||
foreach (var provider in activeProviders)
|
foreach (var provider in activeProviders)
|
||||||
@ -36,7 +35,6 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
_bestLocation = location;
|
_bestLocation = location;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (timeout != Timeout.Infinite)
|
if (timeout != Timeout.Infinite)
|
||||||
_timer = new Timer(TimesUp, null, timeout, 0);
|
_timer = new Timer(TimesUp, null, timeout, 0);
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ using eShopOnContainers.Droid.Services;
|
|||||||
using System;
|
using System;
|
||||||
using eShopOnContainers.Core.Services.Location;
|
using eShopOnContainers.Core.Services.Location;
|
||||||
using eShopOnContainers.Core.Models.Location;
|
using eShopOnContainers.Core.Models.Location;
|
||||||
using eShopOnContainers.Core.Services.Permissions;
|
|
||||||
using eShopOnContainers.Core.Models.Permissions;
|
using eShopOnContainers.Core.Models.Permissions;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -12,6 +11,7 @@ using Android.Locations;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Android.OS;
|
using Android.OS;
|
||||||
|
using eShopOnContainers.Droid.Activities;
|
||||||
|
|
||||||
[assembly: Xamarin.Forms.Dependency(typeof(LocationServiceImplementation))]
|
[assembly: Xamarin.Forms.Dependency(typeof(LocationServiceImplementation))]
|
||||||
namespace eShopOnContainers.Droid.Services
|
namespace eShopOnContainers.Droid.Services
|
||||||
@ -45,20 +45,18 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
|
|
||||||
async Task<bool> CheckPermissionsAsync()
|
async Task<bool> CheckPermissionsAsync()
|
||||||
{
|
{
|
||||||
IPermissionsService permissionsService = new PermissionsService();
|
var status = await MainActivity.PermissionsService.CheckPermissionStatusAsync(Permission.Location);
|
||||||
var status = await permissionsService.CheckPermissionStatusAsync(Permission.Location);
|
|
||||||
if (status != PermissionStatus.Granted)
|
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)
|
if (request[Permission.Location] != PermissionStatus.Granted)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Location permission denied.");
|
Console.WriteLine("Location permission denied.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +70,7 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
|
|
||||||
public bool IsGeolocationEnabled => Providers.Any(p => !IgnoredProviders.Contains(p) && Manager.IsProviderEnabled(p));
|
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;
|
var timeoutMilliseconds = timeout.HasValue ? (int)timeout.Value.TotalMilliseconds : Timeout.Infinite;
|
||||||
if (timeoutMilliseconds <= 0 && timeoutMilliseconds != Timeout.Infinite)
|
if (timeoutMilliseconds <= 0 && timeoutMilliseconds != Timeout.Infinite)
|
||||||
|
@ -20,8 +20,6 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
Dictionary<Permission, PermissionStatus> _results;
|
Dictionary<Permission, PermissionStatus> _results;
|
||||||
IList<string> _requestedPermissions;
|
IList<string> _requestedPermissions;
|
||||||
|
|
||||||
internal static PermissionsService Instance;
|
|
||||||
|
|
||||||
#region Internal Implementation
|
#region Internal Implementation
|
||||||
|
|
||||||
List<string> GetManifestNames(Permission permission)
|
List<string> GetManifestNames(Permission permission)
|
||||||
@ -83,7 +81,7 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Permission GetPermissionForManifestName(string permission)
|
Permission GetPermissionForManifestName(string permission)
|
||||||
{
|
{
|
||||||
switch (permission)
|
switch (permission)
|
||||||
{
|
{
|
||||||
@ -95,7 +93,6 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
return Permission.Unknown;
|
return Permission.Unknown;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void OnRequestPermissionResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
|
public void OnRequestPermissionResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
|
||||||
{
|
{
|
||||||
if (requestCode != _permissionCode)
|
if (requestCode != _permissionCode)
|
||||||
@ -135,8 +132,6 @@ namespace eShopOnContainers.Droid.Services
|
|||||||
|
|
||||||
public Task<PermissionStatus> CheckPermissionStatusAsync(Permission permission)
|
public Task<PermissionStatus> CheckPermissionStatusAsync(Permission permission)
|
||||||
{
|
{
|
||||||
Instance = this;
|
|
||||||
|
|
||||||
var names = GetManifestNames(permission);
|
var names = GetManifestNames(permission);
|
||||||
if (names == null)
|
if (names == null)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user