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

Commit 657f5137 authored by Romain Guy's avatar Romain Guy
Browse files

Make sure onPostExecute() is never called after cancel()

Bug #5651553

As dfined by the documentation.

Change-Id: I627a49ee000d6990b61fd64dde9b06f72cd5cbae
parent 8f9a9f61
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -195,6 +195,7 @@ public abstract class AsyncTask<Params, Progress, Result> {

    private volatile Status mStatus = Status.PENDING;
    
    private final AtomicBoolean mCancelled = new AtomicBoolean();
    private final AtomicBoolean mTaskInvoked = new AtomicBoolean();

    private static class SerialExecutor implements Executor {
@@ -261,6 +262,7 @@ public abstract class AsyncTask<Params, Progress, Result> {
                mTaskInvoked.set(true);

                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                //noinspection unchecked
                return postResult(doInBackground(mParams));
            }
        };
@@ -269,9 +271,7 @@ public abstract class AsyncTask<Params, Progress, Result> {
            @Override
            protected void done() {
                try {
                    final Result result = get();

                    postResultIfNotInvoked(result);
                    postResultIfNotInvoked(get());
                } catch (InterruptedException e) {
                    android.util.Log.w(LOG_TAG, e);
                } catch (ExecutionException e) {
@@ -295,6 +295,7 @@ public abstract class AsyncTask<Params, Progress, Result> {
    }

    private Result postResult(Result result) {
        @SuppressWarnings("unchecked")
        Message message = sHandler.obtainMessage(MESSAGE_POST_RESULT,
                new AsyncTaskResult<Result>(this, result));
        message.sendToTarget();
@@ -411,7 +412,7 @@ public abstract class AsyncTask<Params, Progress, Result> {
     * @see #cancel(boolean)
     */
    public final boolean isCancelled() {
        return mFuture.isCancelled();
        return mCancelled.get();
    }

    /**
@@ -444,6 +445,7 @@ public abstract class AsyncTask<Params, Progress, Result> {
     * @see #onCancelled(Object)
     */
    public final boolean cancel(boolean mayInterruptIfRunning) {
        mCancelled.set(true);
        return mFuture.cancel(mayInterruptIfRunning);
    }