|
|
|
@ -39,7 +39,6 @@ import android.Manifest;
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.content.ActivityNotFoundException;
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
@ -71,10 +70,6 @@ import android.os.OperationCanceledException;
|
|
|
|
|
import android.os.Parcel;
|
|
|
|
|
import android.os.Parcelable;
|
|
|
|
|
import android.os.SystemClock;
|
|
|
|
|
import android.print.PrintAttributes;
|
|
|
|
|
import android.print.PrintDocumentAdapter;
|
|
|
|
|
import android.print.PrintJob;
|
|
|
|
|
import android.print.PrintManager;
|
|
|
|
|
import android.provider.CalendarContract;
|
|
|
|
|
import android.provider.ContactsContract;
|
|
|
|
|
import android.provider.Settings;
|
|
|
|
@ -85,7 +80,6 @@ import android.text.Layout;
|
|
|
|
|
import android.text.Spannable;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
|
import android.text.Spanned;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
|
import android.text.format.DateUtils;
|
|
|
|
@ -113,9 +107,7 @@ import android.view.animation.Animation;
|
|
|
|
|
import android.view.animation.AnimationUtils;
|
|
|
|
|
import android.view.animation.TranslateAnimation;
|
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
|
import android.webkit.WebView;
|
|
|
|
|
import android.webkit.WebViewClient;
|
|
|
|
|
import android.widget.ImageButton;
|
|
|
|
|
import android.widget.SeekBar;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
@ -183,9 +175,6 @@ import org.bouncycastle.cms.jcajce.JceKeyTransRecipient;
|
|
|
|
|
import org.bouncycastle.operator.DefaultAlgorithmNameFinder;
|
|
|
|
|
import org.bouncycastle.util.Store;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
import org.openintents.openpgp.AutocryptPeerUpdate;
|
|
|
|
|
import org.openintents.openpgp.OpenPgpError;
|
|
|
|
|
import org.openintents.openpgp.OpenPgpSignatureResult;
|
|
|
|
@ -201,9 +190,6 @@ import java.io.InputStream;
|
|
|
|
|
import java.io.OutputStream;
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
import java.math.BigInteger;
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.security.KeyStore;
|
|
|
|
|
import java.security.PrivateKey;
|
|
|
|
|
import java.security.cert.CertPathBuilder;
|
|
|
|
@ -237,9 +223,7 @@ import java.util.Locale;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
@ -336,8 +320,6 @@ public class FragmentMessages extends FragmentBase
|
|
|
|
|
private int searchIndex = 0;
|
|
|
|
|
private TextView searchView = null;
|
|
|
|
|
|
|
|
|
|
private WebView printWebView = null;
|
|
|
|
|
|
|
|
|
|
private boolean hide_toolbar;
|
|
|
|
|
private boolean cards;
|
|
|
|
|
private boolean dividers;
|
|
|
|
@ -9962,264 +9944,7 @@ public class FragmentMessages extends FragmentBase
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onPrint(Bundle args) {
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
|
|
|
|
boolean print_html_header = prefs.getBoolean("print_html_header", true);
|
|
|
|
|
boolean print_html_images = prefs.getBoolean("print_html_images", true);
|
|
|
|
|
|
|
|
|
|
args.putBoolean("print_html_header", print_html_header);
|
|
|
|
|
args.putBoolean("print_html_images", print_html_images);
|
|
|
|
|
|
|
|
|
|
new SimpleTask<String[]>() {
|
|
|
|
|
@Override
|
|
|
|
|
protected String[] onExecute(Context context, Bundle args) throws IOException {
|
|
|
|
|
long id = args.getLong("id");
|
|
|
|
|
boolean headers = args.getBoolean("headers");
|
|
|
|
|
boolean print_html_header = args.getBoolean("print_html_header");
|
|
|
|
|
boolean print_html_images = args.getBoolean("print_html_images");
|
|
|
|
|
CharSequence selected = args.getCharSequence("selected");
|
|
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
|
int timeout = prefs.getInt("timeout", ImageHelper.DOWNLOAD_TIMEOUT) * 1000;
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
|
|
|
|
if (message == null || !message.content)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
File file = message.getFile(context);
|
|
|
|
|
if (!file.exists())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
|
if (attachments == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
Document document;
|
|
|
|
|
if (!TextUtils.isEmpty(selected) && selected instanceof Spanned)
|
|
|
|
|
document = JsoupEx.parse(HtmlHelper.toHtml((Spanned) selected, context));
|
|
|
|
|
else
|
|
|
|
|
document = JsoupEx.parse(file);
|
|
|
|
|
|
|
|
|
|
boolean monospaced_pre = prefs.getBoolean("monospaced_pre", false);
|
|
|
|
|
if (message.isPlainOnly() && monospaced_pre)
|
|
|
|
|
HtmlHelper.restorePre(document);
|
|
|
|
|
|
|
|
|
|
HtmlHelper.markText(document);
|
|
|
|
|
|
|
|
|
|
HtmlHelper.embedInlineImages(context, id, document, true);
|
|
|
|
|
|
|
|
|
|
// onPageFinished will not be called if not all images can be loaded
|
|
|
|
|
File dir = new File(context.getFilesDir(), "images");
|
|
|
|
|
List<Future<Void>> futures = new ArrayList<>();
|
|
|
|
|
Elements imgs = document.select("img");
|
|
|
|
|
for (int i = 0; i < imgs.size(); i++) {
|
|
|
|
|
Element img = imgs.get(i);
|
|
|
|
|
String src = img.attr("src");
|
|
|
|
|
if (src.startsWith("http:") || src.startsWith("https:")) {
|
|
|
|
|
final File out = new File(dir, id + "." + i + ".print");
|
|
|
|
|
img.attr("src", "file:" + out.getAbsolutePath());
|
|
|
|
|
|
|
|
|
|
if (print_html_images) {
|
|
|
|
|
if (out.exists() && out.length() > 0)
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
out.delete();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
futures.add(Helper.getDownloadTaskExecutor().submit(new Callable<Void>() {
|
|
|
|
|
@Override
|
|
|
|
|
public Void call() throws Exception {
|
|
|
|
|
try (OutputStream os = new FileOutputStream(out)) {
|
|
|
|
|
URL url = new URL(src);
|
|
|
|
|
Log.i("Caching url=" + url);
|
|
|
|
|
|
|
|
|
|
HttpURLConnection connection = null;
|
|
|
|
|
try {
|
|
|
|
|
connection = ConnectionHelper.openConnectionUnsafe(context, src, timeout, timeout);
|
|
|
|
|
Helper.copy(connection.getInputStream(), os);
|
|
|
|
|
} finally {
|
|
|
|
|
if (connection != null)
|
|
|
|
|
connection.disconnect();
|
|
|
|
|
}
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
Log.w(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Future<Void> 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;}
|
|
|
|
|
// <body><div class=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 (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) {
|
|
|
|
|
DateFormat DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.LONG, SimpleDateFormat.LONG);
|
|
|
|
|
|
|
|
|
|
Element span = document.createElement("span");
|
|
|
|
|
Element strong = document.createElement("strong");
|
|
|
|
|
strong.text(context.getString(R.string.title_received));
|
|
|
|
|
span.appendChild(strong);
|
|
|
|
|
span.appendText(" " + DTF.format(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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new String[]{message.subject, document.body().html()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onExecuted(Bundle args, final String[] data) {
|
|
|
|
|
if (data == null) {
|
|
|
|
|
Log.w("Print no data");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ActivityBase activity = (ActivityBase) getActivity();
|
|
|
|
|
if (activity == null) {
|
|
|
|
|
Log.w("Print no activity");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final Context context = activity.getOriginalContext();
|
|
|
|
|
boolean print_html_images = args.getBoolean("print_html_images");
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
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 = getString(R.string.app_name);
|
|
|
|
|
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 {
|
|
|
|
|
Log.unexpectedError(getParentFragmentManager(), ex, !(ex instanceof ActivityNotFoundException));
|
|
|
|
|
} catch (IllegalStateException exex) {
|
|
|
|
|
ToastEx.makeText(context, Log.formatThrowable(ex), Toast.LENGTH_LONG).show();
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
printWebView = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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(getParentFragmentManager(), ex);
|
|
|
|
|
}
|
|
|
|
|
}.execute(this, args, "message:print");
|
|
|
|
|
FragmentDialogPrint.print((ActivityBase) getActivity(), getParentFragmentManager(), args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onEmptyFolder(Bundle args) {
|
|
|
|
|