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

214 lines
8.4 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
2018-12-31 08:04:33 +00:00
Copyright 2018-2019 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2018-08-23 11:18:07 +00:00
import android.content.Intent;
2019-02-18 07:50:12 +00:00
import android.net.MailTo;
2018-08-23 11:18:07 +00:00
import android.net.Uri;
2018-08-02 13:33:06 +00:00
import android.os.Bundle;
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-10-29 11:31:45 +00:00
import android.view.MenuItem;
2018-08-02 13:33:06 +00:00
2019-01-25 06:44:41 +00:00
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
2018-08-23 11:18:07 +00:00
import java.util.ArrayList;
2018-12-17 16:42:45 +00:00
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
2019-01-28 20:00:08 +00:00
import androidx.core.app.TaskStackBuilder;
2018-08-08 06:55:47 +00:00
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
2018-10-29 11:31:45 +00:00
import androidx.lifecycle.Lifecycle;
2018-08-08 06:55:47 +00:00
2018-09-04 18:51:09 +00:00
public class ActivityCompose extends ActivityBilling implements FragmentManager.OnBackStackChangedListener {
2018-08-03 07:39:43 +00:00
static final int REQUEST_CONTACT_TO = 1;
static final int REQUEST_CONTACT_CC = 2;
static final int REQUEST_CONTACT_BCC = 3;
2018-09-13 18:24:48 +00:00
static final int REQUEST_IMAGE = 4;
static final int REQUEST_ATTACHMENT = 5;
2018-10-28 12:49:13 +00:00
static final int REQUEST_ENCRYPT = 6;
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);
if (getSupportFragmentManager().getFragments().size() == 0) {
2018-08-23 11:18:07 +00:00
Bundle args;
2018-09-04 10:33:27 +00:00
Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action) ||
Intent.ACTION_SENDTO.equals(action) ||
Intent.ACTION_SEND.equals(action) ||
Intent.ACTION_SEND_MULTIPLE.equals(action)) {
2019-02-07 12:44:14 +00:00
Log.i(intent.toString());
2019-01-25 14:40:46 +00:00
Log.logExtras(intent);
2018-08-23 11:18:07 +00:00
args = new Bundle();
args.putString("action", "new");
args.putLong("account", -1);
2018-09-04 10:33:27 +00:00
Uri uri = intent.getData();
2018-10-28 07:27:22 +00:00
if (uri != null && "mailto".equals(uri.getScheme())) {
2019-02-18 07:50:12 +00:00
// https://www.ietf.org/rfc/rfc2368.txt
MailTo mailto = MailTo.parse(uri.toString());
String to = mailto.getTo();
2018-10-28 07:27:22 +00:00
if (to != null)
2018-12-17 16:42:45 +00:00
try {
InternetAddress.parse(to);
args.putString("to", to);
} catch (AddressException ex) {
2018-12-24 12:27:45 +00:00
Log.w(ex);
2018-12-17 16:42:45 +00:00
}
2019-02-18 07:50:12 +00:00
String cc = mailto.getCc();
if (cc != null)
try {
InternetAddress.parse(cc);
args.putString("cc", cc);
} catch (AddressException ex) {
Log.w(ex);
}
String subject = mailto.getSubject();
if (subject != null)
args.putString("subject", subject);
String body = mailto.getBody();
if (body != null)
args.putString("body", body);
2018-10-28 07:27:22 +00:00
}
if (intent.hasExtra(Intent.EXTRA_EMAIL)) {
String[] to = intent.getStringArrayExtra(Intent.EXTRA_EMAIL);
if (to != null)
2018-12-17 16:42:45 +00:00
try {
InternetAddress.parse(TextUtils.join(", ", to));
args.putString("to", TextUtils.join(", ", to));
} catch (AddressException ex) {
2018-12-24 12:27:45 +00:00
Log.w(ex);
2018-12-17 16:42:45 +00:00
}
2018-10-28 07:27:22 +00:00
}
if (intent.hasExtra(Intent.EXTRA_CC)) {
String[] cc = intent.getStringArrayExtra(Intent.EXTRA_CC);
if (cc != null)
2018-12-17 16:42:45 +00:00
try {
InternetAddress.parse(TextUtils.join(", ", cc));
args.putString("cc", TextUtils.join(", ", cc));
} catch (AddressException ex) {
2018-12-24 12:27:45 +00:00
Log.w(ex);
2018-12-17 16:42:45 +00:00
}
2018-10-28 07:27:22 +00:00
}
if (intent.hasExtra(Intent.EXTRA_BCC)) {
String[] bcc = intent.getStringArrayExtra(Intent.EXTRA_BCC);
if (bcc != null)
2018-12-17 16:42:45 +00:00
try {
InternetAddress.parse(TextUtils.join(", ", bcc));
args.putString("bcc", TextUtils.join(", ", bcc));
} catch (AddressException ex) {
2018-12-24 12:27:45 +00:00
Log.w(ex);
2018-12-17 16:42:45 +00:00
}
2018-10-28 07:27:22 +00:00
}
if (intent.hasExtra(Intent.EXTRA_SUBJECT)) {
String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
if (subject != null)
2019-01-05 11:17:33 +00:00
args.putString("subject", subject);
2018-10-28 07:27:22 +00:00
}
2019-01-05 11:17:33 +00:00
if (intent.hasExtra(Intent.EXTRA_HTML_TEXT)) {
String html = intent.getStringExtra(Intent.EXTRA_HTML_TEXT);
if (html != null)
2019-01-25 06:44:41 +00:00
args.putString("body", Jsoup.clean(html, Whitelist.relaxed()));
2019-01-05 11:17:33 +00:00
} else if (intent.hasExtra(Intent.EXTRA_TEXT)) {
2019-01-25 14:40:46 +00:00
CharSequence body = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
2018-10-28 07:27:22 +00:00
if (body != null)
2019-01-25 14:40:46 +00:00
if (body instanceof Spanned)
2019-02-10 12:01:21 +00:00
args.putString("body", Jsoup.clean(HtmlHelper.toHtml((Spanned) body), Whitelist.relaxed()));
2019-01-25 14:40:46 +00:00
else
args.putString("body", body.toString()); // TODO: clean?
2018-10-28 07:27:22 +00:00
}
2018-08-23 11:18:07 +00:00
2018-09-04 10:33:27 +00:00
if (intent.hasExtra(Intent.EXTRA_STREAM))
2018-10-28 07:27:22 +00:00
if (Intent.ACTION_SEND_MULTIPLE.equals(action)) {
ArrayList<Uri> uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (uris != null)
args.putParcelableArrayList("attachments", uris);
} else {
Uri stream = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (stream != null) {
ArrayList<Uri> uris = new ArrayList<>();
uris.add((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
args.putParcelableArrayList("attachments", uris);
}
2018-08-23 11:18:07 +00:00
}
} else
2018-09-04 10:33:27 +00:00
args = intent.getExtras();
2018-08-23 11:18:07 +00:00
2018-08-03 09:58:44 +00:00
FragmentCompose fragment = new FragmentCompose();
2018-08-23 11:18:07 +00:00
fragment.setArguments(args);
2018-08-03 09:58:44 +00:00
2018-08-02 13:33:06 +00:00
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose");
fragmentTransaction.commit();
}
}
@Override
public void onBackStackChanged() {
2019-01-28 20:00:08 +00:00
if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
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);
}
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
}
2018-10-29 11:31:45 +00:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED))
2019-02-05 19:15:45 +00:00
onBackPressed();
2018-10-29 11:31:45 +00:00
return true;
default:
return false;
}
}
2018-08-02 13:33:06 +00:00
}