summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Daichi Hirono <hirono@google.com> 2015-08-17 16:10:05 +0900
committer Daichi Hirono <hirono@google.com> 2015-08-19 13:53:06 +0900
commitd40b030ad55dfe8cb408fa35e0fb94400e2dcbb8 (patch)
tree4fea931e41bb9549cf48506e96cf039dd4f6a1fa
parent8bbb2de27a035f2bb619955dd7dceb0c90e6c42f (diff)
Extract task list operations from MTP DocumentLoader to the internal task list class.
BUG=23067619 Change-Id: I8dc6f50f2e8927fe38b992e4135009f36acf3079
-rw-r--r--packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java94
1 files changed, 55 insertions, 39 deletions
diff --git a/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java b/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
index 23532053a47f..c430def757db 100644
--- a/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
+++ b/packages/MtpDocumentsProvider/src/com/android/mtp/DocumentLoader.java
@@ -30,6 +30,11 @@ import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
+/**
+ * Loader for MTP document.
+ * At the first request, the loader returns only first NUM_INITIAL_ENTRIES. Then it launches
+ * background thread to load the rest documents and caches its result for next requests.
+ */
class DocumentLoader {
static final int NUM_INITIAL_ENTRIES = 10;
static final int NUM_LOADING_ENTRIES = 20;
@@ -37,7 +42,7 @@ class DocumentLoader {
private final MtpManager mMtpManager;
private final ContentResolver mResolver;
- private final LinkedList<LoaderTask> mTasks = new LinkedList<LoaderTask>();
+ private final TaskList mTaskList = new TaskList();
private boolean mHasBackgroundThread = false;
DocumentLoader(MtpManager mtpManager, ContentResolver resolver) {
@@ -56,7 +61,7 @@ class DocumentLoader {
synchronized Cursor queryChildDocuments(String[] columnNames, Identifier parent)
throws IOException {
- LoaderTask task = findTask(parent);
+ LoaderTask task = mTaskList.findTask(parent);
if (task == null) {
int parentHandle = parent.mObjectHandle;
// Need to pass the special value MtpManager.OBJECT_HANDLE_ROOT_CHILDREN to
@@ -70,11 +75,12 @@ class DocumentLoader {
mMtpManager,
parent.mDeviceId,
task.getUnloadedObjectHandles(NUM_INITIAL_ENTRIES)));
+ } else {
+ // Once remove the existing task in order to add it to the head of the list.
+ mTaskList.remove(task);
}
- // Move this task to the head of the list to prioritize it.
- mTasks.remove(task);
- mTasks.addFirst(task);
+ mTaskList.addFirst(task);
if (!task.completed() && !mHasBackgroundThread) {
mHasBackgroundThread = true;
new BackgroundLoaderThread().start();
@@ -84,41 +90,11 @@ class DocumentLoader {
}
synchronized void clearCache(int deviceId) {
- int i = 0;
- while (i < mTasks.size()) {
- if (mTasks.get(i).mIdentifier.mDeviceId == deviceId) {
- mTasks.remove(i);
- } else {
- i++;
- }
- }
+ mTaskList.clearTaskForDevice(deviceId);
}
synchronized void clearCache() {
- int i = 0;
- while (i < mTasks.size()) {
- if (mTasks.get(i).completed()) {
- mTasks.remove(i);
- } else {
- i++;
- }
- }
- }
-
- private LoaderTask findTask(Identifier parent) {
- for (int i = 0; i < mTasks.size(); i++) {
- if (mTasks.get(i).mIdentifier.equals(parent))
- return mTasks.get(i);
- }
- return null;
- }
-
- private LoaderTask findUncompletedTask() {
- for (int i = 0; i < mTasks.size(); i++) {
- if (!mTasks.get(i).completed())
- return mTasks.get(i);
- }
- return null;
+ mTaskList.clearCompletedTask();
}
private class BackgroundLoaderThread extends Thread {
@@ -130,7 +106,7 @@ class DocumentLoader {
int deviceId;
int[] handles;
synchronized (DocumentLoader.this) {
- task = findUncompletedTask();
+ task = mTaskList.findRunningTask();
if (task == null) {
mHasBackgroundThread = false;
return;
@@ -156,13 +132,53 @@ class DocumentLoader {
task.notify(mResolver);
}
} else {
- mTasks.remove(task);
+ mTaskList.remove(task);
}
}
}
}
}
+ private static class TaskList extends LinkedList<LoaderTask> {
+ LoaderTask findTask(Identifier parent) {
+ for (int i = 0; i < size(); i++) {
+ if (get(i).mIdentifier.equals(parent))
+ return get(i);
+ }
+ return null;
+ }
+
+ LoaderTask findRunningTask() {
+ for (int i = 0; i < size(); i++) {
+ if (!get(i).completed())
+ return get(i);
+ }
+ return null;
+ }
+
+ void clearTaskForDevice(int deviceId) {
+ int i = 0;
+ while (i < size()) {
+ if (get(i).mIdentifier.mDeviceId == deviceId) {
+ remove(i);
+ } else {
+ i++;
+ }
+ }
+ }
+
+ void clearCompletedTask() {
+ int i = 0;
+ while (i < size()) {
+ if (get(i).completed()) {
+ remove(i);
+ } else {
+ i++;
+ }
+ }
+ }
+ }
+
private static class LoaderTask {
final Identifier mIdentifier;
final int[] mObjectHandles;