Skip to content

Commit 8ef0ad9

Browse files
committed
Cleanup background support and add logging
Also add timestamp to logs
1 parent 7e9dde9 commit 8ef0ad9

File tree

3 files changed

+31
-59
lines changed

3 files changed

+31
-59
lines changed

Signal-Windows.Lib/SignalLogging.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public bool IsEnabled(LogLevel logLevel)
4949

5050
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
5151
{
52-
Debug.WriteLine(string.Format("[{0}] [{1}] ", logLevel, ClassName) + formatter(state, exception));
52+
Debug.WriteLine(string.Format("{0:s} [{1}] [{2}] ", DateTime.UtcNow, logLevel, ClassName) + formatter(state, exception));
5353
}
5454
}
5555

@@ -118,7 +118,7 @@ public void Log(string line)
118118
{
119119
try
120120
{
121-
Writer.WriteLine($"[{Prefix}] {line}");
121+
Writer.WriteLine($"{DateTime.UtcNow.ToString("s")} [{Prefix}] {line}");
122122
Writer.Flush();
123123
}
124124
catch(Exception e)

Signal-Windows.RC/SignalBackgroundTask.cs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
using System.Text;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using libsignalservice;
9+
using Microsoft.Extensions.Logging;
810
using Microsoft.Toolkit.Uwp.Notifications;
911
using Signal_Windows.Lib;
1012
using Signal_Windows.Lib.Events;
1113
using Signal_Windows.Models;
14+
using Signal_Windows.Storage;
1215
using Windows.ApplicationModel.Background;
1316
using Windows.UI.Notifications;
1417

@@ -19,6 +22,8 @@ public sealed class SignalBackgroundTask : IBackgroundTask
1922
private const string TaskName = "SignalMessageBackgroundTask";
2023
private const string SemaphoreName = "Signal_Windows_Semaphore";
2124

25+
private readonly ILogger Logger = LibsignalLogging.CreateLogger<SignalBackgroundTask>();
26+
2227
private BackgroundTaskDeferral deferral;
2328

2429
private Semaphore semaphore;
@@ -29,16 +34,17 @@ public sealed class SignalBackgroundTask : IBackgroundTask
2934

3035
public async void Run(IBackgroundTaskInstance taskInstance)
3136
{
37+
taskStartTime = DateTime.Now;
38+
taskEndTime = taskStartTime + TimeSpan.FromSeconds(25);
3239
taskInstance.Canceled += TaskInstance_Canceled;
3340
deferral = taskInstance.GetDeferral();
41+
SignalLogging.SetupLogging(false);
3442
toastNotifier = ToastNotificationManager.CreateToastNotifier();
35-
ShowNotification("Background task starting");
36-
taskStartTime = DateTime.Now;
37-
taskEndTime = taskStartTime + TimeSpan.FromSeconds(10);
43+
Logger.LogInformation("Background task starting");
3844
bool appRunning = IsAppRunning();
3945
if (appRunning)
4046
{
41-
ShowNotification("App is running, background task shutting down");
47+
Logger.LogWarning("App is running, background task shutting down");
4248
deferral.Complete();
4349
return;
4450
}
@@ -52,7 +58,7 @@ public async void Run(IBackgroundTaskInstance taskInstance)
5258

5359
private async Task CheckTimer()
5460
{
55-
ShowNotification("Started listening for messages");
61+
Logger.LogInformation("Started listening for messages");
5662
while (true)
5763
{
5864
if (DateTime.Now >= taskEndTime)
@@ -65,7 +71,7 @@ private async Task CheckTimer()
6571

6672
private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
6773
{
68-
ShowNotification($"Background task cancelled: {reason}");
74+
Logger.LogError($"Background task cancelled: {reason}");
6975
Shutdown();
7076
deferral.Complete();
7177
}
@@ -88,7 +94,7 @@ private bool IsAppRunning()
8894

8995
private void Shutdown()
9096
{
91-
ShowNotification("Background task shutting down");
97+
Logger.LogInformation("Background task shutting down");
9298
handle.BackgroundRelease();
9399
semaphore.Release();
94100
}
@@ -141,29 +147,5 @@ private IList<AdaptiveText> GetNotificationText(string authorName, string conten
141147
text.Add(messageText);
142148
return text;
143149
}
144-
145-
private void ShowNotification(string content)
146-
{
147-
ToastContent toastContent = new ToastContent()
148-
{
149-
Visual = new ToastVisual()
150-
{
151-
BindingGeneric = new ToastBindingGeneric()
152-
{
153-
Children =
154-
{
155-
new AdaptiveText()
156-
{
157-
Text = content,
158-
HintWrap = true
159-
}
160-
}
161-
}
162-
}
163-
};
164-
165-
ToastNotification toastNotification = new ToastNotification(toastContent.GetXml());
166-
toastNotifier.Show(toastNotification);
167-
}
168150
}
169151
}

Signal-Windows/App.xaml.cs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,34 +131,24 @@ await Views[currentId].Dispatcher.RunTaskAsync(() =>
131131
protected override async void OnLaunched(LaunchActivatedEventArgs e)
132132
{
133133
string taskName = "SignalMessageBackgroundTask";
134-
bool foundTask = false;
135-
BackgroundExecutionManager.RemoveAccess("2383BenediktRadtke.SignalPrivateMessenger");
136-
//foreach (var task in BackgroundTaskRegistration.AllTasks)
137-
//{
138-
// if (task.Value.Name == taskName)
139-
// {
140-
// backgroundTaskRegistration = task.Value;
141-
// foundTask = true;
142-
// }
143-
//}
134+
BackgroundExecutionManager.RemoveAccess();
144135

145-
if (!foundTask)
136+
var builder = new BackgroundTaskBuilder();
137+
builder.Name = taskName;
138+
builder.TaskEntryPoint = "Signal_Windows.RC.SignalBackgroundTask";
139+
builder.IsNetworkRequested = true;
140+
builder.SetTrigger(new TimeTrigger(15, false));
141+
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
142+
var requestStatus = await BackgroundExecutionManager.RequestAccessAsync();
143+
if (requestStatus != BackgroundAccessStatus.DeniedBySystemPolicy ||
144+
requestStatus != BackgroundAccessStatus.DeniedByUser ||
145+
requestStatus != BackgroundAccessStatus.Unspecified)
146146
{
147-
var builder = new BackgroundTaskBuilder();
148-
builder.Name = taskName;
149-
builder.TaskEntryPoint = "Signal_Windows.RC.SignalBackgroundTask";
150-
builder.IsNetworkRequested = true;
151-
builder.SetTrigger(new TimeTrigger(15, false));
152-
//builder.SetTrigger(new SystemTrigger(SystemTriggerType.ServicingComplete, false));
153-
//builder.SetTrigger(new SystemTrigger(SystemTriggerType.TimeZoneChange, false));
154-
builder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
155-
var requestStatus = await BackgroundExecutionManager.RequestAccessAsync();
156-
if (requestStatus != BackgroundAccessStatus.DeniedBySystemPolicy ||
157-
requestStatus != BackgroundAccessStatus.DeniedByUser ||
158-
requestStatus != BackgroundAccessStatus.Unspecified)
159-
{
160-
backgroundTaskRegistration = builder.Register();
161-
}
147+
backgroundTaskRegistration = builder.Register();
148+
}
149+
else
150+
{
151+
Logger.LogWarning($"Unable to register background task: {requestStatus}");
162152
}
163153

164154
backgroundTaskRegistration.Completed += BackgroundTaskRegistration_Completed;

0 commit comments

Comments
 (0)