Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Fix issue that background task fail on Android. #30

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Covid19Radar/Covid19Radar.Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
using Android.Widget;
using Android.Support.V4.App;
using Covid19Radar.Common;
using Covid19Radar.Services.Logs;
using Covid19Radar.Droid.Services.Logs;
using DryIoc;
using CommonServiceLocator;
using Covid19Radar.Services;
using Covid19Radar.Droid.Services;

namespace Covid19Radar.Droid
{
Expand All @@ -21,8 +27,82 @@ namespace Covid19Radar.Droid
#endif
public class MainApplication : Application
{

public MainApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
{
}

public override void OnCreate()
{
base.OnCreate();

InitializeServiceLocator();
}

/**
* Initialize IOC container what used by background worker.
*/
private void InitializeServiceLocator()
{
var container = new Container();

RegisterPlatformTypes(container);
RegisterTypes(container);

var serviceLocator = new ContainerServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => serviceLocator);
}

private void RegisterPlatformTypes(Container container)
{
container.Register<ILogPathDependencyService, LogPathServiceAndroid>(Reuse.Singleton);
container.Register<ISecureStorageDependencyService, SecureStorageServiceAndroid>(Reuse.Singleton);
container.Register<IPreferencesService, PreferencesService>(Reuse.Singleton);
}

private void RegisterTypes(Container container)
{
container.Register<ILoggerService, LoggerService>(Reuse.Singleton);
container.Register<ILogFileService, LogFileService>(Reuse.Singleton);
container.Register<ILogPathService, LogPathService>(Reuse.Singleton);
container.Register<ILogPeriodicDeleteService, LogPeriodicDeleteService>(Reuse.Singleton);
container.Register<ILogUploadService, LogUploadService>(Reuse.Singleton);
container.Register<IEssentialsService, EssentialsService>(Reuse.Singleton);
container.Register<IUserDataService, UserDataService>(Reuse.Singleton);
container.Register<IExposureNotificationService, ExposureNotificationService>(Reuse.Singleton);
container.Register<ITermsUpdateService, TermsUpdateService>(Reuse.Singleton);
container.Register<IApplicationPropertyService, ApplicationPropertyService>(Reuse.Singleton);
container.Register<IHttpClientService, HttpClientService>(Reuse.Singleton);
#if USE_MOCK
container.Register<IHttpDataService, HttpDataServiceMock>(Reuse.Singleton);
container.Register<IStorageService, StorageServiceMock>(Reuse.Singleton);
#else
container.Register<IHttpDataService, HttpDataService>(Reuse.Singleton);
container.Register<IStorageService, StorageService>(Reuse.Singleton);
#endif
container.Register<ISecureStorageService, SecureStorageService>(Reuse.Singleton);
}
}

public class ContainerServiceLocator : ServiceLocatorImplBase
{
private readonly Container _container;

public ContainerServiceLocator(Container container)
{
_container = container;
}

protected override object DoGetInstance(Type serviceType, string key)
{
if (_container == null) throw new ObjectDisposedException("container");
return _container.Resolve(serviceType, key);
}

protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
{
if (_container == null) throw new ObjectDisposedException("container");
return _container.ResolveMany(serviceType);
}
keiji marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Acr.UserDialogs;
using CommonServiceLocator;
using Covid19Radar.Common;
using Covid19Radar.Model;
using Covid19Radar.Resources;
Expand All @@ -20,9 +21,9 @@ namespace Covid19Radar.Services
[Xamarin.Forms.Internals.Preserve] // Ensure this isn't linked out
public class ExposureNotificationHandler : IExposureNotificationHandler
{
private ILoggerService LoggerService => DependencyService.Resolve<ILoggerService>();
private IHttpDataService HttpDataService => DependencyService.Resolve<IHttpDataService>();
private IExposureNotificationService ExposureNotificationService => DependencyService.Resolve<IExposureNotificationService>();
private ILoggerService LoggerService => ServiceLocator.Current.GetInstance<ILoggerService>();
private IHttpDataService HttpDataService => ServiceLocator.Current.GetInstance<IHttpDataService>();
private IExposureNotificationService ExposureNotificationService => ServiceLocator.Current.GetInstance<IExposureNotificationService>();

public ExposureNotificationHandler()
{
Expand Down Expand Up @@ -143,7 +144,7 @@ public async Task FetchExposureKeyBatchFilesFromServerAsync(Func<IEnumerable<str
{
// Migrate from UserData.
// Since it may be executed during the migration when the application starts, execute it here as well.
var userDataService = DependencyService.Resolve<IUserDataService>();
var userDataService = ServiceLocator.Current.GetInstance<IUserDataService>();
await userDataService.Migrate();

foreach (var serverRegion in AppSettings.Instance.SupportedRegions)
Expand Down
2 changes: 1 addition & 1 deletion Covid19Radar/Covid19Radar/Services/HttpDataServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Covid19Radar.Services
{
class HttpDataServiceMock : IHttpDataService
public class HttpDataServiceMock : IHttpDataService
{
public Task MigrateFromUserData(UserDataModel userData)
{
Expand Down