1 /** 2 * Copyright (C) 2013 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mail.print; 19 20 import android.content.Context; 21 import android.content.res.Resources; 22 23 import com.android.mail.R; 24 import com.android.mail.providers.Conversation; 25 import com.android.mail.ui.AbstractHtmlTemplates; 26 import com.android.mail.utils.LogTag; 27 import com.android.mail.utils.LogUtils; 28 29 /** 30 * Renders data into very simple string-substitution HTML templates for printing conversations. 31 */ 32 public class HtmlPrintTemplates extends AbstractHtmlTemplates { 33 34 private static final String TAG = LogTag.getLogTag(); 35 36 private final String mConversationUpper; 37 private final String mMessage; 38 private final String mConversationLower; 39 private final String mConversationLowerNoJs; 40 private final String mLogo; 41 42 public HtmlPrintTemplates(Context context) { 43 super(context); 44 45 mConversationUpper = readTemplate(R.raw.template_print_conversation_upper); 46 mMessage = readTemplate(R.raw.template_print_message); 47 mConversationLower = readTemplate(R.raw.template_print_conversation_lower); 48 mConversationLowerNoJs = readTemplate(R.raw.template_print_conversation_lower_no_js); 49 mLogo = readTemplate(R.raw.logo); 50 } 51 52 /** 53 * Start building the html for a printed conversation. Can only be called once 54 * until {@link #endPrintConversation()} or {@link #endPrintConversationNoJavascript()} 55 * is called. 56 */ 57 public void startPrintConversation(String subject, int numMessages) { 58 if (mInProgress) { 59 throw new IllegalStateException("Should not call startPrintConversation twice"); 60 } 61 62 reset(); 63 64 final Resources res = mContext.getResources(); 65 final String numMessageString = res.getQuantityString( 66 R.plurals.num_messages, numMessages, numMessages); 67 68 final String printedSubject = 69 Conversation.getSubjectForDisplay(mContext, null /* badgeText */, subject); 70 71 append(mConversationUpper, mLogo, mContext.getString(R.string.app_name), 72 printedSubject, numMessageString); 73 74 mInProgress = true; 75 } 76 77 /** 78 * Add a message to the html for this printed conversation. 79 */ 80 public void appendMessage(String senderName, String senderAddress, String date, 81 String recipients, String bodyHtml, String attachments) { 82 append(mMessage, senderName, senderAddress, date, recipients, bodyHtml, attachments); 83 } 84 85 /** 86 * Adds the end of the printed conversation to the html. NOTE: this method 87 * includes JavaScript. If you need a version without JavaScript, 88 * use {@link #endPrintConversationNoJavascript()}.<br/><br/> 89 * 90 * One example where we use JavaScript is to hide quoted text. 91 * 92 * @return a {@link String} containing the html for the conversation. 93 */ 94 public String endPrintConversation() { 95 if (!mInProgress) { 96 throw new IllegalStateException("must call startConversation first"); 97 } 98 99 append(mConversationLower, mContext.getString(R.string.quoted_text_hidden_print)); 100 101 mInProgress = false; 102 103 LogUtils.d(TAG, "rendered conversation of %d bytes, buffer capacity=%d", 104 mBuilder.length() << 1, mBuilder.capacity() << 1); 105 106 return emit(); 107 } 108 109 /** 110 * Adds the end of the printed conversation to the html. NOTE: this method 111 * does not include any JavaScript. If you need a version with JavaScript, 112 * use {@link #endPrintConversation()}. 113 * @return a {@link String} containing the html for the conversation. 114 */ 115 public String endPrintConversationNoJavascript() { 116 if (!mInProgress) { 117 throw new IllegalStateException("must call startConversation first"); 118 } 119 120 append(mConversationLowerNoJs); 121 122 mInProgress = false; 123 124 LogUtils.d(TAG, "rendered conversation of %d bytes, buffer capacity=%d", 125 mBuilder.length() << 1, mBuilder.capacity() << 1); 126 127 return emit(); 128 } 129 } 130