future : futures)
try {
future.get();
} catch (Throwable ex) {
Log.w(ex);
}
// @page WordSection1 {size:612.0pt 792.0pt; margin:70.85pt 70.85pt 70.85pt 70.85pt;}
// div.WordSection1 {page:WordSection1;}
//
for (Element element : document.body().select("div[class]")) {
String clazz = element.attr("class");
if (clazz.startsWith("WordSection"))
element.removeClass(clazz);
}
if (print_html_header) {
Element header = document.createElement("p");
if (draft) {
Element div = document.createElement("div");
div.attr("style", "text-align: center;");
Element strong = document.createElement("strong");
strong.text(context.getString(R.string.title_compose));
strong.attr("style", "text-transform: uppercase;");
div.appendChild(strong);
header.appendChild(div);
header.appendElement("hr");
}
if (message.from != null && message.from.length > 0) {
Element span = document.createElement("span");
Element strong = document.createElement("strong");
strong.text(context.getString(R.string.title_from));
span.appendChild(strong);
span.appendText(" " + MessageHelper.formatAddresses(message.from));
span.appendElement("br");
header.appendChild(span);
}
if (message.to != null && message.to.length > 0) {
Element span = document.createElement("span");
Element strong = document.createElement("strong");
strong.text(context.getString(R.string.title_to));
span.appendChild(strong);
span.appendText(" " + MessageHelper.formatAddresses(message.to));
span.appendElement("br");
header.appendChild(span);
}
if (message.cc != null && message.cc.length > 0) {
Element span = document.createElement("span");
Element strong = document.createElement("strong");
strong.text(context.getString(R.string.title_cc));
span.appendChild(strong);
span.appendText(" " + MessageHelper.formatAddresses(message.cc));
span.appendElement("br");
header.appendChild(span);
}
if (message.received != null && !draft) {
DateFormat DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.LONG, SimpleDateFormat.LONG);
boolean sent = (EntityFolder.SENT.equals(folder.type) && message.sent != null);
Element span = document.createElement("span");
Element strong = document.createElement("strong");
strong.text(context.getString(sent ? R.string.title_sent : R.string.title_received));
span.appendChild(strong);
span.appendText(" " + DTF.format(sent ? message.sent : message.received));
span.appendElement("br");
header.appendChild(span);
}
for (EntityAttachment attachment : attachments)
if (attachment.isAttachment()) {
Element span = document.createElement("span");
Element strong = document.createElement("strong");
strong.text(context.getString(R.string.title_attachment));
span.appendChild(strong);
if (!TextUtils.isEmpty(attachment.name))
span.appendText(" " + attachment.name);
if (attachment.size != null)
span.appendText(" " + Helper.humanReadableByteCount(attachment.size));
span.appendElement("br");
header.appendChild(span);
}
if (!TextUtils.isEmpty(message.subject)) {
Element span = document.createElement("span");
span.appendText(message.subject);
span.appendElement("br");
header.appendChild(span);
}
if (headers && message.headers != null) {
header.appendElement("hr");
Element pre = document.createElement("pre");
pre.text(message.headers);
header.appendChild(pre);
}
header.appendElement("hr").appendElement("br");
document.body().prependChild(header);
}
args.putLong("received", message.received);
return new String[]{message.subject, document.html()};
}
@Override
protected void onExecuted(Bundle args, final String[] data) {
if (data == null) {
Log.w("Print no data");
return;
}
final Context context = activity.getOriginalContext();
boolean print_html_images = args.getBoolean("print_html_images");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean safe_browsing = prefs.getBoolean("safe_browsing", false);
// https://developer.android.com/training/printing/html-docs.html
printWebView = new WebView(context);
WebSettings settings = printWebView.getSettings();
settings.setUserAgentString(WebViewEx.getUserAgent(context, printWebView));
settings.setLoadsImagesAutomatically(print_html_images);
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
if (WebViewEx.isFeatureSupported(context, WebViewFeature.SAFE_BROWSING_ENABLE))
WebSettingsCompat.setSafeBrowsingEnabled(settings, safe_browsing);
if (WebViewEx.isFeatureSupported(context, WebViewFeature.ATTRIBUTION_REGISTRATION_BEHAVIOR))
WebSettingsCompat.setAttributionRegistrationBehavior(settings, WebSettingsCompat.ATTRIBUTION_BEHAVIOR_DISABLED);
settings.setJavaScriptEnabled(false);
settings.setAllowFileAccess(true);
printWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i("Print page finished");
try {
if (printWebView == null) {
Log.w("Print no view");
return;
}
PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
String jobName = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.format(args.getLong("received"));
if (!TextUtils.isEmpty(data[0]))
jobName += " " + data[0];
Log.i("Print queue job=" + jobName);
PrintDocumentAdapter adapter = printWebView.createPrintDocumentAdapter(jobName);
PrintJob job = printManager.print(jobName, adapter, new PrintAttributes.Builder().build());
EntityLog.log(context, "Print queued job=" + job.getInfo());
} catch (Throwable ex) {
try {
// android.content.ActivityNotFoundException: No Activity found to handle null
// at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2206)
// at android.app.Activity.startIntentSenderForResultInner(Activity.java:6020)
// at android.app.Activity.startIntentSenderForResult(Activity.java:5983)
// at androidx.activity.ComponentActivity.startIntentSenderForResult(SourceFile:2)
// at android.app.Activity.startIntentSenderForResult(Activity.java:5938)
// at androidx.activity.ComponentActivity.startIntentSenderForResult(SourceFile:1)
// at android.app.Activity.startIntentSender(Activity.java:6186)
// at android.app.Activity.startIntentSender(Activity.java:6152)
// at android.print.PrintManager.print(PrintManager.java:538)
// at eu.faircode.email.FragmentDialogPrint$7$2.onPageFinished(SourceFile:127)
boolean report = !(ex instanceof ActivityNotFoundException);
if (ex instanceof ActivityNotFoundException)
ex = new Throwable("A system app or component required for printing is missing." +
" Is the print spooler still enabled?", ex);
Log.unexpectedError(fm, ex, report);
} catch (Throwable exex) {
Log.e(exex);
}
} finally {
printWebView = null;
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.w("Print error " + errorCode + ":" + description);
}
});
Log.i("Print load data");
printWebView.loadDataWithBaseURL("about:blank", data[1], "text/html", StandardCharsets.UTF_8.name(), null);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(fm, ex);
}
}.execute(activity, args, "print");
}
}