(GB/GBMR) (do not merge) delete file from disk when deleting from db
Vasu Nori [Fri, 3 Dec 2010 19:35:04 +0000 (11:35 -0800)]
bug:3175143
sometimes mediaprovider doesn't delete the file from disk when
it is deleted from its db. for example, audio files, pdf files.
DownloadManager/DownloadApp should delete the file when it is
deleted from downloads db.
DO NOT MERGE

this is esentially porting HC fix from DownloadService.java to GB

Change-Id: I70f3a7ad968f82ccba00d664e9a2993d75a18d15

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

index 169ef97..95d07d6 100644 (file)
@@ -363,7 +363,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 {
@@ -377,9 +377,9 @@ public class DownloadService extends Service {
                             // in DownProvider database (the order of deletion is important).
                             getContentResolver().delete(Uri.parse(info.mMediaProviderUri), null,
                                     null);
-                            getContentResolver().delete(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,
-                                    Downloads.Impl._ID + " = ? ",
-                                    new String[]{String.valueOf(info.mId)});
+                            // the following deletes the file and then deletes it from downloads db
+                            Helpers.deleteFile(getContentResolver(), info.mId, info.mFileName,
+                                    info.mMimeType);
                         }
                     }
                 }
@@ -580,21 +580,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 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);
                                 }
                             }
index 0ab3f93..dfd5ffc 100644 (file)
@@ -52,6 +52,7 @@ import android.widget.Toast;
 
 import com.android.providers.downloads.ui.DownloadItem.DownloadSelectListener;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.util.HashSet;
@@ -592,6 +593,14 @@ public class DownloadList extends Activity
                         return;
                     } else {
                         getContentResolver().delete(Uri.parse(mediaProviderUri), null, null);
+                        // sometimes mediaprovider doesn't delete file from sdcard after deleting it
+                        // from its db. delete it now
+                        try {
+                          File file = new File(path);
+                          file.delete();
+                      } catch (Exception e) {
+                          Log.w(LOG_TAG, "file: '" + path + "' couldn't be deleted", e);
+                      }
                     }
                 }
             }