55using System . Text ;
66using System . Threading ;
77using System . Threading . Tasks ;
8+ using libsignalservice ;
9+ using Microsoft . Extensions . Logging ;
810using Microsoft . Toolkit . Uwp . Notifications ;
911using Signal_Windows . Lib ;
1012using Signal_Windows . Lib . Events ;
1113using Signal_Windows . Models ;
14+ using Signal_Windows . Storage ;
1215using Windows . ApplicationModel . Background ;
1316using 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}
0 commit comments