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

アプリを初期化の際にイベントログ関連の情報を削除する #1071

Merged
3 commits merged into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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/Repository/EventLogRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public Task<List<EventLog>> GetLogsAsync(

public Task<bool> RemoveAsync(EventLog eventLog);

public Task RemoveAllAsync();

public Task AddEventNotifiedAsync(
long maxSize = AppConstants.EventLogMaxRequestSizeInBytes
);

public Task<bool> IsExist();
}

public class EventLogRepository : IEventLogRepository
Expand Down Expand Up @@ -269,6 +273,82 @@ private async Task AddEventNotifiedAsyncInternal(long maxSize)
};
await AddAsyncInternal(eventLog, maxSize);
}

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

await _semaphore.WaitAsync();

try
{
RemoveAllAsyncInternal();
}
finally
{
_semaphore.Release();

_loggerService.EndMethod();
}
}

private void RemoveAllAsyncInternal()
{
_loggerService.StartMethod();
try
{
if (Directory.Exists(_basePath))
{
Directory.Delete(_basePath, true);
}
}
finally
{
_loggerService.EndMethod();
}
}

public async Task<bool> IsExist()
{
_loggerService.StartMethod();

await _semaphore.WaitAsync();

try
{
return IsExistInternal();
}
finally
{
_semaphore.Release();

_loggerService.EndMethod();
}
}

private bool IsExistInternal()
{
_loggerService.StartMethod();
try
{
if (!Directory.Exists(_basePath))
{
return false;
}

string[] filesInDirectory = Directory.GetFiles(_basePath);
if (filesInDirectory.Length == 0)
{
return false;
}

return true;
}
finally
{
_loggerService.EndMethod();
}
}
}

public class EventContentExposureNotified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public interface ISendEventLogStateRepository
SendEventLogState GetSendEventLogState(EventType eventType);

bool IsExistNotSetEventType();

void RemoveAll();
}

public class SendEventLogStateRepository : ISendEventLogStateRepository
Expand Down Expand Up @@ -156,5 +158,10 @@ public bool IsExistNotSetEventType()
.Select(eventType => GetSendEventLogState(eventType))
.Any(state => state == SendEventLogState.NotSet);
}

public void RemoveAll()
{
_preferencesService.RemoveValue(PreferenceKey.SendEventLogState);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Covid19Radar.Services;
using Covid19Radar.Views;
using Prism.Navigation;
using Xamarin.CommunityToolkit.ObjectModel;
using Xamarin.Essentials;
using Xamarin.Forms;

Expand All @@ -34,6 +35,8 @@ public class DebugPageViewModel : ViewModelBase
private readonly ICloseApplicationService _closeApplicationService;
private readonly IServerConfigurationRepository _serverConfigurationRepository;
private readonly ILocalNotificationService _localNotificationService;
private readonly ISendEventLogStateRepository _sendEventLogStateRepository;
private readonly IEventLogRepository _eventLogRepository;

private string _debugInfo;
public string DebugInfo
Expand Down Expand Up @@ -154,7 +157,9 @@ public DebugPageViewModel(
AbsExposureDetectionBackgroundService exposureDetectionBackgroundService,
ICloseApplicationService closeApplicationService,
IServerConfigurationRepository serverConfigurationRepository,
ILocalNotificationService localNotificationService
ILocalNotificationService localNotificationService,
ISendEventLogStateRepository sendEventLogStateRepository,
IEventLogRepository eventLogRepository
) : base(navigationService)
{
Title = "Title:Debug";
Expand All @@ -167,6 +172,8 @@ ILocalNotificationService localNotificationService
_closeApplicationService = closeApplicationService;
_serverConfigurationRepository = serverConfigurationRepository;
_localNotificationService = localNotificationService;
_sendEventLogStateRepository = sendEventLogStateRepository;
_eventLogRepository = eventLogRepository;
}

public override async void Initialize(INavigationParameters parameters)
Expand Down Expand Up @@ -309,6 +316,19 @@ private string ConvertSha256(string text)
_ = await NavigationService.NavigateAsync("/" + nameof(ReAgreePrivacyPolicyPage), navigationParams);
});

public IAsyncCommand OnClickAddEventNotifiedIfNeeded => new AsyncCommand(async () =>
{
if (_sendEventLogStateRepository.GetSendEventLogState(EventType.ExposureNotified) == SendEventLogState.Enable)
{
await _eventLogRepository.AddEventNotifiedAsync();
}
});

