diff options
-rw-r--r-- | src/com/android/documentsui/MultiRootDocumentsLoader.java | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/com/android/documentsui/MultiRootDocumentsLoader.java b/src/com/android/documentsui/MultiRootDocumentsLoader.java index 952aa575c..7668b0693 100644 --- a/src/com/android/documentsui/MultiRootDocumentsLoader.java +++ b/src/com/android/documentsui/MultiRootDocumentsLoader.java @@ -124,8 +124,13 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory @Override public DirectoryResult loadInBackground() { - synchronized (mTasks) { - return loadInBackgroundLocked(); + try { + synchronized (mTasks) { + return loadInBackgroundLocked(); + } + } catch (InterruptedException e) { + Log.w(TAG, "loadInBackground is interrupted: ", e); + return null; } } @@ -133,7 +138,7 @@ public abstract class MultiRootDocumentsLoader extends AsyncTaskLoader<Directory 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)); |