fix implementation for BackgroundSyncService for blocking operations

This commit is contained in:
Philipp Crocoll
2025-05-27 16:28:58 +02:00
parent 35367bb28b
commit c98680347a
3 changed files with 8 additions and 4 deletions

View File

@@ -91,11 +91,12 @@ public class BackgroundOperationRunner
public void SetNewActiveContext(IKp2aApp app) public void SetNewActiveContext(IKp2aApp app)
{ {
Context? context = app.ActiveContext; Context? context = app.ActiveContext;
bool isAppContext = context == null || (context.ApplicationContext == context);
lock (_taskQueueLock) 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(); app.StartBackgroundSyncService();
return; return;
} }

View File

@@ -1630,8 +1630,9 @@ namespace keepass2android
{ {
Kp2aLog.Log("Going to background"); Kp2aLog.Log("Going to background");
Kp2a.ActiveContext = null; 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);
} }

View File

@@ -53,6 +53,7 @@ namespace keepass2android.services
StartForeground(NotificationId, BuildNotification()); StartForeground(NotificationId, BuildNotification());
App.Kp2a.ActiveContext = this; App.Kp2a.ActiveContext = this;
BackgroundOperationRunner.Instance.SetNewActiveContext(App.Kp2a); BackgroundOperationRunner.Instance.SetNewActiveContext(App.Kp2a);
BlockingOperationRunner.SetNewActiveContext(this);
return StartCommandResult.Sticky; return StartCommandResult.Sticky;
} }
catch (Exception ex) catch (Exception ex)
@@ -123,6 +124,7 @@ namespace keepass2android.services
_cts.Cancel(); _cts.Cancel();
_cts.Dispose(); _cts.Dispose();
} }
BlockingOperationRunner.RemoveActiveContext(this);
} }
public IProgressUi? ProgressUi public IProgressUi? ProgressUi