Fixed task count

This commit is contained in:
M66B 2021-02-22 11:51:25 +01:00
parent e4e83de8f2
commit 765368afb0
1 changed files with 14 additions and 9 deletions

View File

@ -51,7 +51,6 @@ import java.util.concurrent.Future;
public abstract class SimpleTask<T> 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<T> 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<T> 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<T> implements LifecycleObserver {
}
static int getCount() {
return tasks.size();
int executing = 0;
synchronized (tasks) {
for (SimpleTask task : tasks)
if (task.count)
executing++;
}
return executing;
}
}