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

667 lines
26 KiB
Java
Raw Normal View History

package eu.faircode.email;
/*
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/>.
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
*/
2020-02-21 16:38:46 +00:00
import android.app.AlarmManager;
import android.app.NotificationManager;
2019-03-19 07:05:00 +00:00
import android.app.PendingIntent;
2019-10-04 15:17:42 +00:00
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
2019-10-04 15:17:42 +00:00
import android.content.IntentFilter;
2019-04-04 13:54:07 +00:00
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.PowerManager;
import android.text.TextUtils;
2019-10-03 17:25:08 +00:00
import androidx.annotation.NonNull;
2020-02-21 16:38:46 +00:00
import androidx.core.app.AlarmManagerCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
2019-05-06 11:34:47 +00:00
import androidx.preference.PreferenceManager;
2020-02-20 09:35:01 +00:00
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
2019-07-31 07:55:45 +00:00
import java.util.HashMap;
import java.util.List;
2019-07-31 07:55:45 +00:00
import java.util.Map;
import java.util.Properties;
2019-06-23 17:09:02 +00:00
import java.util.concurrent.ExecutorService;
import javax.mail.Address;
2019-06-23 18:56:07 +00:00
import javax.mail.AuthenticationFailedException;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
2020-05-19 05:36:41 +00:00
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
2019-05-06 20:34:18 +00:00
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
2019-07-26 11:55:00 +00:00
public class ServiceSend extends ServiceBase {
2020-03-11 09:09:52 +00:00
private TupleUnsent lastUnsent = null;
2019-06-23 17:09:02 +00:00
private boolean lastSuitable = false;
2019-10-03 17:25:08 +00:00
private PowerManager.WakeLock wlOutbox;
2020-05-03 18:50:17 +00:00
private TwoStateOwner owner = new TwoStateOwner("send");
2020-05-04 08:02:35 +00:00
private static ExecutorService executor = Helper.getBackgroundExecutor(1, "send");
2019-02-28 07:55:08 +00:00
2020-02-21 16:38:46 +00:00
private static final int PI_SEND = 1;
private static final long CONNECTIVITY_DELAY = 5000L; // milliseconds
private static final int IDENTITY_ERROR_AFTER = 30; // minutes
2020-06-11 17:28:47 +00:00
private static final int RETRY_MAX = 3;
@Override
public void onCreate() {
2019-08-17 11:06:57 +00:00
EntityLog.log(this, "Service send create");
super.onCreate();
2019-10-04 15:17:42 +00:00
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
2019-10-03 17:25:08 +00:00
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wlOutbox = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
2019-06-23 17:09:02 +00:00
// Observe unsent count
2019-10-03 17:25:08 +00:00
DB db = DB.getInstance(this);
2020-03-11 09:09:52 +00:00
db.operation().liveUnsent().observe(this, new Observer<TupleUnsent>() {
@Override
2020-03-11 09:09:52 +00:00
public void onChanged(TupleUnsent unsent) {
if (unsent == null || !unsent.equals(lastUnsent)) {
2019-10-04 15:17:42 +00:00
lastUnsent = unsent;
2020-02-22 21:09:46 +00:00
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
} catch (Throwable ex) {
Log.w(ex);
}
2019-10-04 15:17:42 +00:00
}
}
});
2019-06-23 17:09:02 +00:00
// Observe send operations
2020-05-03 18:50:17 +00:00
db.operation().liveOperations(null).observe(owner, new Observer<List<TupleOperationEx>>() {
2019-06-23 17:09:02 +00:00
private List<Long> handling = new ArrayList<>();
@Override
2020-01-25 09:49:59 +00:00
public void onChanged(final List<TupleOperationEx> operations) {
2019-06-23 17:09:02 +00:00
boolean process = false;
List<Long> ops = new ArrayList<>();
for (EntityOperation op : operations) {
if (!handling.contains(op.id))
process = true;
2020-06-11 13:56:14 +00:00
ops.add(op.id);
2019-06-23 17:09:02 +00:00
}
2019-06-23 17:09:02 +00:00
handling = ops;
if (process) {
2019-06-23 17:09:02 +00:00
Log.i("OUTBOX operations=" + operations.size());
executor.submit(new Runnable() {
@Override
public void run() {
2019-10-03 17:25:08 +00:00
processOperations();
2019-06-23 17:09:02 +00:00
}
});
}
}
});
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
cm.registerNetworkCallback(builder.build(), networkCallback);
2019-10-04 15:17:42 +00:00
IntentFilter iif = new IntentFilter();
iif.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
iif.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(connectionChangedReceiver, iif);
}
@Override
public void onDestroy() {
2019-08-17 11:06:57 +00:00
EntityLog.log(this, "Service send destroy");
2019-10-04 15:17:42 +00:00
unregisterReceiver(connectionChangedReceiver);
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(networkCallback);
stopForeground(true);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel(Helper.NOTIFICATION_SEND);
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
2019-10-04 15:17:42 +00:00
startForeground(Helper.NOTIFICATION_SEND, getNotificationService().build());
return START_STICKY;
}
2019-10-04 15:17:42 +00:00
NotificationCompat.Builder getNotificationService() {
2019-06-23 08:23:49 +00:00
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "send")
.setSmallIcon(R.drawable.baseline_send_24)
.setContentTitle(getString(R.string.title_notification_sending))
.setContentIntent(getPendingIntent(this))
2019-06-23 08:23:49 +00:00
.setAutoCancel(false)
.setShowWhen(true)
.setDefaults(0) // disable sound on pre Android 8
2020-02-04 07:51:05 +00:00
.setLocalOnly(true)
2019-06-23 08:23:49 +00:00
.setPriority(NotificationCompat.PRIORITY_MIN)
2019-07-23 09:38:36 +00:00
.setCategory(NotificationCompat.CATEGORY_SERVICE)
2019-06-23 08:23:49 +00:00
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
2020-03-11 10:07:31 +00:00
if (lastUnsent != null && lastUnsent.count != null)
2019-03-03 09:17:42 +00:00
builder.setContentText(getResources().getQuantityString(
2020-03-11 09:09:52 +00:00
R.plurals.title_notification_unsent, lastUnsent.count, lastUnsent.count));
2020-03-11 10:07:31 +00:00
if (lastUnsent == null || lastUnsent.busy == null || lastUnsent.busy == 0)
2020-03-11 09:09:52 +00:00
builder.setSubText(getString(R.string.title_notification_idle));
2019-06-23 17:09:02 +00:00
if (!lastSuitable)
builder.setSubText(getString(R.string.title_notification_waiting));
return builder;
}
private static PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent(context, ActivityView.class);
intent.setAction("outbox");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return PendingIntent.getActivity(context, ActivityView.REQUEST_OUTBOX, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.i("Service send available=" + network);
2019-10-03 17:25:08 +00:00
checkConnectivity();
2019-02-28 08:07:01 +00:00
}
2019-02-28 08:07:01 +00:00
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities caps) {
2019-03-16 12:06:50 +00:00
Log.i("Service send network=" + network + " caps=" + caps);
2019-10-03 17:25:08 +00:00
checkConnectivity();
2019-02-28 08:07:01 +00:00
}
2019-10-03 17:25:08 +00:00
@Override
public void onLost(@NonNull Network network) {
Log.i("Service send lost=" + network);
checkConnectivity();
}
2019-10-04 15:17:42 +00:00
};
2019-02-28 08:07:01 +00:00
2019-10-04 15:17:42 +00:00
private BroadcastReceiver connectionChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("Received intent=" + intent +
" " + TextUtils.join(" ", Log.getExtras(intent.getExtras())));
checkConnectivity();
}
};
2019-10-04 15:17:42 +00:00
private void checkConnectivity() {
boolean suitable = ConnectionHelper.getNetworkState(this).isSuitable();
2019-10-04 15:17:42 +00:00
if (lastSuitable != suitable) {
lastSuitable = suitable;
EntityLog.log(this, "Service send suitable=" + suitable);
2019-10-04 15:17:42 +00:00
2020-02-22 21:09:46 +00:00
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(Helper.NOTIFICATION_SEND, getNotificationService().build());
} catch (Throwable ex) {
Log.w(ex);
}
// Wait for stabilization of connection
2020-05-03 18:50:17 +00:00
if (suitable)
try {
Thread.sleep(CONNECTIVITY_DELAY);
} catch (InterruptedException ex) {
Log.w(ex);
}
if (suitable)
owner.start();
else
owner.stop();
2019-10-04 15:17:42 +00:00
}
if (suitable)
executor.submit(new Runnable() {
@Override
public void run() {
processOperations();
}
});
2019-10-04 15:17:42 +00:00
}
2019-10-03 17:25:08 +00:00
private void processOperations() {
try {
wlOutbox.acquire();
if (!ConnectionHelper.getNetworkState(this).isSuitable())
return;
2019-10-03 17:25:08 +00:00
DB db = DB.getInstance(this);
EntityFolder outbox = db.folder().getOutbox();
try {
2020-06-11 17:28:47 +00:00
db.folder().setFolderError(outbox.id, null);
2019-10-03 17:25:08 +00:00
db.folder().setFolderSyncState(outbox.id, "syncing");
2020-01-25 09:49:59 +00:00
List<TupleOperationEx> ops = db.operation().getOperations(outbox.id);
2019-10-03 17:25:08 +00:00
Log.i(outbox.name + " pending operations=" + ops.size());
2020-06-11 17:28:47 +00:00
while (ops.size() > 0) {
TupleOperationEx op = ops.get(0);
2019-10-03 17:25:08 +00:00
EntityMessage message = null;
2020-06-11 17:28:47 +00:00
if (op.message != null)
message = db.message().getMessage(op.message);
2019-10-03 17:25:08 +00:00
try {
Log.i(outbox.name +
" start op=" + op.id + "/" + op.name +
" msg=" + op.message +
" args=" + op.args);
2020-06-11 17:28:47 +00:00
db.operation().setOperationTries(op.id, ++op.tries);
db.operation().setOperationError(op.id, null);
if (message != null)
db.message().setMessageError(message.id, null);
2020-02-12 16:34:41 +00:00
db.operation().setOperationState(op.id, "executing");
2019-10-03 17:25:08 +00:00
Map<String, String> crumb = new HashMap<>();
crumb.put("name", op.name);
crumb.put("args", op.args);
crumb.put("folder", op.folder + ":outbox");
if (op.message != null)
crumb.put("message", Long.toString(op.message));
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
Log.breadcrumb("operation", crumb);
switch (op.name) {
case EntityOperation.SYNC:
2020-04-02 19:13:27 +00:00
onSync(outbox);
2019-10-03 17:25:08 +00:00
break;
case EntityOperation.SEND:
if (message == null)
throw new MessageRemovedException();
onSend(message);
break;
case EntityOperation.ANSWERED:
break;
default:
throw new IllegalArgumentException("Unknown operation=" + op.name);
}
db.operation().deleteOperation(op.id);
2020-06-11 17:28:47 +00:00
ops.remove(op);
2019-10-03 17:25:08 +00:00
} catch (Throwable ex) {
Log.e(outbox.name, ex);
2019-12-06 07:50:46 +00:00
EntityLog.log(this, outbox.name + " " + Log.formatThrowable(ex, false));
2019-10-03 17:25:08 +00:00
2019-12-06 07:50:46 +00:00
db.operation().setOperationError(op.id, Log.formatThrowable(ex));
2019-10-03 17:25:08 +00:00
if (message != null)
2019-12-06 07:50:46 +00:00
db.message().setMessageError(message.id, Log.formatThrowable(ex));
2019-10-03 17:25:08 +00:00
2020-06-11 17:28:47 +00:00
if (op.tries >= RETRY_MAX ||
ex instanceof OutOfMemoryError ||
2019-10-03 17:25:08 +00:00
ex instanceof MessageRemovedException ||
ex instanceof FileNotFoundException ||
2020-05-23 20:24:24 +00:00
ex instanceof AuthenticationFailedException ||
2019-10-03 17:25:08 +00:00
ex instanceof SendFailedException ||
ex instanceof IllegalArgumentException) {
Log.w("Unrecoverable");
db.operation().deleteOperation(op.id);
2020-06-11 17:28:47 +00:00
ops.remove(op);
if (message != null) {
String title = MessageHelper.formatAddresses(message.to);
PendingIntent pi = getPendingIntent(this);
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("send:" + message.id, 1,
Core.getNotificationError(this, "error", title, ex, pi).build());
} catch (Throwable ex1) {
Log.w(ex1);
}
}
2019-10-03 17:25:08 +00:00
continue;
} else {
if (message != null) {
String title = MessageHelper.formatAddresses(message.to);
PendingIntent pi = getPendingIntent(this);
EntityLog.log(this, title + " last attempt: " + new Date(message.last_attempt));
long now = new Date().getTime();
long delayed = now - message.last_attempt;
if (delayed > IDENTITY_ERROR_AFTER * 60 * 1000L) {
Log.i("Reporting send error after=" + delayed);
try {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("send:" + message.id, 1,
Core.getNotificationError(this, "warning", title, ex, pi).build());
} catch (Throwable ex1) {
Log.w(ex1);
}
}
}
2019-10-03 17:25:08 +00:00
throw ex;
}
2019-10-03 17:25:08 +00:00
} finally {
Log.i(outbox.name + " end op=" + op.id + "/" + op.name);
2020-02-12 16:34:41 +00:00
db.operation().setOperationState(op.id, null);
2019-10-03 17:25:08 +00:00
}
if (!ConnectionHelper.getNetworkState(this).isSuitable())
break;
}
if (db.operation().getOperations(outbox.id).size() == 0)
stopSelf();
} catch (Throwable ex) {
Log.e(outbox.name, ex);
2019-12-06 07:50:46 +00:00
db.folder().setFolderError(outbox.id, Log.formatThrowable(ex));
2019-10-03 17:25:08 +00:00
} finally {
db.folder().setFolderState(outbox.id, null);
db.folder().setFolderSyncState(outbox.id, null);
}
} finally {
wlOutbox.release();
}
}
2020-04-02 19:13:27 +00:00
private void onSync(EntityFolder outbox) {
DB db = DB.getInstance(this);
db.folder().setFolderError(outbox.id, null);
// Restore snooze timers
for (EntityMessage message : db.message().getSnoozed(outbox.id))
EntityMessage.snooze(this, message.id, message.ui_snoozed);
// Retry failed message
for (long id : db.message().getMessageByFolder(outbox.id)) {
int ops = db.operation().getOperationCount(outbox.id, id, EntityOperation.SEND);
if (ops == 0) {
EntityMessage message = db.message().getMessage(id);
2020-04-09 19:36:33 +00:00
if (message != null && message.ui_snoozed == null)
EntityOperation.queue(this, message, EntityOperation.SEND);
}
}
2020-04-02 19:13:27 +00:00
}
2019-07-13 10:48:44 +00:00
private void onSend(EntityMessage message) throws MessagingException, IOException {
DB db = DB.getInstance(this);
// Mark attempt
if (message.last_attempt == null) {
message.last_attempt = new Date().getTime();
db.message().setMessageLastAttempt(message.id, message.last_attempt);
}
2019-04-04 13:54:07 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2019-04-07 05:59:48 +00:00
boolean debug = prefs.getBoolean("debug", false);
2019-04-04 13:54:07 +00:00
2019-06-23 18:56:07 +00:00
if (message.identity == null)
2019-06-25 10:48:04 +00:00
throw new IllegalArgumentException("Send without identity");
2019-06-23 18:56:07 +00:00
EntityIdentity ident = db.identity().getIdentity(message.identity);
2019-06-25 10:48:04 +00:00
if (ident == null)
throw new IllegalArgumentException("Identity not found");
2019-08-25 06:38:04 +00:00
if (!message.content)
throw new IllegalArgumentException("Message body missing");
2020-05-19 05:36:41 +00:00
// Update message ID
if (message.from != null && message.from.length > 0) {
String from = ((InternetAddress) message.from[0]).getAddress();
int at = (from == null ? -1 : from.indexOf('@'));
if (at > 0 && at + 1 < from.length())
message.msgid = EntityMessage.generateMessageId(from.substring(at + 1));
}
// Create message
2019-07-29 14:42:17 +00:00
Properties props = MessageHelper.getSessionProperties();
if (ident.unicode)
props.put("mail.mime.allowutf8", "true");
2019-07-29 09:17:12 +00:00
Session isession = Session.getInstance(props, null);
2020-04-02 09:33:01 +00:00
MimeMessage imessage = MessageHelper.from(this, message, ident, isession, true);
2019-09-01 07:22:25 +00:00
// Prepare sent message
Long sid = null;
EntityFolder sent = db.folder().getFolderByType(message.account, EntityFolder.SENT);
if (sent != null) {
Log.i(sent.name + " Preparing sent message");
long id = message.id;
2019-11-30 12:57:14 +00:00
imessage.saveChanges();
2020-05-23 14:05:29 +00:00
MessageHelper helper = new MessageHelper(imessage, ServiceSend.this);
2019-09-01 07:22:25 +00:00
2019-09-07 10:20:14 +00:00
if (message.uid != null) {
Log.e("Outbox id=" + message.id + " uid=" + message.uid);
message.uid = null;
}
2020-05-23 14:05:29 +00:00
MessageHelper.MessageParts parts = helper.getMessageParts();
2019-11-30 12:57:14 +00:00
String body = parts.getHtml(this);
2020-05-29 05:57:13 +00:00
Boolean plain = parts.isPlainOnly();
if (plain != null && plain)
body = body.replace("<div x-plain=\"true\">", "<div>");
2019-11-30 12:57:14 +00:00
try {
db.beginTransaction();
message.id = null;
message.folder = sent.id;
message.identity = null;
message.from = helper.getFrom();
2020-03-22 10:28:44 +00:00
message.cc = helper.getCc();
2019-11-30 12:57:14 +00:00
message.bcc = helper.getBcc();
message.reply = helper.getReply();
message.encrypt = parts.getEncryption();
message.ui_encrypt = message.encrypt;
2019-11-30 12:57:14 +00:00
message.received = new Date().getTime();
message.seen = true;
message.ui_seen = true;
message.ui_hide = true;
message.error = null;
message.id = db.message().insertMessage(message);
2020-02-20 09:35:01 +00:00
File file = EntityMessage.getFile(this, message.id);
Helper.writeText(file, body);
2019-11-30 12:57:14 +00:00
db.message().setMessageContent(message.id,
true,
2020-03-26 14:25:44 +00:00
HtmlHelper.getLanguage(this, body),
2019-11-30 12:57:14 +00:00
parts.isPlainOnly(),
2020-03-26 14:25:44 +00:00
HtmlHelper.getPreview(body),
2019-11-30 12:57:14 +00:00
parts.getWarnings(message.warning));
EntityAttachment.copy(this, id, message.id);
Long size = null;
if (body != null)
size = (long) body.length();
2020-02-07 11:58:45 +00:00
Long total = size;
2019-11-30 12:57:14 +00:00
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
for (EntityAttachment attachment : attachments)
if (attachment.size != null)
total = (total == null ? 0 : total) + attachment.size;
db.message().setMessageSize(message.id, size, total);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2019-09-01 07:22:25 +00:00
sid = message.id;
message.id = id;
}
// Create transport
2020-01-29 20:06:45 +00:00
try (EmailService iservice = new EmailService(
2020-02-06 12:01:11 +00:00
this, ident.getProtocol(), ident.realm, ident.insecure, debug)) {
iservice.setUseIp(ident.use_ip, ident.ehlo);
iservice.setUnicode(ident.unicode);
2019-07-29 09:17:12 +00:00
// Connect transport
db.identity().setIdentityState(ident.id, "connecting");
2019-07-29 09:17:12 +00:00
iservice.connect(ident);
db.identity().setIdentityState(ident.id, "connected");
Address[] to = imessage.getAllRecipients();
2019-10-03 17:25:08 +00:00
String via = "via " + ident.host + "/" + ident.user +
" to " + TextUtils.join(", ", to);
// Send message
EntityLog.log(this, "Sending " + via);
2020-06-25 21:29:33 +00:00
if (BuildConfig.DEBUG && false)
throw new SendFailedException("Test");
2019-07-28 19:45:29 +00:00
iservice.getTransport().sendMessage(imessage, to);
2019-03-14 18:55:34 +00:00
long time = new Date().getTime();
2019-10-03 17:25:08 +00:00
EntityLog.log(this, "Sent " + via);
try {
db.beginTransaction();
2019-11-30 12:57:14 +00:00
// Delete from outbox
2019-09-01 07:22:25 +00:00
db.message().deleteMessage(message.id);
2019-11-30 12:57:14 +00:00
// Show in sent folder
2019-09-01 07:22:25 +00:00
if (sid != null) {
db.message().setMessageSent(sid, time);
2019-09-27 16:25:55 +00:00
db.message().setMessageUiHide(sid, false);
2019-08-29 19:00:24 +00:00
}
2019-03-05 12:46:25 +00:00
if (message.inreplyto != null) {
2019-11-06 16:38:55 +00:00
List<EntityMessage> replieds = db.message().getMessagesByMsgId(message.account, message.inreplyto);
2019-03-05 12:46:25 +00:00
for (EntityMessage replied : replieds)
2019-05-18 06:49:20 +00:00
EntityOperation.queue(this, replied, EntityOperation.ANSWERED, true);
2019-03-05 12:46:25 +00:00
}
2020-05-06 12:50:30 +00:00
if (message.wasforwardedfrom != null) {
List<EntityMessage> forwardeds = db.message().getMessagesByMsgId(message.account, message.wasforwardedfrom);
for (EntityMessage forwarded : forwardeds)
EntityOperation.queue(this, forwarded, EntityOperation.KEYWORD, "$Forwarded", true);
}
2019-11-30 12:57:14 +00:00
// Check sent message
2019-11-18 09:23:24 +00:00
if (sid != null) {
// Check for sent orphans
EntityMessage orphan = db.message().getMessage(sid);
EntityOperation.queue(this, orphan, EntityOperation.EXISTS);
}
2019-12-07 19:32:58 +00:00
// Reset identity
db.identity().setIdentityConnected(ident.id, new Date().getTime());
db.identity().setIdentityError(ident.id, null);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2019-12-09 18:44:27 +00:00
ServiceSynchronize.eval(ServiceSend.this, "sent");
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.cancel("send:" + message.id, 1);
} catch (MessagingException ex) {
2019-06-23 18:56:07 +00:00
Log.e(ex);
2019-09-01 07:22:25 +00:00
if (sid != null)
db.message().deleteMessage(sid);
2019-12-06 07:50:46 +00:00
db.identity().setIdentityError(ident.id, Log.formatThrowable(ex));
throw ex;
} finally {
db.identity().setIdentityState(ident.id, null);
}
}
2019-02-28 07:11:46 +00:00
2019-02-28 07:55:08 +00:00
static void boot(final Context context) {
2019-08-01 07:08:28 +00:00
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
DB db = DB.getInstance(context);
2020-04-02 19:13:27 +00:00
2019-08-01 07:08:28 +00:00
EntityFolder outbox = db.folder().getOutbox();
2019-12-13 07:08:18 +00:00
if (outbox != null) {
int operations = db.operation().getOperations(outbox.id).size();
if (operations > 0)
start(context);
}
2019-08-01 07:08:28 +00:00
} catch (Throwable ex) {
Log.e(ex);
2019-02-28 07:55:08 +00:00
}
2019-08-01 07:08:28 +00:00
}
}, "send:boot");
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
thread.start();
2019-02-28 07:55:08 +00:00
}
2019-02-28 07:11:46 +00:00
static void start(Context context) {
2019-02-28 09:37:37 +00:00
ContextCompat.startForegroundService(context,
new Intent(context, ServiceSend.class));
2019-02-28 07:11:46 +00:00
}
2019-08-03 14:05:30 +00:00
2020-02-21 16:38:46 +00:00
static void schedule(Context context, long delay) {
Intent intent = new Intent(context, ServiceSend.class);
PendingIntent pi = PendingIntentCompat.getForegroundService(
context, PI_SEND, intent, PendingIntent.FLAG_UPDATE_CURRENT);
long trigger = System.currentTimeMillis() + delay;
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi);
}
2019-08-03 14:05:30 +00:00
static void watchdog(Context context) {
boot(context);
}
}