make sure filename is non-null before delting it.
Vasu Nori [Fri, 29 Oct 2010 20:34:14 +0000 (13:34 -0700)]
if a download fails for some reason, then filename could be null.
move Helpers.deleteFile() code into private method in DownloadService

Change-Id: Ifb12a84b6185e19c70d62392e102950c07f56a01

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

index d5dff56..9bcfe7f 100644 (file)
@@ -370,21 +370,20 @@ public class DownloadService extends Service {
                                 if (!scanFile(info, false, true)) {
                                     throw new IllegalStateException("scanFile failed!");
                                 }
-                            } else {
-                                // this file should NOT be scanned. delete the file.
-                                Helpers.deleteFile(getContentResolver(), info.mId, info.mFileName,
-                                        info.mMimeType);
+                                continue;
                             }
                         } else {
                             // yes it has mediaProviderUri column already filled in.
-                            // delete it from MediaProvider database and then from downloads table
-                            // in DownProvider database (the order of deletion is important).
+                            // delete it from MediaProvider database.
                             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)});
                         }
+                        // delete the file
+                        deleteFileIfExists(info.mFileName);
+                        // delete from the downloads db
+                        getContentResolver().delete(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,
+                                Downloads.Impl._ID + " = ? ",
+                                new String[]{String.valueOf(info.mId)});
                     }
                 }
             }
@@ -601,7 +600,11 @@ public class DownloadService extends Service {
                                         getContentResolver().delete(uri, null, null);
                                     }
                                     // delete the file and delete its row from the downloads db
-                                    Helpers.deleteFile(resolver, id, path, mimeType);
+                                    deleteFileIfExists(path);
+                                    getContentResolver().delete(
+                                            Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI,
+                                            Downloads.Impl._ID + " = ? ",
+                                            new String[]{String.valueOf(id)});
                                 }
                             }
                         });
@@ -612,4 +615,15 @@ public class DownloadService extends Service {
             }
         }
     }
+
+    private void deleteFileIfExists(String path) {
+        try {
+            if (!TextUtils.isEmpty(path)) {
+                File file = new File(path);
+                file.delete();
+            }
+        } catch (Exception e) {
+            Log.w(Constants.TAG, "file: '" + path + "' couldn't be deleted", e);
+        }
+    }
 }
index 2d571ea..59cc97c 100644 (file)
@@ -16,7 +16,6 @@
 
 package com.android.providers.downloads;
 
-import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.Context;
 import android.content.Intent;
@@ -852,19 +851,4 @@ public class Helpers {
         }
         return sb.toString();
     }
-
-    /*
-     * Delete the given file from device
-     * and delete its row from the downloads database.
-     */
-    /* package */ static void deleteFile(ContentResolver resolver, long id, String path, String mimeType) {
-        try {
-            File file = new File(path);
-            file.delete();
-        } catch (Exception e) {
-            Log.w(Constants.TAG, "file: '" + path + "' couldn't be deleted", e);
-        }
-        resolver.delete(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, Downloads.Impl._ID + " = ? ",
-                new String[]{String.valueOf(id)});
-    }
 }