Print list of attachments.

Doesn't print attachment thumbnails for
image attachments. b/10712542

Change-Id: I5202338f60ddeb35238fce9d79691ba3c7a29e89
diff --git a/assets/images/doc.gif b/assets/images/doc.gif
new file mode 100644
index 0000000..7e053f5
--- /dev/null
+++ b/assets/images/doc.gif
Binary files differ
diff --git a/assets/images/generic.gif b/assets/images/generic.gif
new file mode 100644
index 0000000..3be53cc
--- /dev/null
+++ b/assets/images/generic.gif
Binary files differ
diff --git a/assets/images/graphic.gif b/assets/images/graphic.gif
new file mode 100644
index 0000000..50e10d2
--- /dev/null
+++ b/assets/images/graphic.gif
Binary files differ
diff --git a/assets/images/html.gif b/assets/images/html.gif
new file mode 100644
index 0000000..6302a01
--- /dev/null
+++ b/assets/images/html.gif
Binary files differ
diff --git a/assets/images/pdf.gif b/assets/images/pdf.gif
new file mode 100644
index 0000000..28c76b8
--- /dev/null
+++ b/assets/images/pdf.gif
Binary files differ
diff --git a/assets/images/ppt.gif b/assets/images/ppt.gif
new file mode 100644
index 0000000..4f0c034
--- /dev/null
+++ b/assets/images/ppt.gif
Binary files differ
diff --git a/assets/images/sound.gif b/assets/images/sound.gif
new file mode 100644
index 0000000..ed8c179
--- /dev/null
+++ b/assets/images/sound.gif
Binary files differ
diff --git a/assets/images/txt.gif b/assets/images/txt.gif
new file mode 100644
index 0000000..a25eb72
--- /dev/null
+++ b/assets/images/txt.gif
Binary files differ
diff --git a/assets/images/xls.gif b/assets/images/xls.gif
new file mode 100644
index 0000000..9f5d57b
--- /dev/null
+++ b/assets/images/xls.gif
Binary files differ
diff --git a/assets/images/zip.gif b/assets/images/zip.gif
new file mode 100644
index 0000000..170ece8
--- /dev/null
+++ b/assets/images/zip.gif
Binary files differ
diff --git a/res/raw/template_border.html b/res/raw/template_border.html
index 3669d50..8478cbd 100644
--- a/res/raw/template_border.html
+++ b/res/raw/template_border.html
@@ -1,17 +1 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 <div class="mail-border spacer" style="height: %spx;"></div>
diff --git a/res/raw/template_conversation_lower.html b/res/raw/template_conversation_lower.html
index 9b08dba..9c18cf5 100644
--- a/res/raw/template_conversation_lower.html
+++ b/res/raw/template_conversation_lower.html
@@ -1,19 +1,3 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 <div id="initial-load-signal" class="%s"></div>
 </body>
 <script type="text/javascript">
diff --git a/res/raw/template_conversation_upper.html b/res/raw/template_conversation_upper.html
index fde8985..fb69324 100644
--- a/res/raw/template_conversation_upper.html
+++ b/res/raw/template_conversation_upper.html
@@ -1,19 +1,3 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 <!DOCTYPE html>
 <html>
 <head>
diff --git a/res/raw/template_message.html b/res/raw/template_message.html
index 8aa8e00..8195f7d 100644
--- a/res/raw/template_message.html
+++ b/res/raw/template_message.html
@@ -1,19 +1,3 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 <div id="%s" class="mail-message %s">
     <div class="mail-message-header spacer" style="height: %spx;"></div>
     <div class="mail-message-content collapsible zoom-normal %s" style="display: %s; margin: 16px 0;">%s</div>
diff --git a/res/raw/template_print_conversation_lower.html b/res/raw/template_print_conversation_lower.html
index 6a8c660..bd6a12d 100644
--- a/res/raw/template_print_conversation_lower.html
+++ b/res/raw/template_print_conversation_lower.html
@@ -1,19 +1,3 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 </div>
 </div>
 </body>
