Make media sets unique for the same parentId/childKey.

Change-Id: I133058c5c42985c63185f506692190ccbcf468e3
diff --git a/new3d/src/com/android/gallery3d/data/DataManager.java b/new3d/src/com/android/gallery3d/data/DataManager.java
index b4e16be..60162a0 100644
--- a/new3d/src/com/android/gallery3d/data/DataManager.java
+++ b/new3d/src/com/android/gallery3d/data/DataManager.java
@@ -90,7 +90,7 @@
     }
 
     private int mNextSelfId = 1;
-    public synchronized long obtainSetId(int parentId, int childKey, MediaSet self) {
+    public long obtainSetId(int parentId, int childKey, MediaSet self) {
         long key = parentId;
         key = (key << 32) | (childKey & 0xffffffffL);
 
@@ -173,6 +173,16 @@
         return mMediaSetCache.get(id);
     }
 
+    public MediaSet getMediaSet(int parentId, int childKey) {
+        long key = parentId;
+        key = (key << 32) | (childKey & 0xffffffffL);
+
+        Integer selfId = mKeyToSelfId.get(key);
+        if (selfId == null) return null;
+
+        return getMediaSet(selfId);
+    }
+
     public int getSupportedOperations(long uniqueId) {
         int parentId = DataManager.extractParentId(uniqueId);
         MediaSet parent = getMediaSet(parentId);
diff --git a/new3d/src/com/android/gallery3d/data/LocalAlbumSet.java b/new3d/src/com/android/gallery3d/data/LocalAlbumSet.java
index 47336ec..b6333b5 100644
--- a/new3d/src/com/android/gallery3d/data/LocalAlbumSet.java
+++ b/new3d/src/com/android/gallery3d/data/LocalAlbumSet.java
@@ -54,8 +54,8 @@
 
     private boolean mIsImage;
     private long mUniqueId;
-    private final ArrayList<LocalAlbum> mAlbums = new ArrayList<LocalAlbum>();
-    private HashMap<Integer, String> mLoadBuffer;
+    private ArrayList<LocalAlbum> mAlbums = new ArrayList<LocalAlbum>();
+    private final HashMap<Integer, String> mLoadBuffer = new HashMap<Integer, String>();
 
     public LocalAlbumSet(int parentId, int childKey, GalleryContext context,
             boolean isImage) {
@@ -102,9 +102,6 @@
 
     @Override
     protected void onLoadFromDatabase() {
-        HashMap<Integer, String> map = new HashMap<Integer, String>();
-        mLoadBuffer = map;
-
         Uri uri = mBaseUri.buildUpon().
                 appendQueryParameter("distinct", "true").build();
         Cursor cursor = mResolver.query(
@@ -112,7 +109,7 @@
         if (cursor == null) throw new NullPointerException();
         try {
             while (cursor.moveToNext()) {
-                map.put(cursor.getInt(BUCKET_ID_INDEX),
+                mLoadBuffer.put(cursor.getInt(BUCKET_ID_INDEX),
                         cursor.getString(BUCKET_NAME_INDEX));
             }
         } finally {
@@ -123,16 +120,28 @@
     @Override
     protected void onUpdateContent() {
         HashMap<Integer, String> map = mLoadBuffer;
-        if (map == null) throw new IllegalStateException();
+        ArrayList<LocalAlbum> newAlbums = new ArrayList<LocalAlbum>();
+        DataManager dataManager = mContext.getDataManager();
 
-        mAlbums.clear();
+        int parentId = getMyId();
         for (Map.Entry<Integer, String> entry : map.entrySet()) {
-            mAlbums.add(new LocalAlbum(getMyId(), mContext,
-                    entry.getKey(), entry.getValue(), mIsImage));
+            int childKey = entry.getKey();
+            LocalAlbum album = (LocalAlbum) dataManager.getMediaSet(parentId, childKey);
+            if (album == null) {
+                album = new LocalAlbum(parentId, mContext,
+                        childKey, entry.getValue(), mIsImage);
+            }
+            newAlbums.add(album);
         }
-        mLoadBuffer = null;
+
+        mAlbums = newAlbums;
+        mLoadBuffer.clear();
 
         Collections.sort(mAlbums, LocalAlbum.sBucketNameComparator);
+
+        for (int i = 0, n = mAlbums.size(); i < n; i++) {
+            mAlbums.get(i).reload();
+        }
     }
 
     private class MyContentObserver extends ContentObserver {
diff --git a/new3d/src/com/android/gallery3d/data/PicasaAlbumSet.java b/new3d/src/com/android/gallery3d/data/PicasaAlbumSet.java
index c6d708e..017fd22 100644
--- a/new3d/src/com/android/gallery3d/data/PicasaAlbumSet.java
+++ b/new3d/src/com/android/gallery3d/data/PicasaAlbumSet.java
@@ -32,8 +32,8 @@
     private static final String TAG = "PicasaAlbumSet";
     private final EntrySchema SCHEMA = AlbumEntry.SCHEMA;
 
-    private final ArrayList<PicasaAlbum> mAlbums = new ArrayList<PicasaAlbum>();
-    private final ArrayList<PicasaAlbum> mLoadBuffer = new ArrayList<PicasaAlbum>();
+    private ArrayList<PicasaAlbum> mAlbums = new ArrayList<PicasaAlbum>();
+    private final ArrayList<AlbumEntry> mLoadBuffer = new ArrayList<AlbumEntry>();
     private final long mUniqueId;
 
     public PicasaAlbumSet(int parentId, int childKey, GalleryContext context) {
@@ -72,14 +72,13 @@
 
     @Override
     protected void onLoadFromDatabase() {
-        mLoadBuffer.clear();
         Cursor cursor = mResolver.query(
                 PicasaContentProvider.ALBUMS_URI,
                 SCHEMA.getProjection(), null, null, null);
         try {
             while (cursor.moveToNext()) {
                 AlbumEntry entry = SCHEMA.cursorToObject(cursor, new AlbumEntry());
-                mLoadBuffer.add(new PicasaAlbum(getMyId(), mContext, entry));
+                mLoadBuffer.add(entry);
             }
         } finally {
             cursor.close();
@@ -88,9 +87,25 @@
 
     @Override
     protected void onUpdateContent() {
-        mAlbums.clear();
-        mAlbums.addAll(mLoadBuffer);
+        ArrayList<PicasaAlbum> newAlbums = new ArrayList<PicasaAlbum>();
+        int parentId = getMyId();
+        DataManager dataManager = mContext.getDataManager();
+
+        for (int i = 0, n = mLoadBuffer.size(); i < n; i++) {
+            AlbumEntry entry = mLoadBuffer.get(i);
+            int childKey = (int) entry.id;
+            PicasaAlbum album = (PicasaAlbum) dataManager.getMediaSet(parentId, childKey);
+            if (album == null) {
+                album = new PicasaAlbum(parentId, mContext, entry);
+            }
+            newAlbums.add(album);
+        }
+        mAlbums = newAlbums;
         mLoadBuffer.clear();
+
+        for (int i = 0, n = mAlbums.size(); i < n; i++) {
+            mAlbums.get(i).reload();
+        }
     }
 
     private class MyContentObserver extends ContentObserver {