summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/data/MtpDeviceSet.java
diff options
context:
space:
mode:
authorOwen Lin <owenlin@google.com>2012-03-16 13:51:49 +0800
committerOwen Lin <owenlin@google.com>2012-03-20 19:22:10 +0800
commit813ba6f4c7f6c36928bf926f7856eb744a823b43 (patch)
treea5f8862f961cbf8b913aae0216ddaac11bb49779 /src/com/android/gallery3d/data/MtpDeviceSet.java
parente80b47691bfc340f76464b97bac5ad6fe9f561de (diff)
downloadandroid_packages_apps_Snap-813ba6f4c7f6c36928bf926f7856eb744a823b43.tar.gz
android_packages_apps_Snap-813ba6f4c7f6c36928bf926f7856eb744a823b43.tar.bz2
android_packages_apps_Snap-813ba6f4c7f6c36928bf926f7856eb744a823b43.zip
Make top level albums can be loaded concurrently.
Change-Id: I1acef9fc9a442c36ad1d9d0931b0defc6091bf33 fix: 5947962
Diffstat (limited to 'src/com/android/gallery3d/data/MtpDeviceSet.java')
-rw-r--r--src/com/android/gallery3d/data/MtpDeviceSet.java72
1 files changed, 50 insertions, 22 deletions
diff --git a/src/com/android/gallery3d/data/MtpDeviceSet.java b/src/com/android/gallery3d/data/MtpDeviceSet.java
index 0d0e67ec3..dd79bc1cb 100644
--- a/src/com/android/gallery3d/data/MtpDeviceSet.java
+++ b/src/com/android/gallery3d/data/MtpDeviceSet.java
@@ -22,22 +22,30 @@ import android.util.Log;
import com.android.gallery3d.R;
import com.android.gallery3d.app.GalleryApp;
+import com.android.gallery3d.util.Future;
+import com.android.gallery3d.util.FutureListener;
import com.android.gallery3d.util.MediaSetUtils;
+import com.android.gallery3d.util.ThreadPool.Job;
+import com.android.gallery3d.util.ThreadPool.JobContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
// MtpDeviceSet -- MtpDevice -- MtpImage
-public class MtpDeviceSet extends MediaSet {
+public class MtpDeviceSet extends MediaSet
+ implements FutureListener<ArrayList<MediaSet>> {
private static final String TAG = "MtpDeviceSet";
private GalleryApp mApplication;
- private final ArrayList<MediaSet> mDeviceSet = new ArrayList<MediaSet>();
private final ChangeNotifier mNotifier;
private final MtpContext mMtpContext;
private final String mName;
+ private Future<ArrayList<MediaSet>> mLoadTask;
+ private ArrayList<MediaSet> mDeviceSet = new ArrayList<MediaSet>();
+ private ArrayList<MediaSet> mLoadBuffer;
+
public MtpDeviceSet(Path path, GalleryApp application, MtpContext mtpContext) {
super(path, nextVersionNumber());
mApplication = application;
@@ -46,26 +54,33 @@ public class MtpDeviceSet extends MediaSet {
mName = application.getResources().getString(R.string.set_label_mtp_devices);
}
- private void loadDevices() {
- DataManager dataManager = mApplication.getDataManager();
- // Enumerate all devices
- mDeviceSet.clear();
- List<android.mtp.MtpDevice> devices = mMtpContext.getMtpClient().getDeviceList();
- Log.v(TAG, "loadDevices: " + devices + ", size=" + devices.size());
- for (android.mtp.MtpDevice mtpDevice : devices) {
- int deviceId = mtpDevice.getDeviceId();
- Path childPath = mPath.getChild(deviceId);
- MtpDevice device = (MtpDevice) dataManager.peekMediaObject(childPath);
- if (device == null) {
- device = new MtpDevice(childPath, mApplication, deviceId, mMtpContext);
+ private class DevicesLoader implements Job<ArrayList<MediaSet>> {
+ @Override
+ public ArrayList<MediaSet> run(JobContext jc) {
+ DataManager dataManager = mApplication.getDataManager();
+ ArrayList<MediaSet> result = new ArrayList<MediaSet>();
+
+ // Enumerate all devices
+ List<android.mtp.MtpDevice> devices = mMtpContext.getMtpClient().getDeviceList();
+ Log.v(TAG, "loadDevices: " + devices + ", size=" + devices.size());
+ for (android.mtp.MtpDevice mtpDevice : devices) {
+ synchronized (DataManager.LOCK) {
+ int deviceId = mtpDevice.getDeviceId();
+ Path childPath = mPath.getChild(deviceId);
+ MtpDevice device = (MtpDevice) dataManager.peekMediaObject(childPath);
+ if (device == null) {
+ device = new MtpDevice(childPath, mApplication, deviceId, mMtpContext);
+ }
+ Log.d(TAG, "add device " + device);
+ result.add(device);
+ }
}
- Log.d(TAG, "add device " + device);
- mDeviceSet.add(device);
- }
+ Collections.sort(result, MediaSetUtils.NAME_COMPARATOR);
- Collections.sort(mDeviceSet, MediaSetUtils.NAME_COMPARATOR);
- for (int i = 0, n = mDeviceSet.size(); i < n; i++) {
- mDeviceSet.get(i).reload();
+ for (int i = 0, n = result.size(); i < n; ++i) {
+ result.get(i).reload();
+ }
+ return result;
}
}
@@ -99,11 +114,24 @@ public class MtpDeviceSet extends MediaSet {
}
@Override
- public long reload() {
+ public synchronized long reload() {
if (mNotifier.isDirty()) {
+ if (mLoadTask != null) mLoadTask.cancel();
+ mLoadTask = mApplication.getThreadPool().submit(new DevicesLoader(), this);
+ }
+ if (mLoadBuffer != null) {
+ mDeviceSet = mLoadBuffer;
+ mLoadBuffer = null;
mDataVersion = nextVersionNumber();
- loadDevices();
}
return mDataVersion;
}
+
+ @Override
+ public synchronized void onFutureDone(Future<ArrayList<MediaSet>> future) {
+ if (future != mLoadTask) return;
+ mLoadBuffer = future.get();
+ if (mLoadBuffer == null) mLoadBuffer = new ArrayList<MediaSet>();
+ notifyContentChanged();
+ }
}