Actually delete files when rows are deleted.
Jeff Sharkey [Wed, 17 Jun 2015 00:06:37 +0000 (17:06 -0700)]
Otherwise they're orphaned until the next idle maintenance pass.

Bug: 21786983
Change-Id: I6eb2240d657366b65482bd3a0d5683e5d34a541a

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

index 4b23024..b19da70 100644 (file)
@@ -1142,9 +1142,7 @@ public final class DownloadProvider extends ContentProvider {
      * Deletes a row in the database
      */
     @Override
-    public int delete(final Uri uri, final String where,
-            final String[] whereArgs) {
-
+    public int delete(final Uri uri, final String where, final String[] whereArgs) {
         if (shouldRestrictVisibility()) {
             Helpers.validateSelection(where, sAppReadableColumnsSet);
         }
@@ -1161,12 +1159,21 @@ public final class DownloadProvider extends ContentProvider {
                 deleteRequestHeaders(db, selection.getSelection(), selection.getParameters());
 
                 final Cursor cursor = db.query(DB_TABLE, new String[] {
-                        Downloads.Impl._ID }, selection.getSelection(), selection.getParameters(),
-                        null, null, null);
+                        Downloads.Impl._ID, Downloads.Impl._DATA
+                }, selection.getSelection(), selection.getParameters(), null, null, null);
                 try {
                     while (cursor.moveToNext()) {
                         final long id = cursor.getLong(0);
                         DownloadStorageProvider.onDownloadProviderDelete(getContext(), id);
+
+                        final String path = cursor.getString(1);
+                        if (!TextUtils.isEmpty(path)) {
+                            final File file = new File(path);
+                            if (Helpers.isFilenameValid(getContext(), file)) {
+                                Log.v(Constants.TAG, "Deleting " + file + " via provider delete");
+                                file.delete();
+                            }
+                        }
                     }
                 } finally {
                     IoUtils.closeQuietly(cursor);