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

648 lines
27 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/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2019-05-04 20:49:22 +00:00
*/
2019-02-06 13:07:13 +00:00
import android.content.ContentResolver;
import android.content.Context;
2019-11-10 12:45:13 +00:00
import android.content.Intent;
import android.content.SharedPreferences;
2022-09-28 14:24:17 +00:00
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
2022-06-07 16:33:47 +00:00
import android.graphics.drawable.Drawable;
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;
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;
2021-06-17 14:31:18 +00:00
import android.widget.ImageButton;
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;
2022-01-29 11:22:41 +00:00
import androidx.cardview.widget.CardView;
import androidx.constraintlayout.widget.Group;
2022-08-04 13:24:27 +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;
2022-06-07 16:33:47 +00:00
import org.jsoup.nodes.Element;
2019-11-19 20:53:12 +00:00
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-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 {
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;
2022-02-06 21:43:36 +00:00
private TextView tvReceived;
2020-01-24 10:13:56 +00:00
private TextView tvSubject;
private View vSeparatorAttachments;
private RecyclerView rvAttachment;
private TextView tvBody;
2021-06-17 07:04:06 +00:00
private TextView tvStructure;
2021-06-17 14:31:18 +00:00
private ImageButton ibEml;
2022-01-29 11:22:41 +00:00
private CardView cardHeaders;
private TextView tvHeaders;
2020-01-24 10:13:56 +00:00
private ContentLoadingProgressBar pbWait;
private Group grpReady;
2022-02-25 07:18:46 +00:00
private boolean junk;
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;
2022-03-25 10:05:20 +00:00
private static final int REQUEST_ACCOUNT = 2;
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);
2022-02-25 07:18:46 +00:00
if (savedInstanceState != null)
junk = savedInstanceState.getBoolean("fair:junk");
2022-02-13 13:59:37 +00:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2019-02-06 13:07:13 +00:00
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);
2022-02-06 21:43:36 +00:00
tvReceived = findViewById(R.id.tvReceived);
2020-01-24 10:13:56 +00:00
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);
2021-06-17 14:31:18 +00:00
ibEml = findViewById(R.id.ibEml);
2022-01-29 11:22:41 +00:00
cardHeaders = findViewById(R.id.cardHeaders);
tvHeaders = findViewById(R.id.tvHeaders);
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);
2022-11-11 17:10:09 +00:00
args.putBoolean("always_confirm", true);
2021-04-04 11:28:55 +00:00
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 14:31:18 +00:00
ibEml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
args.putParcelable("uri", getIntent().getData());
new SimpleTask<File>() {
@Override
protected File onExecute(Context context, Bundle args) throws Throwable {
2021-08-07 06:11:34 +00:00
Uri uri = args.getParcelable("uri");
if (uri == null)
throw new FileNotFoundException();
2023-12-13 06:21:15 +00:00
File dir = Helper.ensureExists(context, "shared");
2021-06-17 14:31:18 +00:00
File file = new File(dir, "email.eml");
Helper.copy(context, uri, file);
return file;
}
@Override
protected void onExecuted(Bundle args, File file) {
Helper.share(ActivityEML.this, file, "text/plain", file.getName());
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(getSupportFragmentManager(), ex);
}
}.execute(ActivityEML.this, args, "eml:share");
}
});
2021-06-17 07:28:11 +00:00
// Initialize
2021-06-27 06:13:56 +00:00
FragmentDialogTheme.setBackground(this, view, false);
2019-11-17 17:35:18 +00:00
vSeparatorAttachments.setVisibility(View.GONE);
2019-03-17 09:18:27 +00:00
grpReady.setVisibility(View.GONE);
2022-01-29 11:22:41 +00:00
cardHeaders.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();
}
2022-02-25 07:18:46 +00:00
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("fair:junk", junk);
super.onSaveInstanceState(outState);
}
2020-01-24 10:13:56 +00:00
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");
2022-03-18 16:22:25 +00:00
NoStreamException.check(uri, context);
2019-02-07 12:44:14 +00:00
2019-02-06 16:16:06 +00:00
Result result = new Result();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean download_plain = prefs.getBoolean("download_plain", false);
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)) {
2022-03-27 11:43:45 +00:00
if (is == null)
throw new FileNotFoundException(uri.toString());
2019-02-06 13:07:13 +00:00
2022-08-13 12:03:41 +00:00
Properties props = MessageHelper.getSessionProperties(true);
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();
2022-02-07 06:53:37 +00:00
result.received = helper.getReceivedHeader();
2019-02-06 16:16:06 +00:00
result.subject = helper.getSubject();
2021-09-05 17:37:45 +00:00
result.parts = helper.getMessageParts(false);
2019-08-13 13:01:23 +00:00
String html = result.parts.getHtml(context, download_plain);
2019-11-19 20:53:12 +00:00
if (html != null) {
2020-03-25 19:25:06 +00:00
Document parsed = JsoupEx.parse(html);
2021-11-27 14:28:38 +00:00
HtmlHelper.autoLink(parsed);
2020-03-25 19:25:06 +00:00
Document document = HtmlHelper.sanitizeView(context, parsed, false);
2022-06-07 16:33:47 +00:00
result.body = HtmlHelper.fromDocument(context, document, new HtmlHelper.ImageGetterEx() {
@Override
public Drawable getDrawable(Element img) {
2022-09-28 14:24:17 +00:00
Drawable d = null;
if (TextUtils.isEmpty(img.attr("x-tracking"))) {
String src = img.attr("src");
if (src.startsWith("cid:")) {
String cid = "<" + src.substring(4) + ">";
Integer w = Helper.parseInt(img.attr("width"));
Integer h = Helper.parseInt(img.attr("height"));
Resources res = context.getResources();
int scaleToPixels = res.getDisplayMetrics().widthPixels;
for (MessageHelper.AttachmentPart apart : result.parts.getAttachmentParts())
if (cid.equals(apart.attachment.cid)) {
try {
Bitmap bm = ImageHelper.getScaledBitmap(apart.part.getInputStream(), src, apart.attachment.type, scaleToPixels);
d = new BitmapDrawable(res, bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
ImageHelper.fitDrawable(d, w == null ? 0 : w, h == null ? 0 : h, 1.0f, tvBody);
} catch (Throwable ex) {
Log.e(ex);
}
break;
}
if (d == null)
2022-10-08 05:34:32 +00:00
d = ContextCompat.getDrawable(context, R.drawable.twotone_broken_image_24);
} else
d = ContextCompat.getDrawable(context, R.drawable.twotone_image_24);
2022-09-28 14:24:17 +00:00
} else {
2022-08-04 13:24:27 +00:00
d = ContextCompat.getDrawable(context, R.drawable.twotone_my_location_24);
2022-06-07 16:33:47 +00:00
d.setTint(Helper.resolveColor(context, R.attr.colorWarning));
}
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
}, 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);
2021-09-09 10:52:10 +00:00
SpannableStringBuilder ssb = new SpannableStringBuilderEx();
2022-09-17 09:59:35 +00:00
MessageHelper.getStructure(imessage, ssb, 0, textColorLink);
2021-06-17 07:04:06 +00:00
result.structure = ssb;
2023-12-16 14:42:43 +00:00
result.headers = HtmlHelper.highlightHeaders(context,
helper.getFrom(), helper.getTo(), helper.getReceivedHeader(), helper.getHeaders(), false);
2022-01-29 11:22:41 +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);
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));
2022-02-06 21:43:36 +00:00
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.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
2019-11-11 08:30:33 +00:00
create.setType(apart.attachment.getMimeType());
if (!TextUtils.isEmpty(apart.attachment.name))
create.putExtra(Intent.EXTRA_TITLE, apart.attachment.name);
2023-01-27 08:56:32 +00:00
Helper.openAdvanced(ActivityEML.this, create);
2020-06-14 16:34:13 +00:00
if (create.resolveActivity(getPackageManager()) == null) // system whitelisted
2023-12-06 10:06:33 +00:00
Log.unexpectedError(getSupportFragmentManager(),
new IllegalArgumentException(getString(R.string.title_no_saf)), 25);
2019-11-11 08:30:33 +00:00
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);
2022-01-29 11:22:41 +00:00
tvHeaders.setText(result.headers);
2019-03-17 09:18:27 +00:00
grpReady.setVisibility(View.VISIBLE);
2022-01-29 12:52:28 +00:00
cardHeaders.setVisibility(BuildConfig.DEBUG ? View.VISIBLE : View.GONE);
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) {
2022-03-18 16:22:25 +00:00
if (ex instanceof NoStreamException)
((NoStreamException) ex).report(ActivityEML.this);
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
}
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;
2022-03-25 10:05:20 +00:00
case REQUEST_ACCOUNT:
if (resultCode == RESULT_OK && data != null)
onSave(data.getBundleExtra("args"));
break;
2019-11-10 12:45:13 +00:00
}
} 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");
2021-08-07 06:11:34 +00:00
if (uri == null)
throw new FileNotFoundException();
2019-11-10 12:45:13 +00:00
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();
2022-03-27 11:43:45 +00:00
if (os == null)
throw new FileNotFoundException(uri.toString());
2019-11-10 12:45:13 +00:00
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) {
2023-12-06 16:06:46 +00:00
boolean report = !(ex instanceof IllegalArgumentException || ex instanceof FileNotFoundException);
Log.unexpectedError(getSupportFragmentManager(), ex, report);
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;
}
2022-02-25 07:18:46 +00:00
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
2023-03-26 05:51:17 +00:00
menu.findItem(R.id.menu_junk)
.setVisible(BuildConfig.DEBUG)
.setChecked(junk);
2022-02-25 07:18:46 +00:00
menu.findItem(R.id.menu_save).setIcon(junk
? R.drawable.twotone_report_24
: R.drawable.twotone_move_to_inbox_24);
return super.onPrepareOptionsMenu(menu);
}
2019-08-13 13:38:44 +00:00
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
2022-02-25 07:18:46 +00:00
int itemId = item.getItemId();
2023-11-02 20:08:43 +00:00
if (itemId == android.R.id.home) {
finish();
return true;
} else if (itemId == R.id.menu_save) {
2021-02-05 10:15:02 +00:00
onMenuSave();
return true;
2022-02-25 07:18:46 +00:00
} else if (itemId == R.id.menu_junk) {
junk = !junk;
item.setChecked(junk);
invalidateOptionsMenu();
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() {
2022-03-25 10:05:20 +00:00
Bundle args = new Bundle();
args.putInt("type", EntityAccount.TYPE_IMAP);
FragmentDialogSelectAccount fragment = new FragmentDialogSelectAccount();
fragment.setArguments(args);
fragment.setTargetActivity(this, REQUEST_ACCOUNT);
fragment.show(getSupportFragmentManager(), "eml:account");
}
private void onSave(Bundle args) {
args.putParcelable("uri", uri);
args.putBoolean("junk", junk);
new SimpleTask<String>() {
2022-04-28 08:57:13 +00:00
private Toast toast = null;
2019-08-13 13:38:44 +00:00
@Override
2022-03-25 10:05:20 +00:00
protected void onPreExecute(Bundle args) {
2022-04-28 08:57:13 +00:00
toast = ToastEx.makeText(ActivityEML.this, R.string.title_executing, Toast.LENGTH_LONG);
toast.show();
}
@Override
protected void onPostExecute(Bundle args) {
if (toast != null)
toast.cancel();
2019-08-13 13:38:44 +00:00
}
@Override
2022-03-25 10:05:20 +00:00
protected String onExecute(Context context, Bundle args) throws Throwable {
Uri uri = args.getParcelable("uri");
2022-04-24 06:20:15 +00:00
boolean junk = args.getBoolean("junk");
2022-03-25 10:05:20 +00:00
long aid = args.getLong("account");
2019-08-13 16:22:38 +00:00
2022-03-25 10:05:20 +00:00
DB db = DB.getInstance(context);
EntityAccount account = db.account().getAccount(aid);
if (account == null)
return null;
EntityFolder folder = db.folder().getFolderByType(account.id,
junk ? EntityFolder.JUNK : EntityFolder.INBOX);
if (folder == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_folder));
2019-08-13 16:22:38 +00:00
2022-03-25 10:05:20 +00:00
ContentResolver resolver = context.getContentResolver();
try (InputStream is = resolver.openInputStream(uri)) {
2022-03-27 11:43:45 +00:00
if (is == null)
throw new FileNotFoundException(uri.toString());
2019-08-13 16:22:38 +00:00
2022-08-13 12:03:41 +00:00
Properties props = MessageHelper.getSessionProperties(true);
2022-03-25 10:05:20 +00:00
Session isession = Session.getInstance(props, null);
MimeMessage imessage = new MimeMessage(isession, is);
2019-08-13 16:22:38 +00:00
2022-03-25 10:05:20 +00:00
try (EmailService iservice = new EmailService(
2022-08-12 20:09:50 +00:00
context, account.getProtocol(), account.realm, account.encryption, account.insecure, account.unicode, true)) {
2022-03-25 10:05:20 +00:00
iservice.setPartialFetch(account.partial_fetch);
2023-01-20 23:10:54 +00:00
iservice.setRawFetch(account.raw_fetch);
2022-03-25 10:05:20 +00:00
iservice.setIgnoreBodyStructureSize(account.ignore_size);
iservice.connect(account);
2021-03-24 17:29:29 +00:00
2022-03-25 10:05:20 +00:00
IMAPFolder ifolder = (IMAPFolder) iservice.getStore().getFolder(folder.name);
ifolder.open(Folder.READ_WRITE);
2019-08-13 16:22:38 +00:00
2022-03-25 10:05:20 +00:00
if (ifolder.getPermanentFlags().contains(Flags.Flag.DRAFT))
imessage.setFlag(Flags.Flag.DRAFT, false);
2019-08-13 16:22:38 +00:00
2022-03-25 10:05:20 +00:00
ifolder.appendMessages(new Message[]{imessage});
}
}
EntityOperation.sync(context, folder.id, true);
ServiceSynchronize.eval(context, "EML");
return account.name + "/" + folder.name;
}
@Override
protected void onExecuted(Bundle args, String name) {
ToastEx.makeText(ActivityEML.this, name, Toast.LENGTH_LONG).show();
2019-08-13 13:38:44 +00:00
}
2022-05-14 19:48:19 +00:00
@Override
protected void onDestroyed(Bundle args) {
2022-05-29 11:57:54 +00:00
if (toast != null) {
toast.cancel();
toast = null;
}
2022-05-14 19:48:19 +00:00
}
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) {
2022-03-25 10:05:20 +00:00
if (ex instanceof IllegalArgumentException)
2023-12-12 19:55:55 +00:00
Snackbar.make(findViewById(android.R.id.content), new ThrowableWrapper(ex).getSafeMessage(), Snackbar.LENGTH_LONG)
2022-03-25 10:05:20 +00:00
.setGestureInsetBottomIgnored(true).show();
else
Log.unexpectedError(getSupportFragmentManager(), ex);
2019-08-13 13:38:44 +00:00
}
2022-03-25 10:05:20 +00:00
}.execute(ActivityEML.this, args, "eml:store");
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;
2022-02-06 21:43:36 +00:00
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;
2022-01-29 11:22:41 +00:00
Spanned headers;
2019-02-06 16:16:06 +00:00
}
2019-02-06 13:07:13 +00:00
}