From c98680347a5a97f3531f145f200ff04a9f4c3a3a Mon Sep 17 00:00:00 2001 From: Philipp Crocoll Date: Tue, 27 May 2025 16:28:58 +0200 Subject: [PATCH] fix implementation for BackgroundSyncService for blocking operations --- src/Kp2aBusinessLogic/BackgroundOperationRunner.cs | 5 +++-- src/keepass2android-app/app/App.cs | 5 +++-- src/keepass2android-app/services/BackgroundSyncService.cs | 2 ++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs b/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs index fd3362e4..6c52bbdb 100644 --- a/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs +++ b/src/Kp2aBusinessLogic/BackgroundOperationRunner.cs @@ -91,11 +91,12 @@ public class BackgroundOperationRunner public void SetNewActiveContext(IKp2aApp app) { Context? context = app.ActiveContext; + bool isAppContext = context == null || (context.ApplicationContext == context); lock (_taskQueueLock) { - if (context == null && _thread != null) + if (isAppContext && (_thread != null || BlockingOperationRunner.HasActiveTask)) { - //this will register the service as new active context + //this will register the service as new active context (see BackgroundSyncService.OnStartCommand()) app.StartBackgroundSyncService(); return; } diff --git a/src/keepass2android-app/app/App.cs b/src/keepass2android-app/app/App.cs index ebfb4bbf..39d4e022 100644 --- a/src/keepass2android-app/app/App.cs +++ b/src/keepass2android-app/app/App.cs @@ -1630,8 +1630,9 @@ namespace keepass2android { Kp2aLog.Log("Going to background"); Kp2a.ActiveContext = null; - BackgroundOperationRunner.Instance.SetNewActiveContext( Kp2a); - + // notify background operation runner that there is currently no active context, i.e. no UI where status can be displayed. + // The BackgroundOperationRunner will launch the background sync service if a task (even a blocking one) is running currently. + BackgroundOperationRunner.Instance.SetNewActiveContext(Kp2a); } diff --git a/src/keepass2android-app/services/BackgroundSyncService.cs b/src/keepass2android-app/services/BackgroundSyncService.cs index f7f1a0b5..bf18b8e2 100644 --- a/src/keepass2android-app/services/BackgroundSyncService.cs +++ b/src/keepass2android-app/services/BackgroundSyncService.cs @@ -53,6 +53,7 @@ namespace keepass2android.services StartForeground(NotificationId, BuildNotification()); App.Kp2a.ActiveContext = this; BackgroundOperationRunner.Instance.SetNewActiveContext(App.Kp2a); + BlockingOperationRunner.SetNewActiveContext(this); return StartCommandResult.Sticky; } catch (Exception ex) @@ -123,6 +124,7 @@ namespace keepass2android.services _cts.Cancel(); _cts.Dispose(); } + BlockingOperationRunner.RemoveActiveContext(this); } public IProgressUi? ProgressUi