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

Commit

Permalink
Merge pull request #165 from keiji/version_migration
Browse files Browse the repository at this point in the history
Add feature migrations mechanism
  • Loading branch information
keiji authored Aug 30, 2021
2 parents 2b7974e + 51e1613 commit 55277b0
Show file tree
Hide file tree
Showing 22 changed files with 1,389 additions and 184 deletions.
3 changes: 3 additions & 0 deletions Covid19Radar/Covid19Radar.Android/Covid19Radar.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@
<Compile Include="Services\ApplicationPropertyService.cs" />
<Compile Include="Services\LocalContentService.cs" />
<Compile Include="Services\LocalNotificationService.cs" />
<Compile Include="Services\Migration\MigrationProcessService.cs" />
<Compile Include="Services\Migration\WorkManagerMigrator.cs" />
</ItemGroup>
<ItemGroup>
<None Include="appcenter-pre-build.sh" />
Expand Down Expand Up @@ -454,6 +456,7 @@
<Folder Include="Services\Logs\" />
<Folder Include="Resources\drawable-zh-xxhdpi\" />
<Folder Include="Resources\drawable-zh-xhdpi\" />
<Folder Include="Services\Migration\" />
</ItemGroup>
<ItemGroup>
<AndroidResource Include="Resources\drawable-ja-xhdpi\HelpPage22.png" />
Expand Down
3 changes: 3 additions & 0 deletions Covid19Radar/Covid19Radar.Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Covid19Radar.Droid.Services.Logs;
using Covid19Radar.Services;
using Covid19Radar.Droid.Services;
using Covid19Radar.Services.Migration;
using Covid19Radar.Droid.Services.Migration;
using AndroidX.Work;
using Xamarin.ExposureNotifications;
using Java.Util.Concurrent;
Expand Down Expand Up @@ -63,6 +65,7 @@ private void RegisterPlatformTypes(IContainer container)
container.Register<IApplicationPropertyService, ApplicationPropertyService>(Reuse.Singleton);
container.Register<ILocalContentService, LocalContentService>(Reuse.Singleton);
container.Register<ILocalNotificationService, LocalNotificationService>(Reuse.Singleton);
container.Register<IMigrationProcessService, MigrationProccessService>(Reuse.Singleton);
#if USE_MOCK
container.Register<IDeviceVerifier, DeviceVerifierMock>(Reuse.Singleton);
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Threading.Tasks;
using Android.App;
using Android.Content;
using AndroidX.Work;
using CommonServiceLocator;
using Covid19Radar.Services.Logs;
using Covid19Radar.Services.Migration;
using Xamarin.Forms.Internals;

namespace Covid19Radar.Droid.Services.Migration
{
[Preserve]
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionMyPackageReplaced })]
public class AppVersionUpgradeReceiver : BroadcastReceiver
{
private readonly ILoggerService _loggerService = ServiceLocator.Current.GetInstance<ILoggerService>();

public override void OnReceive(Context context, Intent intent)
{
_loggerService.StartMethod();

if (intent.Action != Intent.ActionMyPackageReplaced)
{
_loggerService.EndMethod();
return;
}

WorkManager workManager = WorkManager.GetInstance(context);
var worker = new OneTimeWorkRequest.Builder(
Java.Lang.Class.FromType(typeof(UpgradeWorker))
).Build();
_ = workManager.Enqueue(worker);

_loggerService.EndMethod();
}
}

public class UpgradeWorker : Worker
{
private readonly ILoggerService _loggerService = ServiceLocator.Current.GetInstance<ILoggerService>();
private readonly IMigrationService _migrationService = ServiceLocator.Current.GetInstance<IMigrationService>();

public UpgradeWorker(
Context context,
WorkerParameters workerParams
) : base(context, workerParams)
{
// do nothing
}

public override Result DoWork()
{
_loggerService.StartMethod();

Task.Run(() => _migrationService.MigrateAsync()).GetAwaiter().GetResult();

_loggerService.EndMethod();

return Result.InvokeSuccess();
}
}