diff --git a/res/raw/template_print_conversation_lower_no_js.html b/res/raw/template_print_conversation_lower_no_js.html
index e6e704c..e689309 100644
--- a/res/raw/template_print_conversation_lower_no_js.html
+++ b/res/raw/template_print_conversation_lower_no_js.html
@@ -1,19 +1,3 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 </div>
 </div>
 </body>
diff --git a/res/raw/template_print_conversation_upper.html b/res/raw/template_print_conversation_upper.html
index 8660c83..62e68bf 100644
--- a/res/raw/template_print_conversation_upper.html
+++ b/res/raw/template_print_conversation_upper.html
@@ -65,7 +65,7 @@
         <table width=100%% cellpadding=0 cellspacing=0 border=0>
             <tr height=14px>
                 <td width=143>
-                    <img src="file:///android_res/drawable/logo"
+                    <img src="file:///android_asset/images/ic_launcher_mail.png"
                          width=143 height=59 alt="%s">
                 </td>
                 <td align=right>
diff --git a/res/raw/template_print_message.html b/res/raw/template_print_message.html
index ded5075..5eb149c 100644
--- a/res/raw/template_print_message.html
+++ b/res/raw/template_print_message.html
@@ -1,19 +1,3 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 <hr>
 <table width=100%% cellpadding=0 cellspacing=0 border=0 class="message">
     <tr>
diff --git a/res/raw/template_super_collapsed.html b/res/raw/template_super_collapsed.html
index 5ac9dac..386bbae 100644
--- a/res/raw/template_super_collapsed.html
+++ b/res/raw/template_super_collapsed.html
@@ -1,17 +1 @@
-<!--
-     Copyright (C) 2013 Google Inc.
-     Licensed to The Android Open Source Project.
-
-     Licensed under the Apache License, Version 2.0 (the "License");
-     you may not use this file except in compliance with the License.
-     You may obtain a copy of the License at
-
-          http://www.apache.org/licenses/LICENSE-2.0
-
-     Unless required by applicable law or agreed to in writing, software
-     distributed under the License is distributed on an "AS IS" BASIS,
-     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-     See the License for the specific language governing permissions and
-     limitations under the License.
--->
 <div class="mail-super-collapsed-block spacer" index="%s" style="height: %spx;"></div>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 66d093a..00063d8 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -955,5 +955,10 @@
     <string name="draft_heading">Draft</string>
     <!-- Shown to inform the user that the quoted text for this message has been hidden. [CHAR LIMIT=50] -->
     <string name="quoted_text_hidden_print">Quoted text hidden</string>
+    <!-- Number of attachments in a message [CHAR LIMIT=30]-->
+    <plurals name="num_attachments">
+        <item quantity="one"><xliff:g id="count">%1$d</xliff:g> attachment</item>
+        <item quantity="other"><xliff:g id="count">%1$d</xliff:g> attachments</item>
+    </plurals>
 
 </resources>
diff --git a/src/com/android/mail/print/Printer.java b/src/com/android/mail/print/Printer.java
index 7744c37..0a3cd49 100644
--- a/src/com/android/mail/print/Printer.java
+++ b/src/com/android/mail/print/Printer.java
@@ -27,10 +27,13 @@
 import com.android.mail.browse.MessageCursor;
 import com.android.mail.providers.Account;
 import com.android.mail.providers.Address;
+import com.android.mail.providers.Attachment;
 import com.android.mail.providers.Conversation;
 import com.android.mail.providers.UIProvider;
+import com.android.mail.utils.AttachmentUtils;
 import com.android.mail.utils.Utils;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -70,7 +73,7 @@
 
             templates.appendMessage(fromAddress.getName(), fromAddress.getAddress(), date,
                     renderRecipients(res, addressCache, message), message.getBodyAsHtml(),
-                    ""); // TODO - attachment html
+                    renderAttachments(context, res, message));
         } while (cursor.moveToNext());
 
         // only include JavaScript if specifically requested
@@ -155,4 +158,86 @@
 
         return TextUtils.join(", ", formattedEmails);
     }
