FairEmail/app/src/main/java/eu/faircode/email/ActivityEml.java

197 lines
7.8 KiB
Java
Raw Normal View History

2019-02-06 13:07:13 +00:00
package eu.faircode.email;
2019-05-04 20:49:22 +00:00
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2019 by Marcel Bokhorst (M66B)
*/
2019-05-14 10:12:13 +00:00
import android.Manifest;
2019-02-06 13:07:13 +00:00
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
2019-08-13 13:01:23 +00:00
import android.text.SpannableStringBuilder;
2019-02-06 16:16:06 +00:00
import android.text.Spanned;
2019-08-13 13:01:23 +00:00
import android.text.style.ForegroundColorSpan;
2019-02-06 13:07:13 +00:00
import android.view.View;
import android.widget.TextView;
import androidx.constraintlayout.widget.Group;
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.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
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);
2019-08-13 13:01:23 +00:00
final TextView tvHeaders = findViewById(R.id.tvHeaders);
2019-02-06 16:16:06 +00:00
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);
2019-03-17 09:18:27 +00:00
final Group grpReady = findViewById(R.id.grpReady);
2019-02-06 16:40:47 +00:00
2019-03-17 09:18:27 +00:00
grpReady.setVisibility(View.GONE);
2019-02-06 13:07:13 +00:00
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-05-14 10:12:13 +00:00
if ("file".equals(uri.getScheme()) &&
!Helper.hasPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
Log.w("EML uri=" + uri);
2019-02-07 12:44:14 +00:00
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
2019-05-14 10:12:13 +00:00
}
2019-02-07 12:44:14 +00:00
2019-02-06 16:16:06 +00:00
Result result = new Result();
2019-02-22 15:59:23 +00:00
ContentResolver resolver = context.getContentResolver();
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
try (InputStream is = descriptor.createInputStream()) {
2019-02-06 13:07:13 +00:00
2019-07-29 14:42:17 +00:00
Properties props = MessageHelper.getSessionProperties();
2019-02-06 13:07:13 +00:00
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();
2019-08-13 13:01:23 +00:00
String headers = helper.getHeaders();
int colorAccent = Helper.resolveColor(context, R.attr.colorAccent);
SpannableStringBuilder ssb = new SpannableStringBuilder(headers);
int index = 0;
for (String line : headers.split("\n")) {
if (line.length() > 0 && !Character.isWhitespace(line.charAt(0))) {
int colon = line.indexOf(':');
if (colon > 0)
ssb.setSpan(new ForegroundColorSpan(colorAccent), index, index + colon, 0);
}
index += line.length() + 1;
}
result.headers = ssb;
2019-02-06 16:16:06 +00:00
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 />");
2019-02-17 13:05:47 +00:00
sb.append(apart.part.getContentType());
if (apart.disposition != null)
sb.append(' ').append(apart.disposition);
if (apart.filename != null)
sb.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-03-10 10:42:59 +00:00
result.html = parts.getHtml(context);
2019-05-29 12:30:56 +00:00
if (result.html != null)
result.body = HtmlHelper.fromHtml(HtmlHelper.sanitize(context, result.html, false));
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
}
}
@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);
2019-08-13 13:01:23 +00:00
tvHeaders.setText(result.headers);
2019-02-06 16:16:06 +00:00
tvParts.setText(result.parts);
tvBody.setText(result.body);
2019-02-11 08:02:44 +00:00
tvHtml.setText(result.html);
2019-02-17 13:05:47 +00:00
tvEml.setText(result.eml.substring(0, Math.min(10 * 1024, result.eml.length()))); // prevent ANR
2019-03-17 09:18:27 +00:00
grpReady.setVisibility(View.VISIBLE);
2019-02-06 13:07:13 +00:00
}
@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(getSupportFragmentManager(), ex);
2019-02-06 13:07:13 +00:00
}
2019-03-17 09:18:27 +00:00
}.execute(this, args, "eml:decode");
2019-02-06 13:07:13 +00:00
}
2019-02-06 16:16:06 +00:00
private class Result {
String from;
String to;
String replyto;
String cc;
String bcc;
String subject;
2019-08-13 13:01:23 +00:00
Spanned headers;
2019-02-06 16:16:06 +00:00
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
}