public class MigrationProccessService : IMigrationProcessService
{
private readonly ILoggerService _loggerService;

public MigrationProccessService(ILoggerService loggerService)
{
_loggerService = loggerService;
}

public async Task SetupAsync()
{
_loggerService.StartMethod();

await new WorkManagerMigrator(
_loggerService
).ExecuteAsync();

_loggerService.EndMethod();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using System.Threading.Tasks;
using AndroidX.Work;
using Covid19Radar.Services.Logs;

namespace Covid19Radar.Droid.Services.Migration
{
internal class WorkManagerMigrator
{
// Array of old work-name.
private static readonly string[] OldWorkNames = {
"exposurenotification",
"cocoaexposurenotification",
};

private readonly ILoggerService _loggerService;

public WorkManagerMigrator(
ILoggerService loggerService
)
{
_loggerService = loggerService;
}

internal Task ExecuteAsync()
{
_loggerService.StartMethod();

var workManager = WorkManager.GetInstance(Xamarin.Essentials.Platform.AppContext);
CancelOldWorks(workManager, OldWorkNames, _loggerService);

Xamarin.ExposureNotifications.ExposureNotification.Init();

_loggerService.EndMethod();

return Task.CompletedTask;
}

private static void CancelOldWorks(WorkManager workManager, string[] oldWorkNames, ILoggerService loggerService)
{
loggerService.StartMethod();

foreach (var oldWorkName in oldWorkNames)
{
workManager.CancelUniqueWork(oldWorkName);
loggerService.Info($"Worker {oldWorkName} is canceled.");
}

loggerService.EndMethod();
}
}
}
3 changes: 3 additions & 0 deletions Covid19Radar/Covid19Radar.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
using Covid19Radar.Common;
using Covid19Radar.iOS.Services;
using Covid19Radar.iOS.Services.Logs;
using Covid19Radar.iOS.Services.Migration;
using Covid19Radar.Services;
using Covid19Radar.Services.Logs;
using Covid19Radar.Services.Migration;
using DryIoc;
using Foundation;
using UIKit;
Expand Down Expand Up @@ -72,6 +74,7 @@ private void RegisterPlatformTypes(IContainer container)
container.Register<IApplicationPropertyService, ApplicationPropertyService>(Reuse.Singleton);
container.Register<ILocalContentService, LocalContentService>(Reuse.Singleton);
container.Register<ILocalNotificationService, LocalNotificationService>(Reuse.Singleton);
container.Register<IMigrationProcessService, MigrationProcessService>(Reuse.Singleton);
#if USE_MOCK
container.Register<IDeviceVerifier, DeviceVerifierMock>(Reuse.Singleton);
#else
Expand Down
2 changes: 2 additions & 0 deletions Covid19Radar/Covid19Radar.iOS/Covid19Radar.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@
<Compile Include="Services\ApplicationPropertyService.cs" />
<Compile Include="Services\LocalContentService.cs" />
<Compile Include="Services\LocalNotificationService.cs" />
<Compile Include="Services\Migration\MigrationProcessService.cs" />
</ItemGroup>
<ItemGroup>
<ImageAsset Include="Assets.xcassets\Contents.json">
Expand Down Expand Up @@ -541,6 +542,7 @@
<Folder Include="Services\Logs\" />
<Folder Include="Renderers\" />
<Folder Include="Resources\zh-Hans.lproj\" />
<Folder Include="Services\Migration\" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
<ProjectExtensions>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

using Covid19Radar.Services.Migration;

namespace Covid19Radar.iOS.Services.Migration
{
public class MigrationProcessService : IMigrationProcessService
{
// Currently, It is not needed that iOS platform specific migration process.
}
}
2 changes: 2 additions & 0 deletions Covid19Radar/Covid19Radar/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using CommonServiceLocator;
using Covid19Radar.Common;
using Covid19Radar.Services.Migration;
using Xamarin.ExposureNotifications;

/*
Expand Down Expand Up @@ -187,6 +188,7 @@ private static void RegisterCommonTypes(IContainer container)
container.Register<IStorageService, StorageService>(Reuse.Singleton);
#endif
container.Register<ISecureStorageService, SecureStorageService>(Reuse.Singleton);
container.Register<IMigrationService, MigrationService>(Reuse.Singleton);
}

protected override void OnStart()
Expand Down
1 change: 1 addition & 0 deletions Covid19Radar/Covid19Radar/Common/PreferenceKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Covid19Radar.Common
public static class PreferenceKey
{
// for preferences
public static string AppVersion = "AppVersion";
public static string StartDateTime = "StartDateTime";
public static string LastProcessTekTimestamp = "LastProcessTekTimestamp";
public static string ExposureNotificationConfiguration = "ExposureNotificationConfiguration";
Expand Down
1 change: 1 addition & 0 deletions Covid19Radar/Covid19Radar/Covid19Radar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<ItemGroup>
<Folder Include="Services\Logs\" />
<Folder Include="Controls\" />
<Folder Include="Services\Migration\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Xamarin.ExposureNotification\Xamarin.ExposureNotification.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
using System.Threading.Tasks;
using Acr.UserDialogs;
using CommonServiceLocator;
using Covid19Radar.Common;
using Covid19Radar.Model;
using Covid19Radar.Resources;
using Covid19Radar.Services.Logs;
using Covid19Radar.Services.Migration;
using Newtonsoft.Json;
using Xamarin.Essentials;
using Xamarin.ExposureNotifications;
Expand All @@ -29,6 +29,7 @@ public class ExposureNotificationHandler : IExposureNotificationHandler
private IHttpDataService HttpDataService => ServiceLocator.Current.GetInstance<IHttpDataService>();
private IExposureNotificationService ExposureNotificationService => ServiceLocator.Current.GetInstance<IExposureNotificationService>();
private IUserDataService UserDataService => ServiceLocator.Current.GetInstance<IUserDataService>();
private IMigrationService MigrationService => ServiceLocator.Current.GetInstance<IMigrationService>();
private readonly IDeviceVerifier DeviceVerifier = ServiceLocator.Current.GetInstance<IDeviceVerifier>();
private ILocalNotificationService LocalNotificationService => ServiceLocator.Current.GetInstance<ILocalNotificationService>();

Expand Down Expand Up @@ -158,9 +159,7 @@ public async Task FetchExposureKeyBatchFilesFromServerAsync(Func<IEnumerable<str

try
{
// Migrate from UserData.
// Since it may be executed during the migration when the application starts, execute it here as well.
await UserDataService.Migrate();
await MigrationService.MigrateAsync();

foreach (var serverRegion in AppSettings.Instance.SupportedRegions)
{
Expand Down
47 changes: 0 additions & 47 deletions Covid19Radar/Covid19Radar/Services/ExposureNotificationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@
using System.Net.Http;
using System.Threading.Tasks;
using Xamarin.ExposureNotifications;
using Xamarin.Forms;

namespace Covid19Radar.Services
{
public interface IExposureNotificationService
{
Task MigrateFromUserData(UserDataModel userData);

Configuration GetConfiguration();
void RemoveConfiguration();

Expand Down Expand Up @@ -52,7 +49,6 @@ public class ExposureNotificationService : IExposureNotificationService
private readonly IHttpClientService httpClientService;
private readonly ISecureStorageService secureStorageService;
private readonly IPreferencesService preferencesService;
private readonly IApplicationPropertyService applicationPropertyService;

public string CurrentStatusMessage { get; set; } = "初期状態";
public Status ExposureNotificationStatus { get; set; }
Expand All @@ -63,53 +59,10 @@ public ExposureNotificationService(ILoggerService loggerService, IHttpClientServ
this.httpClientService = httpClientService;
this.secureStorageService = secureStorageService;
this.preferencesService = preferencesService;
this.applicationPropertyService = applicationPropertyService;

_ = GetExposureNotificationConfig();
}

public async Task MigrateFromUserData(UserDataModel userData)
{
loggerService.StartMethod();

const string ConfigurationPropertyKey = "ExposureNotificationConfigration";

if (userData.LastProcessTekTimestamp != null && userData.LastProcessTekTimestamp.Count > 0)
{
var stringValue = JsonConvert.SerializeObject(userData.LastProcessTekTimestamp);
preferencesService.SetValue(PreferenceKey.LastProcessTekTimestamp, stringValue);
userData.LastProcessTekTimestamp.Clear();
loggerService.Info("Migrated LastProcessTekTimestamp");
}

if (applicationPropertyService.ContainsKey(ConfigurationPropertyKey))
{
var configuration = applicationPropertyService.GetProperties(ConfigurationPropertyKey) as string;
if (!string.IsNullOrEmpty(configuration))
{
preferencesService.SetValue(PreferenceKey.ExposureNotificationConfiguration, configuration);
}
await applicationPropertyService.Remove(ConfigurationPropertyKey);
loggerService.Info("Migrated ExposureNotificationConfiguration");
}

if (userData.ExposureInformation != null)
{
secureStorageService.SetValue(PreferenceKey.ExposureInformation, JsonConvert.SerializeObject(userData.ExposureInformation));
userData.ExposureInformation = null;
loggerService.Info("Migrated ExposureInformation");
}

if (userData.ExposureSummary != null)
{
secureStorageService.SetValue(PreferenceKey.ExposureSummary, JsonConvert.SerializeObject(userData.ExposureSummary));
userData.ExposureSummary = null;
loggerService.Info("Migrated ExposureSummary");
}

loggerService.EndMethod();
}

private async Task GetExposureNotificationConfig()
{
loggerService.StartMethod();
Expand Down
Loading

0 comments on commit 55277b0

Please sign in to comment.