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

609 lines
26 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/>.
2021-01-01 07:56:36 +00:00
Copyright 2018-2021 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;
2021-06-17 07:28:11 +00:00
import android.content.SharedPreferences;
2019-02-06 13:07:13 +00:00
import android.net.Uri;
import android.os.Bundle;
2021-04-04 11:28:55 +00:00
import android.text.Spannable;
2021-06-17 07:04:06 +00:00
import android.text.SpannableStringBuilder;
2019-02-06 16:16:06 +00:00
import android.text.Spanned;
2019-11-10 12:45:13 +00:00
import android.text.TextUtils;
2021-04-04 11:28:55 +00:00
import android.text.method.ArrowKeyMovementMethod;
2021-06-17 07:04:06 +00:00
import android.text.style.ForegroundColorSpan;
2021-04-04 11:28:55 +00:00
import android.text.style.URLSpan;
2021-06-17 07:28:11 +00:00
import android.view.LayoutInflater;
2019-08-13 13:38:44 +00:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
2021-04-04 11:28:55 +00:00
import android.view.MotionEvent;
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;
2021-06-17 07:28:11 +00:00
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
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;
2021-06-17 09:59:57 +00:00
import java.util.ArrayList;
2021-06-17 07:04:06 +00:00
import java.util.Enumeration;
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;
2021-06-17 07:04:06 +00:00
import javax.mail.Header;
2019-08-13 13:38:44 +00:00
import javax.mail.Message;
2021-06-17 07:04:06 +00:00
import javax.mail.Multipart;
import javax.mail.Part;
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 {
2020-01-24 10:13:56 +00:00
private TextView tvFrom;
private TextView tvTo;
private TextView tvReplyTo;
private TextView tvCc;
2020-04-28 12:48:50 +00:00
private TextView tvBcc;
2020-01-24 10:13:56 +00:00
private TextView tvSent;
private TextView tvReceived;
private TextView tvSubject;
private View vSeparatorAttachments;
private RecyclerView rvAttachment;
private TextView tvBody;
2021-06-17 07:04:06 +00:00
private TextView tvStructure;
2020-01-24 10:13:56 +00:00
private ContentLoadingProgressBar pbWait;
private Group grpReady;
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");
2021-06-17 07:28:11 +00:00
View view = LayoutInflater.from(this).inflate(R.layout.activity_eml, null);
setContentView(view);
2019-02-06 13:07:13 +00:00
2020-01-24 10:13:56 +00:00
tvFrom = findViewById(R.id.tvFrom);
tvTo = findViewById(R.id.tvTo);
tvReplyTo = findViewById(R.id.tvReplyTo);
tvCc = findViewById(R.id.tvCc);
2020-04-28 12:48:50 +00:00
tvBcc = findViewById(R.id.tvBcc);
2020-01-24 10:13:56 +00:00
tvSent = findViewById(R.id.tvSent);
tvReceived = findViewById(R.id.tvReceived);
tvSubject = findViewById(R.id.tvSubject);
vSeparatorAttachments = findViewById(R.id.vSeparatorAttachments);
rvAttachment = findViewById(R.id.rvAttachment);
tvBody = findViewById(R.id.tvBody);
2021-06-17 07:04:06 +00:00
tvStructure = findViewById(R.id.tvStructure);
2020-01-24 10:13:56 +00:00
pbWait = findViewById(R.id.pbWait);
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
2021-04-04 11:28:55 +00:00
tvBody.setMovementMethod(new ArrowKeyMovementMethod() {
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
2021-04-04 12:53:25 +00:00
int off = Helper.getOffset(widget, buffer, event);
2021-04-04 11:28:55 +00:00
URLSpan[] link = buffer.getSpans(off, off, URLSpan.class);
if (link.length > 0) {
String url = link[0].getURL();
Uri uri = Uri.parse(url);
if (uri.getScheme() == null)
uri = Uri.parse("https://" + url);
int start = buffer.getSpanStart(link[0]);
int end = buffer.getSpanEnd(link[0]);
String title = (start < 0 || end < 0 || end <= start
? null : buffer.subSequence(start, end).toString());
if (url.equals(title))
title = null;
Bundle args = new Bundle();
args.putParcelable("uri", uri);
args.putString("title", title);
FragmentDialogOpenLink fragment = new FragmentDialogOpenLink();
fragment.setArguments(args);
fragment.show(getSupportFragmentManager(), "open:link");
return true;
}
}
return super.onTouchEvent(widget, buffer, event);
}
});
2019-12-13 07:36:39 +00:00
2021-06-17 07:28:11 +00:00
// Initialize
if (!Helper.isDarkTheme(this)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean beige = prefs.getBoolean("beige", true);
view.setBackgroundColor(ContextCompat.getColor(this, beige
? R.color.lightColorBackground_cards_beige
: R.color.lightColorBackground_cards));
}
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
2020-01-24 10:13:56 +00:00
load();
}
2019-02-06 16:40:47 +00:00
2020-01-24 10:13:56 +00:00
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
load();
}
private void load() {
uri = getIntent().getData();
2019-02-06 16:40:47 +00:00
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>() {
2020-01-24 10:13:56 +00:00
@Override
protected void onPreExecute(Bundle args) {
pbWait.setVisibility(View.VISIBLE);
}
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");
2020-01-24 10:13:56 +00:00
if (uri == null)
throw new FileNotFoundException();
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();
2020-10-14 14:50:15 +00:00
try (InputStream is = resolver.openInputStream(uri)) {
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
2020-05-23 14:05:29 +00:00
MessageHelper helper = new MessageHelper(imessage, context);
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());
2020-04-28 12:48:50 +00:00
result.bcc = MessageHelper.formatAddresses(helper.getBcc());
2019-12-11 07:11:57 +00:00
result.sent = helper.getSent();
result.received = helper.getReceived();
2019-02-06 16:16:06 +00:00
result.subject = helper.getSubject();
2020-05-23 14:05:29 +00:00
result.parts = helper.getMessageParts();
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) {
2020-03-25 19:25:06 +00:00
Document parsed = JsoupEx.parse(html);
Document document = HtmlHelper.sanitizeView(context, parsed, false);
2020-11-08 20:09:31 +00:00
result.body = HtmlHelper.fromDocument(context, document, null, null);
2019-11-19 20:53:12 +00:00
}
2019-02-06 13:07:13 +00:00
2021-06-17 07:04:06 +00:00
int textColorLink = Helper.resolveColor(context, android.R.attr.textColorLink);
SpannableStringBuilder ssb = new SpannableStringBuilder();
2021-06-17 13:47:48 +00:00
getStructure(imessage, ssb, 0, textColorLink);
2021-06-17 07:04:06 +00:00
result.structure = ssb;
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);
2020-04-28 12:48:50 +00:00
tvBcc.setText(result.bcc);
2019-12-11 07:11:57 +00:00
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);
2020-06-14 16:34:13 +00:00
if (create.resolveActivity(getPackageManager()) == null) // system whitelisted
2019-11-11 08:30:33 +00:00
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);
2021-06-17 07:04:06 +00:00
tvStructure.setText(result.structure);
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)
.setGestureInsetBottomIgnored(true).show();
2019-02-07 12:44:14 +00:00
else
2020-08-22 05:44:39 +00:00
Log.unexpectedError(getSupportFragmentManager(), ex, false);
2019-02-06 13:07:13 +00:00
}
2021-06-17 07:04:06 +00:00
2021-06-17 13:47:48 +00:00
private void getStructure(Part part, SpannableStringBuilder ssb, int level, int textColorLink) {
2021-06-17 07:04:06 +00:00
try {
2021-06-17 09:59:57 +00:00
Enumeration<Header> headers;
if (level == 0) {
List<Header> h = new ArrayList<>();
String[] cte = part.getHeader("Content-Transfer-Encoding");
if (cte != null)
for (String header : cte)
h.add(new Header("Content-Transfer-Encoding", header));
String[] ct = part.getHeader("Content-Type");
if (ct == null)
h.add(new Header("Content-Type", "text/plain"));
else
for (String header : ct)
h.add(new Header("Content-Type", header));
headers = new Enumeration<Header>() {
private int index = -1;
@Override
public boolean hasMoreElements() {
return (index + 1 < h.size());
}
@Override
public Header nextElement() {
return h.get(++index);
}
};
2021-06-17 07:04:06 +00:00
} else
2021-06-17 09:59:57 +00:00
headers = part.getAllHeaders();
while (headers.hasMoreElements()) {
Header header = headers.nextElement();
for (int i = 0; i < level; i++)
ssb.append(" ");
int start = ssb.length();
ssb.append(header.getName());
ssb.setSpan(new ForegroundColorSpan(textColorLink), start, ssb.length(), 0);
ssb.append(": ").append(header.getValue()).append('\n');
}
for (int i = 0; i < level; i++)
ssb.append(" ");
int size = part.getSize();
ssb.append("Size: ")
.append(size > 0 ? Helper.humanReadableByteCount(size) : "?")
.append('\n');
ssb.append('\n');
2021-06-17 07:04:06 +00:00
if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
for (int i = 0; i < multipart.getCount(); i++)
try {
2021-06-17 13:47:48 +00:00
getStructure(multipart.getBodyPart(i), ssb, level + 1, textColorLink);
2021-06-17 07:04:06 +00:00
} catch (Throwable ex) {
2021-06-17 09:59:57 +00:00
Log.w(ex);
2021-06-17 07:04:06 +00:00
ssb.append(ex.toString()).append('\n');
}
}
} catch (Throwable ex) {
2021-06-17 09:59:57 +00:00
Log.w(ex);
2021-06-17 07:04:06 +00:00
ssb.append(ex.toString()).append('\n');
}
}
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));
}
OutputStream os = null;
InputStream is;
try {
2020-09-27 15:24:32 +00:00
os = getContentResolver().openOutputStream(uri);
2019-11-10 12:45:13 +00:00
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 (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) {
2021-02-05 10:15:02 +00:00
if (item.getItemId() == R.id.menu_save) {
onMenuSave();
return true;
2019-08-13 13:38:44 +00:00
}
2021-02-05 10:15:02 +00:00
return super.onOptionsItemSelected(item);
2019-08-13 13:38:44 +00:00
}
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);
2020-04-13 15:42:48 +00:00
for (EntityAccount account : accounts)
if (account.protocol == EntityAccount.TYPE_IMAP)
adapter.add(account);
2019-08-13 16:22:38 +00:00
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();
2020-10-14 14:50:15 +00:00
try (InputStream is = resolver.openInputStream(uri)) {
2019-08-13 16:22:38 +00:00
Properties props = MessageHelper.getSessionProperties();
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession, is);
2020-02-06 12:06:10 +00:00
try (EmailService iservice = new EmailService(
2020-08-23 07:28:10 +00:00
context, account.getProtocol(), account.realm, account.encryption, account.insecure, 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});
}
}
2021-03-24 17:29:29 +00:00
EntityOperation.sync(context, inbox.id, true);
2021-03-27 15:03:58 +00:00
ServiceSynchronize.eval(context, "EML");
2021-03-24 17:29:29 +00:00
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)
.setGestureInsetBottomIgnored(true).show();
2019-08-13 16:22:38 +00:00
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;
2020-04-28 12:48:50 +00:00
String bcc;
2019-12-11 07:11:57 +00:00
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;
2021-06-17 07:04:06 +00:00
Spanned structure;
2019-02-06 16:16:06 +00:00
}
2019-02-06 13:07:13 +00:00
}