reduce number of times mediascanner is invoked by downloadservice
Vasu Nori [Tue, 26 Oct 2010 23:37:08 +0000 (16:37 -0700)]
this is one of the 2 bugs mentioned in bug:3129943
sometimes MediaProvider returns null for returned param Uri.
in such cases, mark the file as 'scanned' in the downloads db
so that it is NOT scanned again later by DownlaodService.

depends on Change-Id: I92514e1a11f5119229c0c7292e410e352a9dbcdd

Change-Id: Ie9d839581f656c929a77bd5f244ad6b2fb1585e6

src/com/android/providers/downloads/DownloadService.java

index 2596fae..d5dff56 100644 (file)
@@ -367,7 +367,7 @@ public class DownloadService extends Service {
                             if (info.shouldScanFile()) {
                                 // initiate rescan of the file to - which will populate
                                 // mediaProviderUri column in this row
-                                if (!scanFile(info, true, false)) {
+                                if (!scanFile(info, false, true)) {
                                     throw new IllegalStateException("scanFile failed!");
                                 }
                             } else {
@@ -584,21 +584,23 @@ public class DownloadService extends Service {
                 mMediaScannerService.requestScanFile(info.mFileName, info.mMimeType,
                         new IMediaScannerListener.Stub() {
                             public void scanCompleted(String path, Uri uri) {
-                                if (uri != null && updateDatabase) {
-                                    // file is scanned and mediaprovider returned uri. store it in downloads
-                                    // table (i.e., update this downloaded file's row)
+                                if (updateDatabase) {
+                                    // Mark this as 'scanned' in the database
+                                    // so that it is NOT subject to re-scanning by MediaScanner
+                                    // next time this database row row is encountered
                                     ContentValues values = new ContentValues();
                                     values.put(Constants.MEDIA_SCANNED, 1);
-                                    values.put(Downloads.Impl.COLUMN_MEDIAPROVIDER_URI,
-                                            uri.toString());
+                                    if (uri != null) {
+                                        values.put(Downloads.Impl.COLUMN_MEDIAPROVIDER_URI,
+                                                uri.toString());
+                                    }
                                     getContentResolver().update(key, values, null, null);
-                                } else if (uri == null && deleteFile) {
-                                    // callback returned NO uri..that means this file doesn't
-                                    // exist in MediaProvider. but it still needs to be deleted
-                                    // TODO don't scan files that are not scannable by MediaScanner.
-                                    //      create a public method in MediaFile.java to return false
-                                    //      if the given file's mimetype is not any of the types
-                                    //      the mediaprovider is interested in.
+                                } else if (deleteFile) {
+                                    if (uri != null) {
+                                        // use the Uri returned to delete it from the MediaProvider
+                                        getContentResolver().delete(uri, null, null);
+                                    }
+                                    // delete the file and delete its row from the downloads db
                                     Helpers.deleteFile(resolver, id, path, mimeType);
                                 }
                             }