Fix attachments not downloading on 3g.

Add redownload menu item for photos so they can be retried if they ever get
stuck.

Change-Id: I0e3bd8014f81cf7e230e055f92d1442280307657
diff --git a/res/menu/photo_view_menu.xml b/res/menu/photo_view_menu.xml
index 5caeca4..d654d40 100644
--- a/res/menu/photo_view_menu.xml
+++ b/res/menu/photo_view_menu.xml
@@ -34,6 +34,10 @@
             android:id="@+id/menu_share_all"
             android:showAsAction="never"
             android:title="@string/menu_photo_share_all"/>
+        <item
+            android:id="@+id/menu_download_again"
+            android:showAsAction="never"
+            android:title="@string/download_again"/>
     </group>
 
 </menu>
\ No newline at end of file
diff --git a/src/com/android/mail/browse/AttachmentActionHandler.java b/src/com/android/mail/browse/AttachmentActionHandler.java
index 40a66db..02cec4a 100644
--- a/src/com/android/mail/browse/AttachmentActionHandler.java
+++ b/src/com/android/mail/browse/AttachmentActionHandler.java
@@ -122,7 +122,6 @@
     }
 
     public void startRedownloadingAttachment(Attachment attachment) {
-        showDownloadingDialog();
         final ContentValues params = new ContentValues(2);
         params.put(AttachmentColumns.STATE, AttachmentState.REDOWNLOADING);
         params.put(AttachmentColumns.DESTINATION, attachment.destination);
@@ -134,7 +133,7 @@
      * Displays a loading dialog to be used for downloading attachments.
      * Must be called on the UI thread.
      */
-    private void showDownloadingDialog() {
+    public void showDownloadingDialog() {
         final FragmentTransaction ft = mFragmentManager.beginTransaction();
         final Fragment prev = mFragmentManager.findFragmentByTag(PROGRESS_FRAGMENT_TAG);
         if (prev != null) {
diff --git a/src/com/android/mail/browse/MessageAttachmentBar.java b/src/com/android/mail/browse/MessageAttachmentBar.java
index 1bb6d2e..18efa86 100644
--- a/src/com/android/mail/browse/MessageAttachmentBar.java
+++ b/src/com/android/mail/browse/MessageAttachmentBar.java
@@ -176,6 +176,7 @@
             }
         } else if (res == R.id.download_again) {
             if (mAttachment.isPresentLocally()) {
+                mActionHandler.showDownloadingDialog();
                 mActionHandler.startRedownloadingAttachment(mAttachment);
             }
         } else if (res == R.id.cancel_attachment) {
diff --git a/src/com/android/mail/photo/MailPhotoViewActivity.java b/src/com/android/mail/photo/MailPhotoViewActivity.java
index ecceedf..8f4216e 100644
--- a/src/com/android/mail/photo/MailPhotoViewActivity.java
+++ b/src/com/android/mail/photo/MailPhotoViewActivity.java
@@ -58,6 +58,11 @@
     private MenuItem mSaveAllItem;
     private MenuItem mShareItem;
     private MenuItem mShareAllItem;
+    /**
+     * Only for attachments that are currently downloading. Attachments that failed show the
+     * retry button.
+     */
+    private MenuItem mDownloadAgainItem;
     private AttachmentActionHandler mActionHandler;
     private Menu mMenu;
 
@@ -121,6 +126,7 @@
         mSaveAllItem = mMenu.findItem(R.id.menu_save_all);
         mShareItem = mMenu.findItem(R.id.menu_share);
         mShareAllItem = mMenu.findItem(R.id.menu_share_all);
+        mDownloadAgainItem = mMenu.findItem(R.id.menu_download_again);
 
         return true;
     }
@@ -145,6 +151,7 @@
             mSaveItem.setEnabled(!attachment.isDownloading()
                     && attachment.canSave() && !attachment.isSavedToExternal());
             mShareItem.setEnabled(attachment.canShare());
+            mDownloadAgainItem.setEnabled(attachment.canSave() && attachment.isDownloading());
         } else {
             if (mMenu != null) {
                 mMenu.setGroupEnabled(R.id.photo_view_menu_group, false);
@@ -201,6 +208,9 @@
         } else if (itemId == R.id.menu_share_all) { // share all of the photos
             shareAllAttachments();
             return true;
+        } else if (itemId == R.id.menu_download_again) { // redownload the current photo
+            redownloadAttachment();
+            return true;
         } else {
             return super.onOptionsItemSelected(item);
         }
@@ -277,7 +287,7 @@
             retryButton.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
-                    downloadAttachment();
+                    redownloadAttachment();
                     emptyText.setVisibility(View.GONE);
                     retryButton.setVisibility(View.GONE);
                 }
@@ -294,13 +304,17 @@
     }
 
     /**
-     * Downloads the attachment.
+     * Redownloads the attachment.
      */
-    private void downloadAttachment() {
+    private void redownloadAttachment() {
         final Attachment attachment = getCurrentAttachment();
         if (attachment != null && attachment.canSave()) {
+            // REDOWNLOADING command is only for attachments that are finished or failed.
+            // For an attachment that is downloading (or paused in the DownloadManager), we need to
+            // cancel it first.
             mActionHandler.setAttachment(attachment);
-            mActionHandler.startDownloadingAttachment(AttachmentDestination.CACHE);
+            mActionHandler.cancelAttachment();
+            mActionHandler.startDownloadingAttachment(attachment.destination);
         }
     }