+
+    /**
+     * Builds and returns html for a message's attachments.
+     */
+    private static String renderAttachments(
+            Context context, Resources resources, ConversationMessage message) {
+        if (!message.hasAttachments) {
+            return "";
+        }
+
+        final List<Attachment> attachments = message.getAttachments();
+        final StringBuilder sb = new StringBuilder("<br clear=all>"
+                + "<div style=\"width:50%;border-top:2px #AAAAAA solid\"></div>"
+                + "<table class=att cellspacing=0 cellpadding=5 border=0>");
+
+        // If the message has more than one attachment, list the number of attachments.
+        final int numAttachments = attachments.size();
+        if (numAttachments > 1) {
+            sb.append("<tr><td colspan=2><b style=\"padding-left:3\">")
+                    .append(resources.getQuantityString(
+                            R.plurals.num_attachments, numAttachments, numAttachments))
+                    .append("</b></td></tr>");
+        }
+
+        for (final Attachment attachment : attachments) {
+            sb.append("<tr><td><table cellspacing=\"0\" cellpadding=\"0\"><tr>");
+
+            // TODO - thumbnail previews of images
+
+            sb.append("<td><img width=\"16\" height=\"16\" src=\"file:///android_asset/images/")
+                    .append(getIconFilename(attachment.getContentType()))
+                    .append("\"></td><td width=\"7\"></td><td><b>")
+                    .append(attachment.getName())
+                    .append("</b><br>").append(
+                    AttachmentUtils.convertToHumanReadableSize(context, attachment.size))
+                    .append("</td></tr></table></td></tr>");
+        }
+
+        sb.append("</table>");
+
+        return sb.toString();
+    }
+
+    /**
+     * Returns an appropriate filename for various attachment mime types.
+     */
+    private static String getIconFilename(String mimeType) {
+        if (mimeType.startsWith("application/msword") ||
+                mimeType.startsWith("application/vnd.oasis.opendocument.text") ||
+                mimeType.equals("application/rtf") ||
+                mimeType.equals("application/"
+                        + "vnd.openxmlformats-officedocument.wordprocessingml.document")) {
+            return "doc.gif";
+        } else if (mimeType.startsWith("image/")) {
+            return "graphic.gif";
+        } else if (mimeType.startsWith("text/html")) {
+            return "html.gif";
+        } else if (mimeType.startsWith("application/pdf")) {
+            return "pdf.gif";
+        } else if (mimeType.endsWith("powerpoint") ||
+                mimeType.equals("application/vnd.oasis.opendocument.presentation") ||
+                mimeType.equals("application/"
+                        + "vnd.openxmlformats-officedocument.presentationml.presentation")) {
+            return "ppt.gif";
+        } else if ((mimeType.startsWith("audio/")) ||
+                (mimeType.startsWith("music/"))) {
+            return "sound.gif";
+        } else if (mimeType.startsWith("text/plain")) {
+            return "txt.gif";
+        } else if (mimeType.endsWith("excel") ||
+                mimeType.equals("application/vnd.oasis.opendocument.spreadsheet") ||
+                mimeType.equals("application/"
+                        + "vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
+            return "xls.gif";
+        } else if ((mimeType.endsWith("zip")) ||
+                (mimeType.endsWith("/x-compress")) ||
+                (mimeType.endsWith("/x-compressed"))) {
+            return "zip.gif";
+        } else {
+            return "generic.gif";
+        }
+    }
 }
diff --git a/src/com/android/mail/ui/ConversationViewFragment.java b/src/com/android/mail/ui/ConversationViewFragment.java
index 51484b1..e6b9a1d 100644
--- a/src/com/android/mail/ui/ConversationViewFragment.java
+++ b/src/com/android/mail/ui/ConversationViewFragment.java
@@ -1584,10 +1584,12 @@
         final String convHtml =
                 Printer.print(getContext(), mAccount, getMessageCursor(),
                         mAddressCache, true /* userJavascript */);
-        mWebView.loadDataWithBaseURL(mBaseUri, convHtml, "text/html", "utf-8", null);
+        mWebView.getSettings().setBlockNetworkImage(false);
+        mWebView.loadDataWithBaseURL(
+                mConversation.getBaseUri(mBaseUri), convHtml, "text/html", "utf-8", null);
         final PrintManager printManager =
                 (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
-        printManager.print(getConversation().subject,
+        printManager.print(mConversation.subject,
                 mWebView.createPrintDocumentAdapter(),
                 new PrintAttributes.Builder().build());
     }