Merge "Do not show the downloads page without the user explicitly requesting it."
diff --git a/res/menu/downloadhistory.xml b/res/menu/downloadhistory.xml
index ee3b751..195b0db 100644
--- a/res/menu/downloadhistory.xml
+++ b/res/menu/downloadhistory.xml
@@ -16,9 +16,6 @@
 
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 
-    <item android:id="@+id/download_menu_clear_all"
-        android:title="@string/download_menu_clear_all" 
-        android:icon="@*android:drawable/ic_menu_clear_playlist" />
     <item android:id="@+id/download_menu_cancel_all"
         android:title="@string/download_menu_cancel_all" 
         android:icon="@android:drawable/ic_menu_close_clear_cancel" />
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..a2b144b 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;
@@ -120,9 +122,6 @@
     public boolean onPrepareOptionsMenu(Menu menu) {
         boolean showCancel = getCancelableCount() > 0;
         menu.findItem(R.id.download_menu_cancel_all).setEnabled(showCancel);
-        
-        boolean showClear = getClearableCount() > 0;
-        menu.findItem(R.id.download_menu_clear_all).setEnabled(showClear);
         return super.onPrepareOptionsMenu(menu);
     }
     
@@ -132,14 +131,41 @@
             case R.id.download_menu_cancel_all:
                 promptCancelAll();
                 return true;
-                
-            case R.id.download_menu_clear_all:
-                promptClearList();
-                return true;
         }
         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 +177,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;
@@ -242,25 +293,6 @@
     }
     
     /**
-     * Prompt the user if they would like to clear the download history
-     */
-    private void promptClearList() {
-        new AlertDialog.Builder(this)
-               .setTitle(R.string.download_clear_dlg_title)
-               .setIcon(R.drawable.ssl_icon)
-               .setMessage(R.string.download_clear_dlg_msg)
-               .setPositiveButton(R.string.ok, 
-                       new DialogInterface.OnClickListener() {
-                           public void onClick(DialogInterface dialog, 
-                                   int whichButton) {
-                               clearAllDownloads();
-                           }
-                       })
-                .setNegativeButton(R.string.cancel, null)
-                .show();
-    }
-    
-    /**
      * Return the number of items in the list that can be canceled.
      * @return count
      */
@@ -358,38 +390,7 @@
         }
         return count;
     }
-    
-    /**
-     * Clear all stopped downloads, ie canceled (though should not be
-     * there), error and success download items.
-     */
-    private void clearAllDownloads() {
-        if (mDownloadCursor.moveToFirst()) {
-            StringBuilder where = new StringBuilder();
-            boolean firstTime = true;
-            while (!mDownloadCursor.isAfterLast()) {
-                int status = mDownloadCursor.getInt(mStatusColumnId);
-                if (Downloads.Impl.isStatusCompleted(status)) {
-                    if (firstTime) {
-                        firstTime = false;
-                    } else {
-                        where.append(" OR ");
-                    }
-                    where.append("( ");
-                    where.append(Downloads.Impl._ID);
-                    where.append(" = '");
-                    where.append(mDownloadCursor.getLong(mIdColumnId));
-                    where.append("' )");
-                }
-                mDownloadCursor.moveToNext();
-            }
-            if (!firstTime) {
-                getContentResolver().delete(Downloads.Impl.CONTENT_URI,
-                        where.toString(), null);
-            }
-        }
-    }
-    
+
     /**
      * Open the content where the download db cursor currently is
      */