2019-02-06 13:07:13 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.AssetFileDescriptor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Bundle;
|
2019-02-06 16:16:06 +00:00
|
|
|
import android.text.Spanned;
|
2019-02-06 13:07:13 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2019-02-07 12:44:14 +00:00
|
|
|
import com.google.android.material.snackbar.Snackbar;
|
|
|
|
|
2019-02-06 13:07:13 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
|
|
|
|
import androidx.constraintlayout.widget.Group;
|
|
|
|
|
|
|
|
public class ActivityEml extends ActivityBase {
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
getSupportActionBar().setSubtitle("EML");
|
|
|
|
setContentView(R.layout.activity_eml);
|
|
|
|
|
2019-02-06 16:16:06 +00:00
|
|
|
final TextView tvTo = findViewById(R.id.tvTo);
|
|
|
|
final TextView tvFrom = findViewById(R.id.tvFrom);
|
|
|
|
final TextView tvReplyTo = findViewById(R.id.tvReplyTo);
|
|
|
|
final TextView tvCc = findViewById(R.id.tvCc);
|
|
|
|
final TextView tvBcc = findViewById(R.id.tvBcc);
|
|
|
|
final TextView tvSubject = findViewById(R.id.tvSubject);
|
|
|
|
final TextView tvParts = findViewById(R.id.tvParts);
|
2019-02-06 13:07:13 +00:00
|
|
|
final TextView tvBody = findViewById(R.id.tvBody);
|
2019-02-11 08:02:44 +00:00
|
|
|
final TextView tvHtml = findViewById(R.id.tvHtml);
|
2019-02-06 16:16:06 +00:00
|
|
|
final TextView tvEml = findViewById(R.id.tvEml);
|
2019-02-06 13:07:13 +00:00
|
|
|
final ContentLoadingProgressBar pbWait = findViewById(R.id.pbWait);
|
|
|
|
final Group grpEml = findViewById(R.id.grpEml);
|
2019-02-06 16:40:47 +00:00
|
|
|
|
2019-02-06 13:07:13 +00:00
|
|
|
grpEml.setVisibility(View.GONE);
|
|
|
|
|
2019-02-06 16:40:47 +00:00
|
|
|
Uri uri = getIntent().getData();
|
|
|
|
if (uri == null) {
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
return;
|
|
|
|
} else
|
|
|
|
pbWait.setVisibility(View.VISIBLE);
|
|
|
|
|
|
|
|
Log.i("EML uri=" + uri);
|
2019-02-06 13:07:13 +00:00
|
|
|
|
|
|
|
Bundle args = new Bundle();
|
2019-02-06 16:40:47 +00:00
|
|
|
args.putParcelable("uri", uri);
|
2019-02-06 13:07:13 +00:00
|
|
|
|
2019-02-06 16:16:06 +00:00
|
|
|
new SimpleTask<Result>() {
|
2019-02-07 12:44:14 +00:00
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Bundle args) {
|
|
|
|
pbWait.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:07:13 +00:00
|
|
|
@Override
|
2019-02-06 16:16:06 +00:00
|
|
|
protected Result onExecute(Context context, Bundle args) throws Throwable {
|
2019-02-06 13:07:13 +00:00
|
|
|
Uri uri = args.getParcelable("uri");
|
|
|
|
|
2019-02-07 12:44:14 +00:00
|
|
|
if ("file".equals(uri.getScheme()))
|
|
|
|
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
|
|
|
|
|
2019-02-06 16:16:06 +00:00
|
|
|
Result result = new Result();
|
|
|
|
|
2019-02-06 13:07:13 +00:00
|
|
|
InputStream is = null;
|
|
|
|
try {
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
|
|
|
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
|
|
|
|
is = new BufferedInputStream(descriptor.createInputStream());
|
|
|
|
|
|
|
|
Properties props = MessageHelper.getSessionProperties(Helper.AUTH_TYPE_PASSWORD, null, false);
|
|
|
|
Session isession = Session.getInstance(props, null);
|
|
|
|
MimeMessage mmessage = new MimeMessage(isession, is);
|
|
|
|
|
2019-02-06 16:16:06 +00:00
|
|
|
MessageHelper helper = new MessageHelper(mmessage);
|
|
|
|
|
|
|
|
result.from = MessageHelper.formatAddresses(helper.getFrom());
|
|
|
|
result.to = MessageHelper.formatAddresses(helper.getTo());
|
|
|
|
result.replyto = MessageHelper.formatAddresses(helper.getReply());
|
|
|
|
result.cc = MessageHelper.formatAddresses(helper.getCc());
|
|
|
|
result.bcc = MessageHelper.formatAddresses(helper.getBcc());
|
|
|
|
result.subject = helper.getSubject();
|
|
|
|
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2019-02-06 16:40:47 +00:00
|
|
|
for (MessageHelper.AttachmentPart apart : parts.getAttachmentParts()) {
|
2019-02-06 16:16:06 +00:00
|
|
|
if (sb.length() > 0)
|
|
|
|
sb.append("<br />");
|
|
|
|
sb.append(
|
|
|
|
apart.part.getContentType()).append(' ')
|
|
|
|
.append(apart.disposition).append(' ')
|
|
|
|
.append(apart.filename);
|
2019-02-06 13:07:13 +00:00
|
|
|
}
|
2019-02-10 12:01:21 +00:00
|
|
|
result.parts = HtmlHelper.fromHtml(sb.toString());
|
2019-02-06 16:16:06 +00:00
|
|
|
|
2019-02-11 08:02:44 +00:00
|
|
|
result.html = HtmlHelper.sanitize(parts.getHtml(context), true);
|
|
|
|
result.body = HtmlHelper.fromHtml(result.html);
|
2019-02-06 13:07:13 +00:00
|
|
|
|
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
mmessage.writeTo(bos);
|
2019-02-06 16:16:06 +00:00
|
|
|
result.eml = new String(bos.toByteArray());
|
2019-02-06 13:07:13 +00:00
|
|
|
|
2019-02-06 16:16:06 +00:00
|
|
|
return result;
|
2019-02-06 13:07:13 +00:00
|
|
|
} finally {
|
|
|
|
if (is != null)
|
|
|
|
is.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-02-06 16:16:06 +00:00
|
|
|
protected void onExecuted(Bundle args, Result result) {
|
|
|
|
tvFrom.setText(result.from);
|
|
|
|
tvTo.setText(result.to);
|
|
|
|
tvReplyTo.setText(result.replyto);
|
|
|
|
tvCc.setText(result.cc);
|
|
|
|
tvBcc.setText(result.bcc);
|
|
|
|
tvSubject.setText(result.subject);
|
|
|
|
tvParts.setText(result.parts);
|
|
|
|
tvBody.setText(result.body);
|
2019-02-11 08:02:44 +00:00
|
|
|
tvHtml.setText(result.html);
|
2019-02-06 16:16:06 +00:00
|
|
|
tvEml.setText(result.eml);
|
2019-02-06 13:07:13 +00:00
|
|
|
grpEml.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onException(Bundle args, Throwable ex) {
|
2019-02-07 12:44:14 +00:00
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
Snackbar.make(findViewById(android.R.id.content), ex.getMessage(), Snackbar.LENGTH_LONG).show();
|
|
|
|
else
|
|
|
|
Helper.unexpectedError(ActivityEml.this, ActivityEml.this, ex);
|
2019-02-06 13:07:13 +00:00
|
|
|
}
|
|
|
|
}.execute(this, args, "eml");
|
|
|
|
}
|
2019-02-06 16:16:06 +00:00
|
|
|
|
|
|
|
private class Result {
|
|
|
|
String from;
|
|
|
|
String to;
|
|
|
|
String replyto;
|
|
|
|
String cc;
|
|
|
|
String bcc;
|
|
|
|
String subject;
|
|
|
|
Spanned parts;
|
|
|
|
Spanned body;
|
2019-02-11 08:02:44 +00:00
|
|
|
String html;
|
2019-02-06 16:16:06 +00:00
|
|
|
String eml;
|
|
|
|
}
|
2019-02-06 13:07:13 +00:00
|
|
|
}
|