mirror of
https://github.com/M66B/FairEmail.git
synced 2025-12-26 15:40:59 +01:00
Allow cancelling simple task
This commit is contained in:
@@ -39,6 +39,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
// This simple task is simple to use, but it is also simple to cause bugs that can easily lead to crashes
|
||||
// Make sure to not access any member in any outer scope from onExecute
|
||||
@@ -49,6 +50,9 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
||||
private boolean count = true;
|
||||
private int executing = 0;
|
||||
|
||||
private String name;
|
||||
private Future<?> future;
|
||||
|
||||
private static final List<SimpleTask> tasks = new ArrayList<>();
|
||||
|
||||
private static final ExecutorService executor =
|
||||
@@ -89,6 +93,8 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
||||
private void run(final Context context, final LifecycleOwner owner, final Bundle args, final String name) {
|
||||
final Handler handler = new Handler();
|
||||
|
||||
this.name = name;
|
||||
|
||||
if (owner instanceof TwoStateOwner)
|
||||
Log.e(new Throwable("SimpleTask/TwoStateOwner"));
|
||||
|
||||
@@ -109,7 +115,7 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
||||
onException(args, ex);
|
||||
}
|
||||
|
||||
executor.submit(new Runnable() {
|
||||
future = executor.submit(new Runnable() {
|
||||
private Object data;
|
||||
private Throwable ex;
|
||||
|
||||
@@ -187,7 +193,16 @@ public abstract class SimpleTask<T> implements LifecycleObserver {
|
||||
});
|
||||
}
|
||||
|
||||
void cancel(Context context) {
|
||||
if (future != null)
|
||||
if (future.cancel(false)) {
|
||||
Log.i("Cancelled task=" + name);
|
||||
cleanup(context);
|
||||
}
|
||||
}
|
||||
|
||||
private void cleanup(Context context) {
|
||||
future = null;
|
||||
synchronized (tasks) {
|
||||
tasks.remove(this);
|
||||
if (count)
|
||||
|
||||
Reference in New Issue
Block a user