am 32fce221: am 2dc83e22: Move MTP communication for inserted files outside of db transaction.

* commit '32fce221950565c87cc1d04f8f9d5d664952f01b':
  Move MTP communication for inserted files outside of db transaction.
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 6b6c964..c5d15b1 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -2614,13 +2614,15 @@
             return setObjectReferences(helper, db, handle, values);
         }
 
+
         db.beginTransaction();
+        ArrayList<Long> notifyRowIds = new ArrayList<Long>();
         int numInserted = 0;
         try {
             int len = values.length;
             for (int i = 0; i < len; i++) {
                 if (values[i] != null) {
-                    insertInternal(uri, match, values[i]);
+                    insertInternal(uri, match, values[i], notifyRowIds);
                 }
             }
             numInserted = len;
@@ -2628,6 +2630,10 @@
         } finally {
             db.endTransaction();
         }
+
+        // Notify MTP (outside of successful transaction)
+        notifyMtp(notifyRowIds);
+
         getContext().getContentResolver().notifyChange(uri, null);
         return numInserted;
     }
@@ -2635,7 +2641,11 @@
     @Override
     public Uri insert(Uri uri, ContentValues initialValues) {
         int match = URI_MATCHER.match(uri);
-        Uri newUri = insertInternal(uri, match, initialValues);
+
+        ArrayList<Long> notifyRowIds = new ArrayList<Long>();
+        Uri newUri = insertInternal(uri, match, initialValues, notifyRowIds);
+        notifyMtp(notifyRowIds);
+
         // do not signal notification for MTP objects.
         // we will signal instead after file transfer is successful.
         if (newUri != null && match != MTP_OBJECTS) {
@@ -2644,6 +2654,13 @@
         return newUri;
     }
 
+    private void notifyMtp(ArrayList<Long> rowIds) {
+        int size = rowIds.size();
+        for (int i = 0; i < size; i++) {
+            sendObjectAdded(rowIds.get(i).longValue());
+        }
+    }
+
     private int playlistBulkInsert(SQLiteDatabase db, Uri uri, ContentValues values[]) {
         DatabaseUtils.InsertHelper helper =
             new DatabaseUtils.InsertHelper(db, "audio_playlists_map");
@@ -2766,7 +2783,7 @@
     }
 
     private long insertFile(DatabaseHelper helper, Uri uri, ContentValues initialValues, int mediaType,
-            boolean notify) {
+                            boolean notify, ArrayList<Long> notifyRowIds) {
         SQLiteDatabase db = helper.getWritableDatabase();
         ContentValues values = null;
 
@@ -2981,7 +2998,7 @@
             if (LOCAL_LOGV) Log.v(TAG, "insertFile: values=" + values + " returned: " + rowId);
 
             if (rowId != -1 && notify) {
-                sendObjectAdded(rowId);
+                notifyRowIds.add(rowId);
             }
         } else {
             helper.mNumUpdates++;
@@ -3138,7 +3155,8 @@
         }
     }
 
-    private Uri insertInternal(Uri uri, int match, ContentValues initialValues) {
+    private Uri insertInternal(Uri uri, int match, ContentValues initialValues,
+                               ArrayList<Long> notifyRowIds) {
         long rowId;
 
         if (LOCAL_LOGV) Log.v(TAG, "insertInternal: "+uri+", initValues="+initialValues);
@@ -3176,7 +3194,8 @@
 
         switch (match) {
             case IMAGES_MEDIA: {
-                rowId = insertFile(helper, uri, initialValues, FileColumns.MEDIA_TYPE_IMAGE, true);
+                rowId = insertFile(helper, uri, initialValues,
+                        FileColumns.MEDIA_TYPE_IMAGE, true, notifyRowIds);
                 if (rowId > 0) {
                     newUri = ContentUris.withAppendedId(
                             Images.Media.getContentUri(uri.getPathSegments().get(0)), rowId);
@@ -3211,7 +3230,8 @@
             }
 
             case AUDIO_MEDIA: {
-                rowId = insertFile(helper, uri, initialValues, FileColumns.MEDIA_TYPE_AUDIO, true);
+                rowId = insertFile(helper, uri, initialValues,
+                        FileColumns.MEDIA_TYPE_AUDIO, true, notifyRowIds);
                 if (rowId > 0) {
                     newUri = ContentUris.withAppendedId(Audio.Media.getContentUri(uri.getPathSegments().get(0)), rowId);
                     if (genre != null) {
@@ -3270,7 +3290,8 @@
             case AUDIO_PLAYLISTS: {
                 ContentValues values = new ContentValues(initialValues);
                 values.put(MediaStore.Audio.Playlists.DATE_ADDED, System.currentTimeMillis() / 1000);
-                rowId = insertFile(helper, uri, values, FileColumns.MEDIA_TYPE_PLAYLIST, true);
+                rowId = insertFile(helper, uri, values,
+                        FileColumns.MEDIA_TYPE_PLAYLIST, true, notifyRowIds);
                 if (rowId > 0) {
                     newUri = ContentUris.withAppendedId(Audio.Playlists.getContentUri(uri.getPathSegments().get(0)), rowId);
                 }
@@ -3291,7 +3312,8 @@
             }
 
             case VIDEO_MEDIA: {
-                rowId = insertFile(helper, uri, initialValues, FileColumns.MEDIA_TYPE_VIDEO, true);
+                rowId = insertFile(helper, uri, initialValues,
+                        FileColumns.MEDIA_TYPE_VIDEO, true, notifyRowIds);
                 if (rowId > 0) {
                     newUri = ContentUris.withAppendedId(Video.Media.getContentUri(
                             uri.getPathSegments().get(0)), rowId);
@@ -3346,16 +3368,16 @@
 
             case FILES:
                 rowId = insertFile(helper, uri, initialValues,
-                        FileColumns.MEDIA_TYPE_NONE, true);
+                        FileColumns.MEDIA_TYPE_NONE, true, notifyRowIds);
                 if (rowId > 0) {
                     newUri = Files.getContentUri(uri.getPathSegments().get(0), rowId);
                 }
                 break;
 
             case MTP_OBJECTS:
-                // don't send a notification if the insert originated from MTP
+                // We don't send a notification if the insert originated from MTP
                 rowId = insertFile(helper, uri, initialValues,
-                        FileColumns.MEDIA_TYPE_NONE, false);
+                        FileColumns.MEDIA_TYPE_NONE, false, notifyRowIds);
                 if (rowId > 0) {
                     newUri = Files.getMtpObjectsUri(uri.getPathSegments().get(0), rowId);
                 }