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

430 lines
16 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2021-03-27 20:35:31 +00:00
import android.app.NotificationManager;
2023-02-05 13:14:44 +00:00
import android.content.ClipData;
2021-03-27 20:35:31 +00:00
import android.content.Context;
2018-08-23 11:18:07 +00:00
import android.content.Intent;
import android.content.SharedPreferences;
2018-08-23 11:18:07 +00:00
import android.net.Uri;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
2020-09-03 11:52:37 +00:00
import android.text.Html;
2019-01-25 14:40:46 +00:00
import android.text.Spanned;
2018-08-23 11:18:07 +00:00
import android.text.TextUtils;
2018-08-02 13:33:06 +00:00
import androidx.core.app.TaskStackBuilder;
import androidx.core.net.MailTo;
2021-08-23 07:19:05 +00:00
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
2021-08-23 10:01:37 +00:00
import androidx.lifecycle.Lifecycle;
2021-03-27 20:35:31 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceManager;
2023-05-15 07:26:32 +00:00
import org.jsoup.nodes.Document;
2021-03-27 20:35:31 +00:00
import java.io.File;
2018-08-23 11:18:07 +00:00
import java.util.ArrayList;
2023-02-05 13:14:44 +00:00
import java.util.Arrays;
2021-03-27 20:35:31 +00:00
import java.util.List;
import java.util.Map;
2018-08-23 11:18:07 +00:00
2019-08-13 08:27:17 +00:00
public class ActivityCompose extends ActivityBase implements FragmentManager.OnBackStackChangedListener {
2019-05-08 11:15:02 +00:00
static final int PI_REPLY = 1;
2018-08-02 13:33:06 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compose);
2018-08-04 16:54:57 +00:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setCustomView(R.layout.action_bar);
getSupportActionBar().setDisplayShowCustomEnabled(true);
2018-08-04 16:54:57 +00:00
2018-08-02 13:33:06 +00:00
getSupportFragmentManager().addOnBackStackChangedListener(this);
2020-07-06 08:28:00 +00:00
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
2021-08-23 07:19:05 +00:00
handle(getIntent(), true);
2020-07-03 16:24:16 +00:00
}
2018-08-03 09:58:44 +00:00
2020-07-03 16:24:16 +00:00
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
2021-08-23 07:19:05 +00:00
handle(intent, false);
2018-08-02 13:33:06 +00:00
}
@Override
public void onBackStackChanged() {
2019-01-28 20:00:08 +00:00
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
2023-01-22 11:43:33 +00:00
Intent intent = getIntent();
String action = intent.getAction();
2023-06-02 15:52:34 +00:00
boolean shared = (isShared(action) && !intent.hasExtra("fair:account"));
2023-01-22 11:43:33 +00:00
boolean widget = (action != null && action.startsWith("widget:"));
String[] tos = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
boolean cloud = (tos != null && tos.length == 1 && BuildConfig.CLOUD_EMAIL.equals(tos[0]));
if (cloud) {
Intent setup = new Intent(this, ActivitySetup.class)
.setAction("misc")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
.putExtra("tab", "backup");
startActivity(setup);
2023-04-29 10:36:14 +00:00
} else if (!shared && !widget) {
2021-02-10 18:35:41 +00:00
Intent parent = getParentActivityIntent();
if (parent != null)
if (shouldUpRecreateTask(parent))
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(parent)
.startActivities();
else {
parent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parent);
}
2021-02-01 19:06:20 +00:00
}
2022-07-04 14:44:40 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().remove("last_composing").apply();
2023-04-29 10:36:14 +00:00
try {
if (shared || widget) {
Helper.excludeFromRecents(this);
finishAffinity();
} else
finishAndRemoveTask();
} catch (Throwable ex) {
Log.e(ex);
finish();
}
2019-01-28 20:00:08 +00:00
}
2018-08-02 13:33:06 +00:00
}
2020-07-03 16:24:16 +00:00
2021-08-23 07:19:05 +00:00
private void handle(Intent intent, boolean create) {
2020-07-03 16:24:16 +00:00
Bundle args;
String action = intent.getAction();
2021-08-23 07:19:05 +00:00
Log.i("Handle action=" + action + " create=" + create + " " + this);
2021-02-01 19:06:20 +00:00
if (isShared(action)) {
2020-07-03 16:24:16 +00:00
args = new Bundle();
Uri uri = intent.getData();
2023-01-06 20:03:08 +00:00
// Workaround mailto in email address
if (uri == null && intent.hasExtra(Intent.EXTRA_EMAIL))
try {
String[] to = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
if (to != null && to.length == 1 &&
to[0] != null && to[0].startsWith("mailto:")) {
uri = Uri.parse(to[0]);
intent.removeExtra(Intent.EXTRA_EMAIL);
}
} catch (Throwable ex) {
Log.w(ex);
}
2020-11-14 13:27:53 +00:00
if (uri != null && "mailto".equalsIgnoreCase(uri.getScheme())) {
2020-07-03 16:24:16 +00:00
// https://www.ietf.org/rfc/rfc2368.txt
MailTo mailto = MailTo.parse(uri.toString());
2020-07-03 16:24:16 +00:00
2021-06-22 20:07:19 +00:00
List<String> to = sanitize(new String[]{mailto.getTo()});
2021-07-24 18:10:44 +00:00
if (to.size() > 0)
2021-06-22 20:07:19 +00:00
args.putString("to", to.get(0));
2021-05-22 15:20:58 +00:00
2021-06-22 20:07:19 +00:00
List<String> cc = sanitize(new String[]{mailto.getCc()});
2021-07-24 18:10:44 +00:00
if (cc.size() > 0)
2021-06-22 20:07:19 +00:00
args.putString("cc", cc.get(0));
2020-07-03 16:24:16 +00:00
String subject = mailto.getSubject();
2021-07-24 18:10:44 +00:00
if (!TextUtils.isEmpty(subject))
2020-07-03 16:24:16 +00:00
args.putString("subject", subject);
Map<String, String> headers = mailto.getHeaders();
if (headers != null)
2021-07-24 18:10:44 +00:00
for (String key : headers.keySet()) {
List<String> address = sanitize(new String[]{headers.get(key)});
if (address.size() == 0)
continue;
2021-06-29 19:50:12 +00:00
if ("bcc".equalsIgnoreCase(key))
2021-07-24 18:10:44 +00:00
args.putString("bcc", address.get(0));
2021-06-29 19:50:12 +00:00
else if ("in-reply-to".equalsIgnoreCase(key))
2021-07-24 18:10:44 +00:00
args.putString("inreplyto", address.get(0));
}
2020-07-03 16:24:16 +00:00
String body = mailto.getBody();
2021-07-24 18:10:44 +00:00
if (!TextUtils.isEmpty(body)) {
2020-09-03 11:52:37 +00:00
StringBuilder sb = new StringBuilder();
for (String line : body.split("\\r?\\n"))
sb.append("<span>").append(Html.escapeHtml(line)).append("<span><br>");
args.putString("body", sb.toString());
}
2020-07-03 16:24:16 +00:00
}
if (intent.hasExtra(Intent.EXTRA_SHORTCUT_ID)) {
2021-07-24 18:10:44 +00:00
List<String> to = sanitize(new String[]{intent.getStringExtra(Intent.EXTRA_SHORTCUT_ID)});
if (to.size() > 0)
args.putString("to", to.get(0));
2020-07-03 16:24:16 +00:00
}
if (intent.hasExtra(Intent.EXTRA_EMAIL)) {
2021-05-21 18:40:55 +00:00
List<String> to = sanitize(intent.getStringArrayExtra(Intent.EXTRA_EMAIL));
if (to.size() > 0)
2020-07-03 16:24:16 +00:00
args.putString("to", TextUtils.join(", ", to));
}
if (intent.hasExtra(Intent.EXTRA_CC)) {
2021-05-21 18:40:55 +00:00
List<String> cc = sanitize(intent.getStringArrayExtra(Intent.EXTRA_CC));
if (cc.size() > 0)
2020-07-03 16:24:16 +00:00
args.putString("cc", TextUtils.join(", ", cc));
}
if (intent.hasExtra(Intent.EXTRA_BCC)) {
2021-05-21 18:40:55 +00:00
List<String> bcc = sanitize(intent.getStringArrayExtra(Intent.EXTRA_BCC));
if (bcc.size() > 0)
2020-07-03 16:24:16 +00:00
args.putString("bcc", TextUtils.join(", ", bcc));
}
if (intent.hasExtra(Intent.EXTRA_SUBJECT)) {
String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
2021-07-24 18:10:44 +00:00
if (!TextUtils.isEmpty(subject))
2020-07-03 16:24:16 +00:00
args.putString("subject", subject);
}
2021-07-24 18:10:44 +00:00
String html = null;
if (intent.hasExtra(Intent.EXTRA_HTML_TEXT))
html = intent.getStringExtra(Intent.EXTRA_HTML_TEXT);
if (TextUtils.isEmpty(html) &&
intent.hasExtra(Intent.EXTRA_TEXT)) {
2020-07-03 16:24:16 +00:00
CharSequence body = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
if (body != null)
if (body instanceof Spanned)
2021-07-24 18:10:44 +00:00
html = HtmlHelper.toHtml((Spanned) body, this);
2020-07-03 16:24:16 +00:00
else {
String text = body.toString();
2023-05-15 07:26:32 +00:00
if (!TextUtils.isEmpty(text)) {
2021-07-24 18:10:44 +00:00
html = "<span>" + text.replaceAll("\\r?\\n", "<br>") + "</span>";
2023-05-15 07:26:32 +00:00
Document d = JsoupEx.parse(html);
HtmlHelper.autoLink(d, true);
html = d.body().html();
}
2020-07-03 16:24:16 +00:00
}
}
2021-07-24 18:10:44 +00:00
if (!TextUtils.isEmpty(html))
args.putString("body", html);
2023-02-05 13:14:44 +00:00
ArrayList<Uri> uris = new ArrayList<>();
ClipData clip = intent.getClipData();
if (clip != null)
for (int i = 0; i < clip.getItemCount(); i++) {
ClipData.Item item = clip.getItemAt(i);
Uri stream = (item == null ? null : item.getUri());
if (stream != null)
2020-07-03 16:24:16 +00:00
uris.add(stream);
}
2023-02-05 13:14:44 +00:00
if (intent.hasExtra(Intent.EXTRA_STREAM)) {
ArrayList<Uri> streams = (Intent.ACTION_SEND_MULTIPLE.equals(action)
? intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
: new ArrayList<>(Arrays.asList((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM))));
if (streams != null) {
// Some apps send null streams
for (Uri stream : streams)
if (stream != null) {
boolean found = false;
for (Uri e : uris)
if (stream.equals(e)) {
found = true;
break;
}
if (!found)
uris.add(stream);
}
}
}
if (uris.size() > 0)
args.putParcelableArrayList("attachments", uris);
2020-07-03 16:24:16 +00:00
} else
args = intent.getExtras();
2021-08-23 07:19:05 +00:00
FragmentManager fm = getSupportFragmentManager();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean attach_new = prefs.getBoolean("attach_new", true);
if (!attach_new && !create &&
args.size() == 1 &&
(args.containsKey("to") ||
args.containsKey("attachments"))) {
2021-08-23 07:19:05 +00:00
List<Fragment> fragments = fm.getFragments();
if (fragments.size() == 1 &&
fragments.get(0) instanceof FragmentCompose) {
FragmentCompose fragment = ((FragmentCompose) fragments.get(0));
if (args.containsKey("to"))
fragment.onAddTo(args.getString("to"));
else if (args.containsKey("attachments"))
fragment.onSharedAttachments(args.getParcelableArrayList("attachments"));
2021-08-23 07:19:05 +00:00
return;
}
}
if (isShared(action)) {
args.putString("action", "new");
2022-01-31 19:15:17 +00:00
args.putLong("account",
intent.getLongExtra("fair:account", -1L));
2021-08-23 07:19:05 +00:00
}
2021-08-23 10:01:37 +00:00
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
getSupportFragmentManager().popBackStack("compose", FragmentManager.POP_BACK_STACK_INCLUSIVE);
2021-08-23 08:23:49 +00:00
2020-07-03 16:24:16 +00:00
FragmentCompose fragment = new FragmentCompose();
fragment.setArguments(args);
2021-08-23 07:19:05 +00:00
FragmentTransaction fragmentTransaction = fm.beginTransaction();
2020-07-03 16:24:16 +00:00
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose");
fragmentTransaction.commit();
}
2021-02-01 19:06:20 +00:00
private static boolean isShared(String action) {
return (Intent.ACTION_VIEW.equals(action) ||
Intent.ACTION_SENDTO.equals(action) ||
Intent.ACTION_SEND.equals(action) ||
Intent.ACTION_SEND_MULTIPLE.equals(action));
}
2021-03-27 20:35:31 +00:00
2021-05-21 18:40:55 +00:00
private List<String> sanitize(String[] addresses) {
List<String> result = new ArrayList<>();
if (addresses != null)
for (String address : addresses) {
2021-06-23 21:05:20 +00:00
if (TextUtils.isEmpty(address))
2021-06-22 20:07:19 +00:00
continue;
2021-08-20 20:09:20 +00:00
address = address.replaceAll("\\s+", " ");
2021-05-22 16:14:43 +00:00
address = address.replaceAll("\u200b", ""); // Discord: zero width space
2021-05-21 18:40:55 +00:00
if (!TextUtils.isEmpty(address))
result.add(address);
}
return result;
}
2021-03-28 17:56:35 +00:00
static void undoSend(final long id, final Context context, final LifecycleOwner owner, final FragmentManager manager) {
2021-03-27 20:35:31 +00:00
Bundle args = new Bundle();
args.putLong("id", id);
2021-03-28 17:56:35 +00:00
new SimpleTask<Long>() {
2021-03-27 20:35:31 +00:00
@Override
2021-03-28 17:56:35 +00:00
protected Long onExecute(Context context, Bundle args) {
2021-03-27 20:35:31 +00:00
long id = args.getLong("id");
2021-03-28 17:56:35 +00:00
return undoSend(id, context);
}
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
@Override
protected void onExecuted(Bundle args, Long id) {
if (id == null)
return;
context.startActivity(
new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id));
}
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(manager, ex, !(ex instanceof IllegalArgumentException));
}
}.execute(context, owner, args, "undo:sent");
}
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
static Long undoSend(long id, Context context) {
DB db = DB.getInstance(context);
2021-03-27 20:35:31 +00:00
2022-04-07 06:45:43 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean save_drafts = prefs.getBoolean("save_drafts", true);
2021-04-01 06:42:07 +00:00
// Cancel send
2021-03-28 17:56:35 +00:00
EntityOperation operation = db.operation().getOperation(id, EntityOperation.SEND);
if (operation != null)
2021-04-01 06:42:07 +00:00
if ("executing".equals(operation.state))
2021-03-28 17:56:35 +00:00
return null;
2021-04-01 06:42:07 +00:00
else
2021-03-28 17:56:35 +00:00
db.operation().deleteOperation(operation.id);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
EntityMessage message;
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
try {
db.beginTransaction();
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
message = db.message().getMessage(id);
if (message == null)
return null;
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
db.folder().setFolderError(message.folder, null);
if (message.identity != null)
db.identity().setIdentityError(message.identity, null);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
File source = message.getFile(context);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
// Insert into drafts
EntityFolder drafts = db.folder().getFolderByType(message.account, EntityFolder.DRAFTS);
if (drafts == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_drafts));
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
message.id = null;
message.folder = drafts.id;
message.fts = false;
message.ui_snoozed = null;
message.error = null;
message.id = db.message().insertMessage(message);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
File target = message.getFile(context);
source.renameTo(target);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
List<EntityAttachment> attachments = db.attachment().getAttachments(id);
for (EntityAttachment attachment : attachments)
db.attachment().setMessage(attachment.id, message.id);
2021-03-27 20:35:31 +00:00
2022-04-07 06:45:43 +00:00
if (save_drafts &&
(message.ui_encrypt == null ||
EntityMessage.ENCRYPT_NONE.equals(message.ui_encrypt)))
EntityOperation.queue(context, message, EntityOperation.ADD);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
// Delete from outbox
db.message().deleteMessage(id); // will delete operation too
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
ServiceSynchronize.eval(context, "outbox/drafts");
2021-03-27 20:35:31 +00:00
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(context, NotificationManager.class);
2021-07-15 16:36:39 +00:00
nm.cancel("send:" + id, NotificationHelper.NOTIFICATION_TAGGED);
2021-03-27 20:35:31 +00:00
2021-03-28 17:56:35 +00:00
return message.id;
2021-03-27 20:35:31 +00:00
}
2018-08-02 13:33:06 +00:00
}