Replace clearing an item from the downloads list with deleting the item.

Fix for http://b/issue?id=2367238
diff --git a/res/menu/downloadhistorycontextfinished.xml b/res/menu/downloadhistorycontextfinished.xml
index 2714b5b..8d1fdd0 100644
--- a/res/menu/downloadhistorycontextfinished.xml
+++ b/res/menu/downloadhistorycontextfinished.xml
@@ -18,7 +18,7 @@
 
     <item android:id="@+id/download_menu_open"
         android:title="@string/download_menu_open" />
-    <item android:id="@+id/download_menu_clear"
-        android:title="@string/download_menu_clear" />
+    <item android:id="@+id/download_menu_delete"
+        android:title="@string/download_menu_delete" />
 
 </menu>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7ee1164..d81e38d 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -569,6 +569,8 @@
     <!-- Context menu item on Download history screen -->
     <string name="download_menu_clear">Clear from list</string>
     <!-- Context menu item in Download history screen -->
+    <string name="download_menu_delete">Delete</string>
+    <!-- Context menu item in Download history screen -->
     <string name="download_menu_cancel">Cancel download</string>
     <!-- Menu item -->
     <string name="download_menu_cancel_all">Cancel all downloads</string>
@@ -582,6 +584,8 @@
     <string name="download_cancel_dlg_title">Cancel downloads</string>
     <!-- Confirmation dialog message -->
     <string name="download_cancel_dlg_msg">All <xliff:g id="download_count">%d</xliff:g> downloads will be canceled and cleared from the download history.</string>
+    <!-- Confirmation dialog title -->
+    <string name="download_delete_file">File will be deleted</string>
     <!-- Dialog title -->
     <string name="download_file_error_dlg_title">Out of space</string>
     <!-- Dialog message -->
diff --git a/src/com/android/browser/BrowserDownloadPage.java b/src/com/android/browser/BrowserDownloadPage.java
index 979e9f1..3483c30 100644
--- a/src/com/android/browser/BrowserDownloadPage.java
+++ b/src/com/android/browser/BrowserDownloadPage.java
@@ -26,9 +26,11 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.database.Cursor;
+import android.database.DatabaseUtils;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.Downloads;
+import android.provider.MediaStore;
 import android.view.ContextMenu;
 import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
@@ -140,6 +142,37 @@
         return false;
     }
 
+    /**
+     * Remove the file from the list of downloads.
+     * @param id Unique ID of the download to remove.
+     */
+    private void clearFromDownloads(long id) {
+        getContentResolver().delete(ContentUris.withAppendedId(
+                Downloads.Impl.CONTENT_URI, id), null, null);
+    }
+
+    /**
+     * Remove the file from the SD card
+     * @param filename Name of the file to delete.
+     * @param mimetype Mimetype of the file to delete.
+     * @return boolean True on success, false on failure.
+     */
+    private boolean deleteFile(String filename, String mimetype) {
+        Uri uri;
+        if (mimetype.startsWith("image")) {
+            uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
+        } else if (mimetype.startsWith("audio")) {
+            uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
+        } else if (mimetype.startsWith("video")) {
+            uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
+        } else {
+            File file = new File(filename);
+            return file.delete();
+        }
+        return getContentResolver().delete(uri, MediaStore.MediaColumns.DATA
+                + " = " + DatabaseUtils.sqlEscapeString(filename), null) > 0;
+    }
+
     @Override
     public boolean onContextItemSelected(MenuItem item) {
         if (!mDownloadAdapter.moveCursorToPackedChildPosition(
@@ -151,12 +184,37 @@
                 hideCompletedDownload();
                 openCurrentDownload();
                 return true;
-                
+
+            case R.id.download_menu_delete:
+                int filenameColumnId =
+                        mDownloadCursor.getColumnIndexOrThrow(Downloads._DATA);
+                final String filename = mDownloadCursor.getString(
+                        filenameColumnId);
+                int mimetypeColumnId = mDownloadCursor.getColumnIndexOrThrow(
+                        Downloads.Impl.COLUMN_MIME_TYPE);
+                final String mimetype = mDownloadCursor.getString(
+                        mimetypeColumnId);
+                final long id = mDownloadCursor.getLong(mIdColumnId);
+                new AlertDialog.Builder(this)
+                        .setTitle(R.string.download_delete_file)
+                        .setIcon(android.R.drawable.ic_dialog_alert)
+                        .setMessage(filename)
+                        .setNegativeButton(R.string.cancel, null)
+                        .setPositiveButton(R.string.ok,
+                                new DialogInterface.OnClickListener() {
+                                    public void onClick(DialogInterface dialog,
+                                            int whichButton) {
+                                        if (deleteFile(filename, mimetype)) {
+                                            clearFromDownloads(id);
+                                        }
+                                    }
+                                })
+                        .show();
+                break;
+
             case R.id.download_menu_clear:
             case R.id.download_menu_cancel:
-                getContentResolver().delete(
-                        ContentUris.withAppendedId(Downloads.Impl.CONTENT_URI,
-                        mDownloadCursor.getLong(mIdColumnId)), null, null);
+                clearFromDownloads(mDownloadCursor.getLong(mIdColumnId));
                 return true;
         }
         return false;