OSDN Git Service

Avoid a race condition causing dup'd images.
authorSeth Raphael <magicseth@google.com>
Wed, 2 Apr 2014 21:43:39 +0000 (14:43 -0700)
committerSeth Raphael <magicseth@google.com>
Wed, 2 Apr 2014 21:49:59 +0000 (14:49 -0700)
When a session is finished, it is written to the
media store.

We now have a listener looking for new images in
the media store.  If that happens to fire before
the session manager has updated the filmstrip,
the photo ends up being added twice.

Now, the listener checks to see if it is a photo
already being handled by a session

Bug: 13644124

Change-Id: I8a3c4a161ad0c326d9295f6714f6c6cbbcc6764e

src/com/android/camera/Storage.java
src/com/android/camera/data/CameraDataAdapter.java

index 42a3bb0..0a245a1 100644 (file)
@@ -56,6 +56,7 @@ public class Storage {
     private static final Log.Tag TAG = new Log.Tag("Storage");
     private static final String GOOGLE_COM = "google.com";
     private static HashMap<Uri, Uri> sSessionsToContentUris = new HashMap<Uri, Uri>();
+    private static HashMap<Uri, Uri> sContentUrisToSessions = new HashMap<Uri, Uri>();
     private static HashMap<Uri, byte[]> sSessionsToPlaceholderBytes = new HashMap<Uri, byte[]>();
     private static HashMap<Uri, Point> sSessionsToSizes= new HashMap<Uri, Point>();
 
@@ -239,6 +240,7 @@ public class Storage {
             resultUri = addImage(resolver, title, date, location, orientation, jpegLength, path,
                     width, height, mimeType);
             sSessionsToContentUris.put(imageUri, resultUri);
+            sContentUrisToSessions.put(resultUri, imageUri);
         } else {
             // Update the MediaStore
             resolver.update(imageUri, values, null, null);
@@ -336,6 +338,16 @@ public class Storage {
     }
 
     /**
+     * Takes a content URI and returns the original Session Uri if any
+     *
+     * @param contentUri the uri of the media store content
+     * @return The session uri of the original session, if it exists, or null.
+     */
+    public static Uri getSessionUriFromContentUri(Uri contentUri) {
+        return sContentUrisToSessions.get(contentUri);
+    }
+
+    /**
      * Determines if a URI points to a camera session
      *
      * @param uri the uri to check
index ad5d89f..a0387cf 100644 (file)
@@ -23,6 +23,7 @@ import android.net.Uri;
 import android.os.AsyncTask;
 import android.view.View;
 
+import com.android.camera.Storage;
 import com.android.camera.debug.Log;
 import com.android.camera.filmstrip.ImageData;
 import com.android.camera.util.Callback;
@@ -335,8 +336,12 @@ public class CameraDataAdapter implements LocalDataAdapter {
                 mLastPhotoId = Math.max(mLastPhotoId, newestPhoto.getContentId());
             }
             // We may add data that is already present, but if we do, it will be deduped in addData.
+            // addData does not dedupe session items, so we ignore them here
             for (LocalData localData : newPhotoData) {
-                addData(localData);
+                Uri sessionUri = Storage.getSessionUriFromContentUri(localData.getUri());
+                if (sessionUri == null) {
+                    addData(localData);
+                }
             }
         }
     }