diff --git a/app/src/main/java/eu/faircode/email/AdapterMessage.java b/app/src/main/java/eu/faircode/email/AdapterMessage.java index 1ee42e78e5..52ffe6634f 100644 --- a/app/src/main/java/eu/faircode/email/AdapterMessage.java +++ b/app/src/main/java/eu/faircode/email/AdapterMessage.java @@ -38,6 +38,9 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; +import android.print.PrintManager; import android.provider.ContactsContract; import android.text.Editable; import android.text.Html; @@ -237,6 +240,8 @@ public class AdapterMessage extends RecyclerView.Adapter> liveAttachments = null; private Observer> observerAttachments = null; + private WebView printWebView = null; + ViewHolder(final View itemView) { super(itemView); @@ -1364,38 +1369,7 @@ public class AdapterMessage extends RecyclerView.Adapter'; - EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); - if (attachment != null && attachment.available) { - File file = EntityAttachment.getFile(context, attachment.id); - try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { - byte[] bytes = new byte[(int) file.length()]; - if (is.read(bytes) != bytes.length) - throw new IOException("length"); - - StringBuilder sb = new StringBuilder(); - sb.append("data:"); - sb.append(attachment.type); - sb.append(";base64,"); - sb.append(Base64.encodeToString(bytes, Base64.DEFAULT)); - - img.attr("src", sb.toString()); - } - } - } - } catch (Throwable ex) { - Log.e(ex); - } - - return doc.html(); + return getHtmlEmbedded(id); } @Override @@ -2022,6 +1996,51 @@ public class AdapterMessage extends RecyclerView.Adapter() { + @Override + protected String onExecute(Context context, Bundle args) throws Throwable { + long id = args.getLong("id"); + return getHtmlEmbedded(id); + } + + @Override + protected void onExecuted(Bundle args, String html) { + // https://developer.android.com/training/printing/html-docs.html + printWebView = new WebView(context); + WebSettings settings = printWebView.getSettings(); + settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); + + printWebView.setWebViewClient(new WebViewClient() { + public boolean shouldOverrideUrlLoading(WebView view, String url) { + return false; + } + + @Override + public void onPageFinished(WebView view, String url) { + PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE); + String jobName = context.getString(R.string.app_name); + if (!TextUtils.isEmpty(data.message.subject)) + jobName += " - " + data.message.subject; + PrintDocumentAdapter adapter = printWebView.createPrintDocumentAdapter(jobName); + printManager.print(jobName, adapter, new PrintAttributes.Builder().build()); + printWebView = null; + } + }); + + printWebView.loadDataWithBaseURL("email://", html, "text/html", "UTF-8", null); + } + + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(context, owner, ex); + } + }.execute(context, owner, args, "message:print"); + } + private void onMenuShowHeaders(ActionData data) { boolean show_headers = !properties.getValue("headers", data.message.id); properties.setValue("headers", data.message.id, show_headers); @@ -2265,6 +2284,7 @@ public class AdapterMessage extends RecyclerView.Adapter'; + EntityAttachment attachment = DB.getInstance(context).attachment().getAttachment(id, cid); + if (attachment != null && attachment.available) { + File file = EntityAttachment.getFile(context, attachment.id); + try (InputStream is = new BufferedInputStream(new FileInputStream(file))) { + byte[] bytes = new byte[(int) file.length()]; + if (is.read(bytes) != bytes.length) + throw new IOException("length"); + + StringBuilder sb = new StringBuilder(); + sb.append("data:"); + sb.append(attachment.type); + sb.append(";base64,"); + sb.append(Base64.encodeToString(bytes, Base64.DEFAULT)); + + img.attr("src", sb.toString()); + } + } + } + } + + return doc.html(); + } + ItemDetailsLookup.ItemDetails getItemDetails(@NonNull MotionEvent motionEvent) { return new ItemDetailsMessage(this); } diff --git a/app/src/main/res/menu/menu_message.xml b/app/src/main/res/menu/menu_message.xml index 233799d9e4..7110036e64 100644 --- a/app/src/main/res/menu/menu_message.xml +++ b/app/src/main/res/menu/menu_message.xml @@ -32,6 +32,10 @@ android:id="@+id/menu_share" android:title="@string/title_share" /> + + Forward Create rule … Share + Print Show headers Download raw message Save raw message