From 2542018ca9755f40fcf484e22c473a3248576022 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 13 Jan 2023 14:55:24 +0100 Subject: [PATCH] Dequeue cancelled tasks --- app/src/main/java/eu/faircode/email/SimpleTask.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/SimpleTask.java b/app/src/main/java/eu/faircode/email/SimpleTask.java index fdc8391eba..4098c34585 100644 --- a/app/src/main/java/eu/faircode/email/SimpleTask.java +++ b/app/src/main/java/eu/faircode/email/SimpleTask.java @@ -45,6 +45,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.ThreadPoolExecutor; // 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 @@ -358,6 +360,11 @@ public abstract class SimpleTask implements LifecycleObserver { } void cancel(Context context) { + ExecutorService executor = getExecutor(context); + if (executor instanceof ThreadPoolExecutor && future instanceof Runnable) { + boolean removed = ((ThreadPoolExecutor) executor).remove((Runnable) future); + Log.i("Remove task=" + name + " removed=" + removed); + } if (future != null && future.cancel(false)) { Log.i("Cancelled task=" + name); cleanup(context);