This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #165 from keiji/version_migration
Add feature migrations mechanism
- Loading branch information
Showing
22 changed files
with
1,389 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
Covid19Radar/Covid19Radar.Android/Services/Migration/MigrationProcessService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
Covid19Radar/Covid19Radar.Android/Services/Migration/WorkManagerMigrator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
Covid19Radar/Covid19Radar.iOS/Services/Migration/MigrationProcessService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.