diff options
author | 2021-01-05 15:12:54 -0800 | |
---|---|---|
committer | 2021-01-06 16:12:38 -0800 | |
commit | c57e2fb7f7de82358d917ebb2c503bb9ad74f2fb (patch) | |
tree | 9cfc1bcca654959e3b1c1cea7b406eb8254eccb1 | |
parent | 47e3f4f17bb1939132c678d97339d4b9592eb1dc (diff) |
If loading is cancelled, interrupt loadInBackgroundLocked().
Bug: 165161481
Test: atest DocumentsUIGoogleTests
Change-Id: I2b4a1636efeda4fb5a33b2f0ca6da63686b16e98
-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)); |