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

Commit a04b7a1f authored by Zemiao Zhu's avatar Zemiao Zhu Committed by Android (Google) Code Review
Browse files

Merge "If loading is cancelled, interrupt loadInBackgroundLocked()." into mainline-prod

parents ced66840 c57e2fb7
Loading
Loading
Loading
Loading
+19 −7
Original line number Diff line number Diff line
@@ -124,16 +124,21 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory

    @Override
    public DirectoryResult loadInBackground() {
        try {
            synchronized (mTasks) {
                return loadInBackgroundLocked();
            }
        } catch (InterruptedException e) {
            Log.w(TAG, "loadInBackground is interrupted: ", e);
            return null;
        }
    }

    public void setObserver(LockingContentObserver observer) {
        mObserver = observer;
    }

    private DirectoryResult loadInBackgroundLocked() {
    private DirectoryResult loadInBackgroundLocked() throws InterruptedException {
        if (mFirstPassLatch == null) {
            // First time through we kick off all the recent tasks, and wait
            // around to see if everyone finishes quickly.
@@ -144,6 +149,11 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory
                        getQueryTask(rootEntry.getKey(), rootEntry.getValue()));
            }

            if (isLoadInBackgroundCanceled()) {
                // Loader is cancelled (e.g. about to be reset), preempt loading.
                throw new InterruptedException("Loading is cancelled!");
            }

            mFirstPassLatch = new CountDownLatch(mTasks.size());
            for (QueryTask task : mTasks.values()) {
                mExecutors.lookup(task.authority).execute(task);
@@ -164,6 +174,11 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory
        int totalQuerySize = 0;
        List<Cursor> cursors = new ArrayList<>(mTasks.size());
        for (QueryTask task : mTasks.values()) {
            if (isLoadInBackgroundCanceled()) {
                // Loader is cancelled (e.g. about to be reset), preempt loading.
                throw new InterruptedException("Loading is cancelled!");
            }

            if (task.isDone()) {
                try {
                    final Cursor[] taskCursors = task.get();
@@ -291,7 +306,7 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory
        DirectoryResult oldResult = mResult;
        mResult = result;

        if (isStarted()) {
        if (isStarted() && !isAbandoned() && !isLoadInBackgroundCanceled()) {
            super.deliverResult(result);
        }

@@ -325,9 +340,6 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        synchronized (mTasks) {
            for (QueryTask task : mTasks.values()) {
                mExecutors.lookup(task.authority).execute(() -> FileUtils.closeQuietly(task));