diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Tasks/UiBasedBackgroundTask.java b/app/src/main/java/org/shadowice/flocke/andotp/Tasks/UiBasedBackgroundTask.java index b95595a9..4293eb5a 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Tasks/UiBasedBackgroundTask.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Tasks/UiBasedBackgroundTask.java @@ -26,6 +26,8 @@ public abstract class UiBasedBackgroundTask { @Nullable private Result awaitedResult; + private volatile boolean isCanceled = false; + /** @param failedResult The result to return if the task fails (throws an exception or returns null). */ public UiBasedBackgroundTask(@NonNull Result failedResult) { this.failedResult = failedResult; @@ -37,6 +39,10 @@ public abstract class UiBasedBackgroundTask { * be stored until a new callback is set. */ public void setCallback(@Nullable UiCallback callback) { synchronized (callbackLock) { + // Don't bother doing anything if the task was canceled. + if (isCanceled()) { + return; + } this.callback = callback; // If we have an awaited result and are setting a new callback, publish the result immediately. if (awaitedResult != null && callback != null) { @@ -66,6 +72,10 @@ public abstract class UiBasedBackgroundTask { } synchronized (callbackLock) { + // Don't bother issuing callback or storing result if this task is canceled. + if (isCanceled()) { + return; + } if (callback != null) { emitResultOnMainThread(callback, result); } else { @@ -81,6 +91,16 @@ public abstract class UiBasedBackgroundTask { @NonNull protected abstract Result doInBackground() throws Exception; + @AnyThread + public boolean isCanceled() { + return isCanceled; + } + + @AnyThread + public void cancel() { + isCanceled = true; + } + @FunctionalInterface public interface UiCallback { @MainThread