Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 78e6b493 authored by Eugene Susla's avatar Eugene Susla
Browse files

Correctly handle AndroidFuture#cancel

Also fixes AndroidFuture#completeExceptionally return result

Test: presubmit
Change-Id: I6c1cdb69c89829f41efb59472e925b526801a524
parent b2f63d11
Loading
Loading
Loading
Loading
+18 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.Preconditions;
import com.android.internal.util.function.pooled.PooledLambda;

import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
@@ -129,7 +130,23 @@ public class AndroidFuture<T> extends CompletableFuture<T> implements Parcelable
        if (changed) {
            onCompleted(null, ex);
        }
        return super.completeExceptionally(ex);
        return changed;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        boolean changed = super.cancel(mayInterruptIfRunning);
        if (changed) {
            try {
                get();
                throw new IllegalStateException("Expected CancellationException");
            } catch (CancellationException ex) {
                onCompleted(null, ex);
            } catch (Throwable e) {
                throw new IllegalStateException("Expected CancellationException", e);
            }
        }
        return changed;
    }

    @CallSuper