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

Commit 9bc69487 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Synchronize access to Job.mState" into main

parents 3e26ed85 3487ce93
Loading
Loading
Loading
Loading
+31 −14
Original line number Original line Diff line number Diff line
@@ -121,7 +121,7 @@ abstract public class Job implements Runnable {
    private final Map<String, ContentProviderClient> mClients = new HashMap<>();
    private final Map<String, ContentProviderClient> mClients = new HashMap<>();
    private final Features mFeatures;
    private final Features mFeatures;


    private volatile @State int mState = STATE_CREATED;
    private @State int mState = STATE_CREATED;


    /**
    /**
     * A simple progressable job, much like an AsyncTask, but with support
     * A simple progressable job, much like an AsyncTask, but with support
@@ -155,19 +155,30 @@ abstract public class Job implements Runnable {


    @Override
    @Override
    public final void run() {
    public final void run() {
        if (isCanceled()) {
        synchronized (this) {
            if (mState == STATE_CANCELED) {
                // Canceled before running
                // Canceled before running
                return;
                return;
            }
            }


            mState = STATE_STARTED;
            mState = STATE_STARTED;
        }

        listener.onStart(this);
        listener.onStart(this);


        try {
        try {
            boolean result = setUp();
            boolean ok = setUp();
            if (result && !isCanceled()) {

            if (ok) {
                synchronized (this) {
                    if (mState == STATE_CANCELED) {
                        ok = false;
                    } else {
                        mState = STATE_SET_UP;
                        mState = STATE_SET_UP;
                start();
                    }
                }

                if (ok) start();
            }
            }
        } catch (RuntimeException e) {
        } catch (RuntimeException e) {
            // No exceptions should be thrown here, as all calls to the provider must be
            // No exceptions should be thrown here, as all calls to the provider must be
@@ -175,7 +186,10 @@ abstract public class Job implements Runnable {
            Log.e(TAG, "Operation failed due to an unhandled runtime exception.", e);
            Log.e(TAG, "Operation failed due to an unhandled runtime exception.", e);
            Metrics.logFileOperationErrors(operationType, failedDocs, failedUris);
            Metrics.logFileOperationErrors(operationType, failedDocs, failedUris);
        } finally {
        } finally {
            mState = (mState == STATE_STARTED || mState == STATE_SET_UP) ? STATE_COMPLETED : mState;
            synchronized (this) {
                if (mState == STATE_STARTED || mState == STATE_SET_UP) mState = STATE_COMPLETED;
            }

            finish();
            finish();
            listener.onFinished(this);
            listener.onFinished(this);


@@ -245,21 +259,24 @@ abstract public class Job implements Runnable {
        }
        }
    }
    }


    final @State int getState() {
    final synchronized @State int getState() {
        return mState;
        return mState;
    }
    }


    /** Requests the cancellation of this job. Can be called from any thread. */
    final void cancel() {
    final void cancel() {
        synchronized (this) {
            mState = STATE_CANCELED;
            mState = STATE_CANCELED;
        }
        mSignal.cancel();
        mSignal.cancel();
        Metrics.logFileOperationCancelled(operationType);
        Metrics.logFileOperationCancelled(operationType);
    }
    }


    final boolean isCanceled() {
    final synchronized boolean isCanceled() {
        return mState == STATE_CANCELED;
        return mState == STATE_CANCELED;
    }
    }


    final boolean isFinished() {
    final synchronized boolean isFinished() {
        return mState == STATE_CANCELED || mState == STATE_COMPLETED;
        return mState == STATE_CANCELED || mState == STATE_COMPLETED;
    }
    }