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
|
|
|
|
2021-01-01 07:56:36 +00:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2018-08-02 13:33:06 +00:00
|
|
|
*/
|
|
|
|
|
2018-08-23 11:18:07 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
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
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.core.app.TaskStackBuilder;
|
2020-11-12 21:53:43 +00:00
|
|
|
import androidx.core.net.MailTo;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.fragment.app.FragmentManager;
|
|
|
|
import androidx.fragment.app.FragmentTransaction;
|
|
|
|
|
2018-08-23 11:18:07 +00:00
|
|
|
import java.util.ArrayList;
|
2020-12-28 11:52:08 +00:00
|
|
|
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);
|
|
|
|
|
2018-08-02 13:33:06 +00:00
|
|
|
getSupportFragmentManager().addOnBackStackChangedListener(this);
|
|
|
|
|
2020-07-06 08:28:00 +00:00
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() == 0)
|
|
|
|
handle(getIntent());
|
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);
|
|
|
|
handle(intent);
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackStackChanged() {
|
2019-01-28 20:00:08 +00:00
|
|
|
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
|
2021-02-10 18:35:41 +00:00
|
|
|
if (!isShared(getIntent().getAction())) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-15 19:52:12 +00:00
|
|
|
finishAndRemoveTask();
|
2019-01-28 20:00:08 +00:00
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|
2020-07-03 16:24:16 +00:00
|
|
|
|
|
|
|
private void handle(Intent intent) {
|
|
|
|
Bundle args;
|
|
|
|
String action = intent.getAction();
|
2021-02-01 19:06:20 +00:00
|
|
|
if (isShared(action)) {
|
2020-07-03 16:24:16 +00:00
|
|
|
args = new Bundle();
|
|
|
|
args.putString("action", "new");
|
|
|
|
args.putLong("account", -1);
|
|
|
|
|
|
|
|
Uri uri = intent.getData();
|
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
|
2020-11-12 21:53:43 +00:00
|
|
|
MailTo mailto = MailTo.parse(uri.toString());
|
2020-07-03 16:24:16 +00:00
|
|
|
|
|
|
|
String to = mailto.getTo();
|
|
|
|
if (to != null)
|
|
|
|
args.putString("to", to);
|
|
|
|
|
|
|
|
String cc = mailto.getCc();
|
|
|
|
if (cc != null)
|
|
|
|
args.putString("cc", cc);
|
|
|
|
|
|
|
|
String subject = mailto.getSubject();
|
|
|
|
if (subject != null)
|
|
|
|
args.putString("subject", subject);
|
|
|
|
|
2020-12-28 11:52:08 +00:00
|
|
|
Map<String, String> headers = mailto.getHeaders();
|
|
|
|
if (headers != null)
|
|
|
|
for (String key : headers.keySet())
|
|
|
|
if ("in-reply-to".equalsIgnoreCase(key)) {
|
|
|
|
args.putString("inreplyto", headers.get(key));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:24:16 +00:00
|
|
|
String body = mailto.getBody();
|
2020-09-03 11:52:37 +00:00
|
|
|
if (body != null) {
|
|
|
|
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)) {
|
|
|
|
String to = intent.getStringExtra(Intent.EXTRA_SHORTCUT_ID);
|
|
|
|
if (to != null)
|
|
|
|
args.putString("to", to);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_EMAIL)) {
|
|
|
|
String[] to = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
|
|
|
|
if (to != null)
|
|
|
|
args.putString("to", TextUtils.join(", ", to));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_CC)) {
|
|
|
|
String[] cc = intent.getStringArrayExtra(Intent.EXTRA_CC);
|
|
|
|
if (cc != null)
|
|
|
|
args.putString("cc", TextUtils.join(", ", cc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_BCC)) {
|
|
|
|
String[] bcc = intent.getStringArrayExtra(Intent.EXTRA_BCC);
|
|
|
|
if (bcc != null)
|
|
|
|
args.putString("bcc", TextUtils.join(", ", bcc));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_SUBJECT)) {
|
|
|
|
String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
|
|
|
|
if (subject != null)
|
|
|
|
args.putString("subject", subject);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_HTML_TEXT)) {
|
|
|
|
String html = intent.getStringExtra(Intent.EXTRA_HTML_TEXT);
|
|
|
|
if (!TextUtils.isEmpty(html))
|
|
|
|
args.putString("body", html);
|
|
|
|
} else if (intent.hasExtra(Intent.EXTRA_TEXT)) {
|
|
|
|
CharSequence body = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
|
|
|
|
if (body != null)
|
|
|
|
if (body instanceof Spanned)
|
|
|
|
args.putString("body", HtmlHelper.toHtml((Spanned) body, this));
|
|
|
|
else {
|
|
|
|
String text = body.toString();
|
|
|
|
if (!TextUtils.isEmpty(text)) {
|
|
|
|
String html = "<span>" + text.replaceAll("\\r?\\n", "<br>") + "</span>";
|
|
|
|
args.putString("body", html);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_STREAM))
|
|
|
|
if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
|
2021-01-23 09:26:05 +00:00
|
|
|
ArrayList<Uri> streams = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
|
|
|
|
if (streams != null) {
|
|
|
|
// Some apps send null streams
|
|
|
|
ArrayList<Uri> uris = new ArrayList<>();
|
|
|
|
for (Uri stream : streams)
|
|
|
|
if (stream != null)
|
|
|
|
uris.add(stream);
|
|
|
|
if (uris.size() > 0)
|
|
|
|
args.putParcelableArrayList("attachments", uris);
|
|
|
|
}
|
2020-07-03 16:24:16 +00:00
|
|
|
} else {
|
|
|
|
Uri stream = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
|
|
|
if (stream != null) {
|
|
|
|
ArrayList<Uri> uris = new ArrayList<>();
|
|
|
|
uris.add(stream);
|
|
|
|
args.putParcelableArrayList("attachments", uris);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
args = intent.getExtras();
|
|
|
|
|
|
|
|
FragmentCompose fragment = new FragmentCompose();
|
|
|
|
fragment.setArguments(args);
|
|
|
|
|
|
|
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
|
|
|
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));
|
|
|
|
}
|
2018-08-02 13:33:06 +00:00
|
|
|
}
|