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

451 lines
20 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/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
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;
2019-08-13 16:22:38 +00:00
import android.content.DialogInterface;
2019-11-10 12:45:13 +00:00
import android.content.Intent;
2019-02-06 13:07:13 +00:00
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Bundle;
2019-11-10 12:45:13 +00:00
import android.os.ParcelFileDescriptor;
2019-02-06 16:16:06 +00:00
import android.text.Spanned;
2019-11-10 12:45:13 +00:00
import android.text.TextUtils;
2019-12-13 07:36:39 +00:00
import android.text.method.LinkMovementMethod;
2019-08-13 13:38:44 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2019-02-06 13:07:13 +00:00
import android.view.View;
2019-08-13 16:22:38 +00:00
import android.widget.ArrayAdapter;
2019-02-06 13:07:13 +00:00
import android.widget.TextView;
2019-08-13 13:38:44 +00:00
import android.widget.Toast;
2019-02-06 13:07:13 +00:00
2019-08-13 13:38:44 +00:00
import androidx.annotation.NonNull;
2019-11-10 12:45:13 +00:00
import androidx.annotation.Nullable;
2019-08-13 16:22:38 +00:00
import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.Group;
2019-11-11 08:30:33 +00:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2019-02-07 12:44:14 +00:00
import com.google.android.material.snackbar.Snackbar;
2019-08-13 13:38:44 +00:00
import com.sun.mail.imap.IMAPFolder;
2019-02-07 12:44:14 +00:00
2019-11-19 20:53:12 +00:00
import org.jsoup.nodes.Document;
2020-01-24 10:00:09 +00:00
import java.io.File;
2019-11-10 12:45:13 +00:00
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
2019-02-06 13:07:13 +00:00
import java.io.InputStream;
2019-11-10 12:45:13 +00:00
import java.io.OutputStream;
2019-12-11 07:11:57 +00:00
import java.text.DateFormat;
2019-08-13 16:22:38 +00:00
import java.util.List;
2019-02-06 13:07:13 +00:00
import java.util.Properties;
2019-08-13 13:38:44 +00:00
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
2019-02-06 13:07:13 +00:00
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
2019-08-29 19:57:04 +00:00
public class ActivityEML extends ActivityBase {
2019-08-13 13:38:44 +00:00
private Uri uri;
2019-11-10 12:45:13 +00:00
private MessageHelper.AttachmentPart apart;
private static final int REQUEST_ATTACHMENT = 1;
2019-08-13 13:38:44 +00:00
2019-02-06 13:07:13 +00:00
@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 tvFrom = findViewById(R.id.tvFrom);
2019-12-11 07:11:57 +00:00
final TextView tvTo = findViewById(R.id.tvTo);
final TextView tvReplyTo = findViewById(R.id.tvReplyTo);
final TextView tvCc = findViewById(R.id.tvCc);
final TextView tvSent = findViewById(R.id.tvSent);
final TextView tvReceived = findViewById(R.id.tvReceived);
2019-02-06 16:16:06 +00:00
final TextView tvSubject = findViewById(R.id.tvSubject);
2019-11-17 17:35:18 +00:00
final View vSeparatorAttachments = findViewById(R.id.vSeparatorAttachments);
2019-11-11 08:30:33 +00:00
final RecyclerView rvAttachment = findViewById(R.id.rvAttachment);
2019-02-06 13:07:13 +00:00
final TextView tvBody = findViewById(R.id.tvBody);
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-11-11 08:30:33 +00:00
rvAttachment.setHasFixedSize(false);
LinearLayoutManager llm = new LinearLayoutManager(this);
rvAttachment.setLayoutManager(llm);
2019-11-10 12:45:13 +00:00
2019-12-13 07:36:39 +00:00
tvBody.setMovementMethod(LinkMovementMethod.getInstance());
2019-11-17 17:35:18 +00:00
vSeparatorAttachments.setVisibility(View.GONE);
2019-03-17 09:18:27 +00:00
grpReady.setVisibility(View.GONE);
2019-02-06 13:07:13 +00:00
2019-08-13 13:38:44 +00:00
uri = getIntent().getData();
2019-02-06 16:40:47 +00:00
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-11-03 19:07:26 +00:00
if (!"content".equals(uri.getScheme()) &&
2019-05-14 10:12:13 +00:00
!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);
2019-08-13 13:38:44 +00:00
MimeMessage imessage = new MimeMessage(isession, is);
2019-02-06 13:07:13 +00:00
2019-08-13 13:38:44 +00:00
MessageHelper helper = new MessageHelper(imessage);
2019-02-06 16:16:06 +00:00
result.from = MessageHelper.formatAddresses(helper.getFrom());
result.to = MessageHelper.formatAddresses(helper.getTo());
2019-12-11 07:11:57 +00:00
result.replyTo = MessageHelper.formatAddresses(helper.getReply());
result.cc = MessageHelper.formatAddresses(helper.getCc());
result.sent = helper.getSent();
result.received = helper.getReceived();
2019-02-06 16:16:06 +00:00
result.subject = helper.getSubject();
2019-11-30 10:09:30 +00:00
result.parts = helper.getMessageParts(context);
2019-08-13 13:01:23 +00:00
2019-11-10 12:45:13 +00:00
String html = result.parts.getHtml(context);
2019-11-19 20:53:12 +00:00
if (html != null) {
2019-12-13 07:36:39 +00:00
Document document = HtmlHelper.sanitize(context, html, false, true);
2019-11-19 20:53:12 +00:00
result.body = HtmlHelper.fromHtml(document.html());
}
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) {
2019-12-11 07:11:57 +00:00
DateFormat DTF = Helper.getDateTimeInstance(ActivityEML.this);
2019-02-06 16:16:06 +00:00
tvFrom.setText(result.from);
tvTo.setText(result.to);
2019-12-11 07:11:57 +00:00
tvReplyTo.setText(result.replyTo);
tvCc.setText(result.cc);
tvSent.setText(result.sent == null ? null : DTF.format(result.sent));
tvReceived.setText(result.received == null ? null : DTF.format(result.received));
2019-02-06 16:16:06 +00:00
tvSubject.setText(result.subject);
2019-11-10 12:45:13 +00:00
2019-11-17 17:35:18 +00:00
vSeparatorAttachments.setVisibility(result.parts.getAttachmentParts().size() > 0 ? View.VISIBLE : View.GONE);
2019-11-10 12:45:13 +00:00
2019-11-11 08:30:33 +00:00
AdapterAttachmentEML adapter = new AdapterAttachmentEML(
ActivityEML.this,
result.parts.getAttachmentParts(),
new AdapterAttachmentEML.IEML() {
@Override
2020-01-24 10:00:09 +00:00
public void onShare(MessageHelper.AttachmentPart apart) {
new SimpleTask<File>() {
@Override
protected File onExecute(Context context, Bundle args) throws Throwable {
apart.attachment.id = 0L;
File file = apart.attachment.getFile(context);
Log.i("Writing to " + file);
try (InputStream is = apart.part.getInputStream()) {
try (OutputStream os = new FileOutputStream(file)) {
byte[] buffer = new byte[Helper.BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
}
}
return file;
}
@Override
protected void onExecuted(Bundle args, File file) {
Helper.share(ActivityEML.this, file,
apart.attachment.getMimeType(),
apart.attachment.name);
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(ActivityEML.this, new Bundle(), "eml:share");
}
@Override
public void onSave(MessageHelper.AttachmentPart apart) {
2019-11-11 08:30:33 +00:00
ActivityEML.this.apart = apart;
Intent create = new Intent(Intent.ACTION_CREATE_DOCUMENT);
create.addCategory(Intent.CATEGORY_OPENABLE);
create.setType(apart.attachment.getMimeType());
if (!TextUtils.isEmpty(apart.attachment.name))
create.putExtra(Intent.EXTRA_TITLE, apart.attachment.name);
2019-12-01 17:19:17 +00:00
Helper.openAdvanced(create);
2019-11-11 08:30:33 +00:00
if (create.resolveActivity(getPackageManager()) == null)
ToastEx.makeText(ActivityEML.this, R.string.title_no_saf, Toast.LENGTH_LONG).show();
else
startActivityForResult(Helper.getChooser(ActivityEML.this, create), REQUEST_ATTACHMENT);
}
});
rvAttachment.setAdapter(adapter);
2019-11-10 12:45:13 +00:00
2019-02-06 16:16:06 +00:00
tvBody.setText(result.body);
2019-03-17 09:18:27 +00:00
grpReady.setVisibility(View.VISIBLE);
2019-02-06 13:07:13 +00:00
}
@Override
2019-08-13 16:22:38 +00:00
protected void onException(Bundle args, @NonNull 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
2019-12-06 07:50:46 +00:00
Log.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
2019-11-10 12:45:13 +00:00
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
switch (requestCode) {
case REQUEST_ATTACHMENT:
if (resultCode == RESULT_OK && data != null)
onSaveAttachment(data);
break;
}
} catch (Throwable ex) {
Log.e(ex);
}
}
private void onSaveAttachment(Intent data) {
Bundle args = new Bundle();
args.putParcelable("uri", data.getData());
new SimpleTask<Void>() {
@Override
protected Void onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
if (!"content".equals(uri.getScheme())) {
Log.w("Save attachment uri=" + uri);
throw new IllegalArgumentException(context.getString(R.string.title_no_stream));
}
ParcelFileDescriptor pfd = null;
OutputStream os = null;
InputStream is;
try {
pfd = getContentResolver().openFileDescriptor(uri, "w");
os = new FileOutputStream(pfd.getFileDescriptor());
is = apart.part.getInputStream();
byte[] buffer = new byte[Helper.BUFFER_SIZE];
int read;
while ((read = is.read(buffer)) != -1)
os.write(buffer, 0, read);
} finally {
try {
if (pfd != null)
pfd.close();
} catch (Throwable ex) {
Log.w(ex);
}
try {
if (os != null)
os.close();
} catch (Throwable ex) {
Log.w(ex);
}
}
return null;
}
@Override
protected void onExecuted(Bundle args, Void data) {
ToastEx.makeText(ActivityEML.this, R.string.title_attachment_saved, Toast.LENGTH_LONG).show();
}
@Override
protected void onException(Bundle args, Throwable ex) {
if (ex instanceof IllegalArgumentException || ex instanceof FileNotFoundException)
ToastEx.makeText(ActivityEML.this, ex.getMessage(), Toast.LENGTH_LONG).show();
else
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getSupportFragmentManager(), ex);
2019-11-10 12:45:13 +00:00
}
}.execute(this, args, "eml:attachment");
}
2019-08-13 13:38:44 +00:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_eml, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_save:
onMenuSave();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void onMenuSave() {
2019-08-13 16:22:38 +00:00
new SimpleTask<List<EntityAccount>>() {
2019-08-13 13:38:44 +00:00
@Override
2019-08-13 16:22:38 +00:00
protected List<EntityAccount> onExecute(Context context, Bundle args) {
2019-08-13 13:38:44 +00:00
DB db = DB.getInstance(context);
2019-08-13 16:22:38 +00:00
return db.account().getSynchronizingAccounts();
2019-08-13 13:38:44 +00:00
}
@Override
2019-08-13 16:22:38 +00:00
protected void onExecuted(Bundle args, List<EntityAccount> accounts) {
ArrayAdapter<EntityAccount> adapter =
2019-08-29 19:57:04 +00:00
new ArrayAdapter<>(ActivityEML.this, R.layout.spinner_item1, android.R.id.text1);
2019-08-13 16:22:38 +00:00
adapter.addAll(accounts);
2019-08-29 19:57:04 +00:00
new AlertDialog.Builder(ActivityEML.this)
2019-10-29 12:21:34 +00:00
.setTitle(R.string.title_save_eml)
2019-08-13 16:22:38 +00:00
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EntityAccount account = adapter.getItem(which);
Bundle args = new Bundle();
args.putParcelable("uri", uri);
args.putLong("account", account.id);
new SimpleTask<String>() {
@Override
protected void onPreExecute(Bundle args) {
2019-08-29 19:57:04 +00:00
ToastEx.makeText(ActivityEML.this, R.string.title_executing, Toast.LENGTH_LONG).show();
2019-08-13 16:22:38 +00:00
}
@Override
protected String onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
long aid = args.getLong("account");
DB db = DB.getInstance(context);
EntityAccount account = db.account().getAccount(aid);
if (account == null)
return null;
EntityFolder inbox = db.folder().getFolderByType(account.id, EntityFolder.INBOX);
if (inbox == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_folder));
ContentResolver resolver = context.getContentResolver();
AssetFileDescriptor descriptor = resolver.openTypedAssetFileDescriptor(uri, "*/*", null);
try (InputStream is = descriptor.createInputStream()) {
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession, is);
2019-10-19 08:05:43 +00:00
try (MailService iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, false, true)) {
2019-08-13 16:22:38 +00:00
iservice.setPartialFetch(account.partial_fetch);
iservice.setIgnoreBodyStructureSize(account.ignore_size);
2019-08-13 16:22:38 +00:00
iservice.connect(account);
IMAPFolder ifolder = (IMAPFolder) iservice.getStore().getFolder(inbox.name);
ifolder.open(Folder.READ_WRITE);
if (ifolder.getPermanentFlags().contains(Flags.Flag.DRAFT))
imessage.setFlag(Flags.Flag.DRAFT, false);
ifolder.appendMessages(new Message[]{imessage});
}
2019-12-07 19:32:58 +00:00
EntityOperation.sync(context, inbox.id, true);
2019-12-09 18:44:27 +00:00
ServiceSynchronize.eval(context, "EML");
2019-08-13 16:22:38 +00:00
}
return account.name + "/" + inbox.name;
}
@Override
protected void onExecuted(Bundle args, String name) {
2019-08-29 19:57:04 +00:00
ToastEx.makeText(ActivityEML.this, name, Toast.LENGTH_LONG).show();
2019-08-13 16:22:38 +00:00
}
@Override
protected void onException(Bundle args, @NonNull Throwable ex) {
if (ex instanceof IllegalArgumentException)
Snackbar.make(findViewById(android.R.id.content), ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getSupportFragmentManager(), ex);
2019-08-13 16:22:38 +00:00
}
2019-08-29 19:57:04 +00:00
}.execute(ActivityEML.this, args, "eml:store");
2019-08-13 16:22:38 +00:00
}
})
2019-09-12 11:23:32 +00:00
.setNegativeButton(android.R.string.cancel, null)
2019-08-13 16:22:38 +00:00
.show();
2019-08-13 13:38:44 +00:00
}
@Override
2019-08-13 16:22:38 +00:00
protected void onException(Bundle args, @NonNull Throwable ex) {
2019-12-06 07:50:46 +00:00
Log.unexpectedError(getSupportFragmentManager(), ex);
2019-08-13 13:38:44 +00:00
}
2019-08-13 16:22:38 +00:00
}.execute(this, new Bundle(), "messages:accounts");
2019-08-13 13:38:44 +00:00
}
2019-02-06 16:16:06 +00:00
private class Result {
String from;
String to;
2019-12-11 07:11:57 +00:00
String replyTo;
String cc;
Long sent;
Long received;
2019-02-06 16:16:06 +00:00
String subject;
2019-11-10 12:45:13 +00:00
MessageHelper.MessageParts parts;
2019-02-06 16:16:06 +00:00
Spanned body;
}
2019-02-06 13:07:13 +00:00
}