public IAsyncCommand OnClickAddEventNotifiedForce => new AsyncCommand(async () =>
{
await _eventLogRepository.AddEventNotifiedAsync();
});

public Command OnClickQuit => new Command(() =>
{
Application.Current.Quit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public string AppVer
private readonly IUserDataRepository userDataRepository;
private readonly IExposureDataRepository exposureDataRepository;
private readonly IExposureConfigurationRepository exposureConfigurationRepository;
private readonly ISendEventLogStateRepository _sendEventLogStateRepository;
private readonly IEventLogRepository _eventLogRepository;
private readonly ILogFileService logFileService;
private readonly AbsExposureNotificationApiService exposureNotificationApiService;
private readonly ICloseApplicationService closeApplicationService;
Expand All @@ -46,6 +48,8 @@ public SettingsPageViewModel(
IUserDataRepository userDataRepository,
IExposureDataRepository exposureDataRepository,
IExposureConfigurationRepository exposureConfigurationRepository,
ISendEventLogStateRepository sendEventLogStateRepository,
IEventLogRepository eventLogRepository,
ILogFileService logFileService,
AbsExposureNotificationApiService exposureNotificationApiService,
ICloseApplicationService closeApplicationService,
Expand All @@ -58,11 +62,29 @@ IEssentialsService essentialsService
this.userDataRepository = userDataRepository;
this.exposureDataRepository = exposureDataRepository;
this.exposureConfigurationRepository = exposureConfigurationRepository;
_sendEventLogStateRepository = sendEventLogStateRepository;
_eventLogRepository = eventLogRepository;
this.logFileService = logFileService;
this.exposureNotificationApiService = exposureNotificationApiService;
this.closeApplicationService = closeApplicationService;
}

public override async void Initialize(INavigationParameters parameters)
{
loggerService.StartMethod();
base.Initialize(parameters);

try
{
bool isExistEventLogs = await _eventLogRepository.IsExist();
loggerService.Info($"isExistEventLogs: {isExistEventLogs}");
}
finally
{
loggerService.EndMethod();
}
}

public IAsyncCommand OnEventLogSend => new AsyncCommand(async () =>
{
loggerService.StartMethod();
Expand All @@ -73,7 +95,7 @@ IEssentialsService essentialsService
loggerService.EndMethod();
});

public ICommand OnChangeResetData => new Command(async () =>
public IAsyncCommand OnChangeResetData => new AsyncCommand(async () =>
{
loggerService.StartMethod();

Expand All @@ -100,6 +122,9 @@ IEssentialsService essentialsService
userDataRepository.RemoveAllUpdateDate();
userDataRepository.RemoveAllExposureNotificationStatus();

_sendEventLogStateRepository.RemoveAll();
await _eventLogRepository.RemoveAllAsync();

_ = logFileService.DeleteLogsDir();

UserDialogs.Instance.HideLoading();
Expand Down
10 changes: 9 additions & 1 deletion Covid19Radar/Covid19Radar/Views/Settings/DebugPage.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<?xml version="1.0" encoding="UTF-8" ?>
<!-- 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 http://mozilla.org/MPL/2.0/. -->
Expand Down Expand Up @@ -108,6 +108,14 @@
Command="{Binding Path=OnClickRemoveAllUpdateDate}"
Style="{StaticResource DefaultButton}"
Text="RemoveAllUpdateDate" />
<Button
Command="{Binding Path=OnClickAddEventNotifiedIfNeeded}"
Style="{StaticResource DefaultButton}"
Text="AddEventNotified(If needed)" />
<Button
Command="{Binding Path=OnClickAddEventNotifiedForce}"
Style="{StaticResource DefaultButton}"
Text="AddEventNotified(Force)" />
<Button
Command="{Binding Path=OnClickQuit}"
Style="{StaticResource DefaultButton}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,13 @@ public void IsExistNotSetEventTypeTest_Enabled()

Assert.False(sendEventLogStateRepository.IsExistNotSetEventType());
}

[Fact]
public void RemoveAllTest()
{
var sendEventLogStateRepository = CreateRepository();
sendEventLogStateRepository.RemoveAll();
mockPreferencesService.Verify(x => x.RemoveValue(PreferenceKey.SendEventLogState), Times.Once());
}
}
}
Loading