From 765368afb003a4c3044b12ae6fa0405811bf55dc Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 22 Feb 2021 11:51:25 +0100 Subject: [PATCH] Fixed task count --- .../java/eu/faircode/email/SimpleTask.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/SimpleTask.java b/app/src/main/java/eu/faircode/email/SimpleTask.java index 4772618b89..05a635ce70 100644 --- a/app/src/main/java/eu/faircode/email/SimpleTask.java +++ b/app/src/main/java/eu/faircode/email/SimpleTask.java @@ -51,7 +51,6 @@ import java.util.concurrent.Future; public abstract class SimpleTask implements LifecycleObserver { private boolean log = true; private boolean count = true; - private int executing = 0; private String name; private Future future; @@ -127,12 +126,9 @@ public abstract class SimpleTask implements LifecycleObserver { // prevent garbage collection synchronized (tasks) { tasks.add(this); - if (count) - executing++; } - LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); - lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", executing)); + updateTaskCount(context); try { onPreExecute(args); @@ -260,13 +256,16 @@ public abstract class SimpleTask implements LifecycleObserver { future = null; synchronized (tasks) { tasks.remove(this); - if (count) - executing--; } + updateTaskCount(context); + } + + private void updateTaskCount(Context context) { + int executing = getCount(); + Log.i("Remaining tasks=" + executing + "/" + tasks.size()); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); lbm.sendBroadcast(new Intent(ACTION_TASK_COUNT).putExtra("count", executing)); - Log.i("Remaining tasks=" + tasks.size()); } protected void onPreExecute(Bundle args) { @@ -283,6 +282,12 @@ public abstract class SimpleTask implements LifecycleObserver { } static int getCount() { - return tasks.size(); + int executing = 0; + synchronized (tasks) { + for (SimpleTask task : tasks) + if (task.count) + executing++; + } + return executing; } }