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

Commit fe0d3523 authored by Romain Guy's avatar Romain Guy Committed by Android (Google) Code Review
Browse files

Merge "Make sure onPostExecute() is never called after cancel() Bug #5651553"

parents 714e0a6d 657f5137
Loading
Loading
Loading
Loading
+6 −4
Original line number Original line Diff line number Diff line
@@ -195,6 +195,7 @@ public abstract class AsyncTask<Params, Progress, Result> {


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


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


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

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


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


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