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

Commit c57e2fb7 authored by Zemiao Zhu's avatar Zemiao Zhu
Browse files

If loading is cancelled, interrupt loadInBackgroundLocked().

Bug: 165161481
Test: atest DocumentsUIGoogleTests
Change-Id: I2b4a1636efeda4fb5a33b2f0ca6da63686b16e98
parent 47e3f4f1
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));