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

1127 lines
47 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/>.
2024-01-01 07:50:49 +00:00
Copyright 2018-2024 by Marcel Bokhorst (M66B)
*/
2023-03-04 07:57:12 +00:00
import static eu.faircode.email.ServiceAuthenticator.AUTH_TYPE_GRAPH;
2022-11-03 16:40:45 +00:00
import android.Manifest;
2020-02-21 16:38:46 +00:00
import android.app.AlarmManager;
import android.app.Notification;
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;
2023-02-05 09:46:38 +00:00
import android.net.Uri;
import android.os.PowerManager;
2021-10-07 14:27:01 +00:00
import android.os.SystemClock;
import android.text.TextUtils;
2019-10-03 17:25:08 +00:00
import androidx.annotation.NonNull;
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;
2021-01-12 10:59:45 +00:00
import com.sun.mail.smtp.SMTPSendFailedException;
import com.sun.mail.util.TraceOutputStream;
2021-01-12 10:59:45 +00:00
2023-03-04 07:57:12 +00:00
import org.json.JSONException;
2021-12-13 07:36:14 +00:00
import java.io.ByteArrayOutputStream;
2020-02-20 09:35:01 +00:00
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
2021-12-12 18:00:58 +00:00
import java.util.Arrays;
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;
2020-07-28 14:58:57 +00:00
import java.util.Objects;
import java.util.Properties;
2023-01-01 10:45:31 +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;
2022-11-03 16:40:45 +00:00
import biweekly.ICalendar;
import biweekly.component.VEvent;
import biweekly.property.Method;
public class ServiceSend extends ServiceBase implements SharedPreferences.OnSharedPreferenceChangeListener {
2024-03-07 18:58:56 +00:00
private int retry_max = RETRY_MAX_DEFAULT;
2020-03-11 09:09:52 +00:00
private TupleUnsent lastUnsent = null;
2020-07-28 14:58:57 +00:00
private Network lastActive = null;
2019-06-23 17:09:02 +00:00
private boolean lastSuitable = false;
2021-10-07 14:27:01 +00:00
private int lastProgress = -1;
2021-08-31 19:11:12 +00:00
private TwoStateOwner owner;
2019-10-03 17:25:08 +00:00
private PowerManager.WakeLock wlOutbox;
2020-06-26 08:59:32 +00:00
private List<Long> handling = new ArrayList<>();
2020-07-28 14:58:10 +00:00
2023-01-01 10:45:31 +00:00
private static final ExecutorService executor =
2023-01-13 16:38:41 +00:00
Helper.getBackgroundExecutor(1, "send");
2023-01-01 10:45:31 +00:00
2023-09-10 18:21:13 +00:00
private static final long RETRY_WAIT = 5000L; // milliseconds
2020-07-29 12:22:48 +00:00
private static final int CONNECTIVITY_DELAY = 5000; // milliseconds
2021-10-07 14:27:01 +00:00
private static final int PROGRESS_UPDATE_INTERVAL = 1000; // milliseconds
2024-03-07 10:45:10 +00:00
private static final long STOP_DELAY = 2500L;
2021-03-30 07:36:51 +00:00
static final int PI_SEND = 1;
2023-02-05 09:46:38 +00:00
static final int PI_FIX = 2;
2021-03-30 07:36:51 +00:00
2024-03-07 18:58:56 +00:00
static final int RETRY_MAX_DEFAULT = 3;
@Override
public void onCreate() {
2019-08-17 11:06:57 +00:00
EntityLog.log(this, "Service send create");
super.onCreate();
2024-04-16 10:39:35 +00:00
try {
startForeground(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
} catch (Throwable ex) {
if (Helper.isPlayStoreInstall())
Log.i(ex);
else
Log.e(ex);
}
2024-03-07 18:58:56 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
retry_max = prefs.getInt("send_retry_max", RETRY_MAX_DEFAULT);
2021-08-31 19:11:12 +00:00
owner = new TwoStateOwner(this, "send");
2022-04-13 20:27:33 +00:00
PowerManager pm = Helper.getSystemService(this, PowerManager.class);
2019-10-03 17:25:08 +00:00
wlOutbox = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, BuildConfig.APPLICATION_ID + ":send");
2024-03-07 10:45:10 +00:00
final Runnable quit = new RunnableEx("send:quit") {
@Override
protected void delegate() {
stopSelf();
}
};
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-07-29 06:32:15 +00:00
EntityLog.log(ServiceSend.this, "Unsent=" + (unsent == null ? null : unsent.count));
2019-10-04 15:17:42 +00:00
2020-02-22 21:09:46 +00:00
try {
2022-06-09 10:38:17 +00:00
NotificationManager nm =
Helper.getSystemService(ServiceSend.this, NotificationManager.class);
if (NotificationHelper.areNotificationsEnabled(nm))
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
2020-02-22 21:09:46 +00:00
} catch (Throwable ex) {
Log.w(ex);
}
2024-03-07 10:45:10 +00:00
getMainHandler().removeCallbacks(quit);
if (unsent == null || unsent.count == 0)
2024-03-07 10:45:10 +00:00
getMainHandler().postDelayed(quit, STOP_DELAY);
2019-10-04 15:17:42 +00:00
}
}
});
2019-06-23 17:09:02 +00:00
// Observe send operations
2020-10-04 14:57:19 +00:00
db.operation().liveSend().observe(owner, new Observer<List<EntityOperation>>() {
2019-06-23 17:09:02 +00:00
@Override
2020-10-04 14:57:19 +00:00
public void onChanged(List<EntityOperation> operations) {
2020-06-26 08:59:32 +00:00
if (operations == null)
operations = new ArrayList<>();
2020-10-04 14:57:19 +00:00
final List<EntityOperation> process = new ArrayList<>();
2020-06-26 08:59:32 +00:00
2019-06-23 17:09:02 +00:00
List<Long> ops = new ArrayList<>();
2020-10-04 14:57:19 +00:00
for (EntityOperation op : operations) {
2019-06-23 17:09:02 +00:00
if (!handling.contains(op.id))
2020-06-26 08:59:32 +00:00
process.add(op);
2020-06-11 13:56:14 +00:00
ops.add(op.id);
2019-06-23 17:09:02 +00:00
}
handling = ops;
2020-06-26 08:59:32 +00:00
if (process.size() > 0) {
2020-07-28 14:38:31 +00:00
EntityLog.log(ServiceSend.this,
"Send process=" + TextUtils.join(",", process) +
" handling=" + TextUtils.join(",", handling));
2019-06-23 17:09:02 +00:00
2023-01-01 10:45:31 +00:00
executor.submit(new Runnable() {
2019-06-23 17:09:02 +00:00
@Override
public void run() {
2020-06-26 08:59:32 +00:00
processOperations(process);
2019-06-23 17:09:02 +00:00
}
});
}
}
});
2024-03-16 07:52:55 +00:00
ConnectionHelper.NetworkState state = ConnectionHelper.getNetworkState(this);
lastActive = state.getActive();
lastSuitable = state.isSuitable();
2020-06-26 08:59:32 +00:00
if (lastSuitable)
owner.start();
2022-04-13 20:27:33 +00:00
ConnectivityManager cm = Helper.getSystemService(this, ConnectivityManager.class);
2019-06-23 17:09:02 +00:00
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);
2023-09-21 06:45:04 +00:00
ContextCompat.registerReceiver(this,
connectionChangedReceiver,
iif,
ContextCompat.RECEIVER_NOT_EXPORTED);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
2024-04-11 05:50:39 +00:00
@Override
public void onTimeout(int startId) {
Log.e(new Throwable("onTimeout"));
}
@Override
public void onDestroy() {
2019-08-17 11:06:57 +00:00
EntityLog.log(this, "Service send destroy");
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
2019-10-04 15:17:42 +00:00
unregisterReceiver(connectionChangedReceiver);
2022-04-13 20:27:33 +00:00
ConnectivityManager cm = Helper.getSystemService(this, ConnectivityManager.class);
cm.unregisterNetworkCallback(networkCallback);
2020-08-23 15:34:14 +00:00
getMainHandler().removeCallbacks(_checkConnectivity);
2020-07-29 12:22:48 +00:00
2020-06-26 08:59:32 +00:00
owner.stop();
handling.clear();
stopForeground(true);
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(this, NotificationManager.class);
2021-07-15 16:36:39 +00:00
nm.cancel(NotificationHelper.NOTIFICATION_SEND);
super.onDestroy();
2022-05-02 08:15:29 +00:00
CoalMine.watch(this, this.getClass().getName() + "#onDestroy");
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (ConnectionHelper.PREF_NETWORK.contains(key))
checkConnectivity();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
2024-04-16 10:39:35 +00:00
try {
startForeground(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
} catch (Throwable ex) {
if (Helper.isPlayStoreInstall())
Log.i(ex);
else
Log.e(ex);
}
2021-03-30 07:36:51 +00:00
Log.i("Send intent=" + intent);
Log.logExtras(intent);
return START_STICKY;
}
private Notification getNotificationService(boolean alert) {
2019-06-23 08:23:49 +00:00
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this, "send")
2023-11-12 06:52:29 +00:00
.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
2020-11-01 13:21:00 +00:00
.setSmallIcon(R.drawable.baseline_send_white_24)
2019-06-23 08:23:49 +00:00
.setContentTitle(getString(R.string.title_notification_sending))
.setContentIntent(getPendingIntent(this))
2019-06-23 08:23:49 +00:00
.setAutoCancel(false)
.setShowWhen(true)
2022-03-23 09:21:14 +00:00
.setOnlyAlertOnce(!alert)
.setDefaults(0) // disable sound on pre Android 8
2019-06-23 08:23:49 +00:00
.setPriority(NotificationCompat.PRIORITY_MIN)
2019-07-23 09:38:36 +00:00
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
.setLocalOnly(true)
.setOngoing(true);
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));
2021-10-07 14:27:01 +00:00
if (lastProgress >= 0)
builder.setProgress(100, lastProgress, false);
2023-01-28 13:14:01 +00:00
if (!lastSuitable) {
Intent manage = new Intent(this, ActivitySetup.class)
.setAction("connection")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)
.putExtra("tab", "connection");
PendingIntent piManage = PendingIntentCompat.getActivity(
this, ActivitySetup.PI_CONNECTION, manage, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action.Builder actionManage = new NotificationCompat.Action.Builder(
R.drawable.twotone_settings_24,
getString(R.string.title_setup_manage),
piManage);
builder.addAction(actionManage.build());
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
return notification;
}
2020-07-04 19:48:39 +00:00
NotificationCompat.Builder getNotificationError(String recipient, Throwable ex, int tries_left) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, tries_left == 0 ? "error" : "warning")
2020-06-26 11:18:53 +00:00
.setSmallIcon(R.drawable.baseline_warning_white_24)
.setContentTitle(getString(R.string.title_notification_sending_failed, recipient))
.setContentIntent(getPendingIntent(this))
2024-03-13 12:31:55 +00:00
.setAutoCancel(true)
2020-06-26 11:18:53 +00:00
.setShowWhen(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
2020-07-04 19:48:39 +00:00
.setOnlyAlertOnce(false)
2020-06-26 11:18:53 +00:00
.setCategory(NotificationCompat.CATEGORY_ERROR)
2020-07-04 19:48:39 +00:00
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
if (tries_left == 0) {
builder.setContentText(Log.formatThrowable(ex, false))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Log.formatThrowable(ex, "\n", false)));
2021-06-29 08:05:00 +00:00
} else {
String msg = getString(R.string.title_notification_sending_left, tries_left);
builder.setContentText(msg)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg + "\n" + getString(R.string.title_notification_sending_retry)));
}
2020-07-04 19:48:39 +00:00
return builder;
2020-06-26 11:18:53 +00:00
}
private static PendingIntent getPendingIntent(Context context) {
Intent intent = new Intent(context, ActivityView.class);
intent.setAction("outbox");
2022-06-25 12:27:46 +00:00
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2021-03-27 17:54:55 +00:00
return PendingIntentCompat.getActivity(
2021-05-22 14:54:17 +00:00
context, ActivityView.PI_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) {
2024-02-09 17:39:25 +00:00
Log.i("Received " + intent +
2019-10-04 15:17:42 +00:00
" " + TextUtils.join(" ", Log.getExtras(intent.getExtras())));
checkConnectivity();
}
};
2019-10-04 15:17:42 +00:00
private void checkConnectivity() {
2020-08-23 15:34:14 +00:00
getMainHandler().postDelayed(_checkConnectivity, CONNECTIVITY_DELAY);
2020-07-28 14:58:10 +00:00
}
2020-07-29 12:22:48 +00:00
private Runnable _checkConnectivity = new Runnable() {
@Override
public void run() {
Network active = ConnectionHelper.getActiveNetwork(ServiceSend.this);
boolean restart = !Objects.equals(lastActive, active);
if (restart) {
lastActive = active;
EntityLog.log(ServiceSend.this, "Service send active=" + active);
if (lastSuitable) {
EntityLog.log(ServiceSend.this, "Service send restart");
lastSuitable = false;
owner.stop();
handling.clear();
}
}
2019-10-04 15:17:42 +00:00
2020-07-29 12:22:48 +00:00
boolean suitable = ConnectionHelper.getNetworkState(ServiceSend.this).isSuitable();
if (lastSuitable != suitable) {
lastSuitable = suitable;
EntityLog.log(ServiceSend.this, "Service send suitable=" + suitable);
2020-07-29 12:22:48 +00:00
try {
2022-06-09 10:38:17 +00:00
NotificationManager nm =
Helper.getSystemService(ServiceSend.this, NotificationManager.class);
if (NotificationHelper.areNotificationsEnabled(nm))
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
2020-07-29 12:22:48 +00:00
} catch (Throwable ex) {
Log.w(ex);
}
2020-07-29 06:30:51 +00:00
2020-07-29 12:22:48 +00:00
if (suitable)
owner.start();
else {
owner.stop();
handling.clear();
}
}
}
2020-07-29 12:22:48 +00:00
};
2019-10-04 15:17:42 +00:00
2020-10-04 14:57:19 +00:00
private void processOperations(List<EntityOperation> ops) {
2022-10-22 06:19:29 +00:00
long start = new Date().getTime();
2019-10-03 17:25:08 +00:00
try {
2022-10-22 06:19:29 +00:00
wlOutbox.acquire(Helper.WAKELOCK_MAX);
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-07-28 14:38:31 +00:00
EntityLog.log(this, "Send processing operations=" + ops.size());
2020-06-11 17:28:47 +00:00
while (ops.size() > 0) {
2020-06-26 08:59:32 +00:00
if (!ConnectionHelper.getNetworkState(this).isSuitable())
break;
2020-10-04 14:57:19 +00:00
EntityOperation op = ops.get(0);
2023-02-02 11:05:12 +00:00
if (db.operation().getOperation(op.id) == null) {
ops.remove(op);
continue;
}
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 {
2020-07-28 14:38:31 +00:00
EntityLog.log(this, "Send start op=" + op.id + "/" + op.name +
2019-10-03 17:25:08 +00:00
" msg=" + op.message +
2020-06-26 08:59:32 +00:00
" tries=" + op.tries +
2019-10-03 17:25:08 +00:00
" 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));
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);
2020-07-28 14:38:31 +00:00
EntityLog.log(this, "Send " + Log.formatThrowable(ex, false));
2019-10-03 17:25:08 +00:00
2024-03-07 10:58:15 +00:00
boolean unrecoverable = (ex instanceof OutOfMemoryError ||
ex instanceof MessageRemovedException ||
ex instanceof FileNotFoundException ||
(ex instanceof AuthenticationFailedException && !ConnectionHelper.isIoError(ex)) ||
ex instanceof SendFailedException ||
ex instanceof IllegalArgumentException);
2024-03-07 18:58:56 +00:00
int tries_left = (unrecoverable ? 0 : retry_max - op.tries);
2019-12-06 07:50:46 +00:00
db.operation().setOperationError(op.id, Log.formatThrowable(ex));
2020-07-04 19:48:39 +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-07-04 19:48:39 +00:00
try {
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(this, NotificationManager.class);
2023-02-05 09:46:38 +00:00
if (NotificationHelper.areNotificationsEnabled(nm)) {
NotificationCompat.Builder builder = getNotificationError(
MessageHelper.formatAddressesShort(message.to), ex, tries_left);
if (ex instanceof AuthenticationFailedException &&
ex.getMessage() != null &&
ex.getMessage().contains("535 5.7.3 Authentication unsuccessful")) {
EntityIdentity identity = db.identity().getIdentity(message.identity);
if (identity == null) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Helper.SUPPORT_URI))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent piFix = PendingIntentCompat.getActivity(
this, PI_FIX, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(piFix);
} else {
2023-03-06 14:58:30 +00:00
EntityAccount account = db.account().getAccount(identity.account);
int protocol = (account == null ? -1 : account.protocol);
Intent intent = new Intent(this, ActivityError.class);
intent.setAction("535:" + identity.id);
2023-12-12 19:55:55 +00:00
intent.putExtra("title", new ThrowableWrapper(ex).getSafeMessage());
intent.putExtra("message", Log.formatThrowable(ex, "\n", false));
intent.putExtra("provider", "outlookgraph");
intent.putExtra("account", identity.account);
2023-03-06 14:58:30 +00:00
intent.putExtra("protocol", protocol);
2023-08-06 14:05:50 +00:00
intent.putExtra("auth_type", AUTH_TYPE_GRAPH);
intent.putExtra("identity", identity.id);
intent.putExtra("personal", identity.name);
intent.putExtra("address", identity.user);
intent.putExtra("faq", 14);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntentCompat.getActivity(
this, ActivityError.PI_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
}
2023-02-05 09:46:38 +00:00
}
2022-06-09 10:38:17 +00:00
nm.notify("send:" + message.id,
NotificationHelper.NOTIFICATION_TAGGED,
2023-02-05 09:46:38 +00:00
builder.build());
}
2020-07-04 19:48:39 +00:00
} catch (Throwable ex1) {
Log.w(ex1);
}
}
2024-03-07 14:02:27 +00:00
if (tries_left <= 0) {
Log.w("Send tries left=" + tries_left +
" unrecoverable=" + unrecoverable);
2019-10-03 17:25:08 +00:00
db.operation().deleteOperation(op.id);
2020-06-11 17:28:47 +00:00
ops.remove(op);
2023-09-10 18:21:13 +00:00
} else {
2024-03-07 14:02:27 +00:00
Log.i("Send retry wait");
2023-09-10 18:21:13 +00:00
Thread.sleep(RETRY_WAIT);
2019-10-03 17:25:08 +00:00
throw ex;
2023-09-10 18:21:13 +00:00
}
2019-10-03 17:25:08 +00:00
} finally {
2020-07-28 14:38:31 +00:00
EntityLog.log(this, "Send 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
}
}
} 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 {
2022-10-22 06:19:29 +00:00
if (wlOutbox.isHeld())
wlOutbox.release();
else
Log.i("send release elapse=" + (new Date().getTime() - start));
2019-10-03 17:25:08 +00:00
}
}
2020-04-02 19:13:27 +00:00
private void onSync(EntityFolder outbox) {
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(this, NotificationManager.class);
2020-04-02 19:13:27 +00:00
2020-09-17 14:33:52 +00:00
DB db = DB.getInstance(this);
2020-06-26 08:59:32 +00:00
try {
db.beginTransaction();
db.folder().setFolderError(outbox.id, null);
2020-04-02 19:13:27 +00:00
2020-09-17 14:33:52 +00:00
// Requeue non executing operations
2020-06-26 08:59:32 +00:00
for (long id : db.message().getMessageByFolder(outbox.id)) {
EntityMessage message = db.message().getMessage(id);
2024-03-13 12:31:55 +00:00
if (message == null || message.warning != null)
2020-09-17 14:33:52 +00:00
continue;
EntityOperation op = db.operation().getOperation(message.id, EntityOperation.SEND);
if (op != null) {
if ("executing".equals(op.state))
continue;
db.operation().deleteOperation(op.id);
}
2020-09-17 14:33:52 +00:00
2023-10-17 13:40:09 +00:00
EntityLog.log(this, "Send restore id=" + message.id + " error=" + message.error);
2023-02-16 06:51:05 +00:00
2020-09-17 14:33:52 +00:00
db.message().setMessageError(message.id, null);
2021-07-15 16:36:39 +00:00
nm.cancel("send:" + message.id, NotificationHelper.NOTIFICATION_TAGGED);
2020-09-17 14:33:52 +00:00
if (message.ui_snoozed == null)
EntityOperation.queue(this, message, EntityOperation.SEND);
else
EntityMessage.snooze(this, message.id, message.ui_snoozed);
}
2020-06-26 08:59:32 +00:00
db.setTransactionSuccessful();
} catch (IllegalArgumentException ex) {
Log.w(ex);
2020-06-26 08:59:32 +00:00
} finally {
db.endTransaction();
}
2021-01-12 09:15:45 +00:00
ServiceSend.start(this);
2020-04-02 19:13:27 +00:00
}
2023-03-04 07:57:12 +00:00
private void onSend(EntityMessage message) throws JSONException, MessagingException, IOException {
DB db = DB.getInstance(this);
2021-06-17 05:59:48 +00:00
// Check if cancelled by user or by errors
2021-03-22 07:04:52 +00:00
EntityOperation operation = db.operation().getOperation(message.id, EntityOperation.SEND);
if (operation == null)
2021-06-17 05:59:48 +00:00
return;
2021-03-22 07:04:52 +00:00
// Mark attempt
if (message.last_attempt == null) {
message.last_attempt = new Date().getTime();
db.message().setMessageLastAttempt(message.id, message.last_attempt);
}
2022-04-13 20:27:33 +00:00
NotificationManager nm = Helper.getSystemService(this, NotificationManager.class);
2022-06-09 10:38:17 +00:00
if (NotificationHelper.areNotificationsEnabled(nm))
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(true));
2021-10-07 14:27:01 +00:00
2019-04-04 13:54:07 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
2024-03-13 12:31:55 +00:00
boolean send_partial = prefs.getBoolean("send_partial", false);
boolean reply_move = prefs.getBoolean("reply_move", false);
2022-08-26 16:19:14 +00:00
boolean reply_move_inbox = prefs.getBoolean("reply_move_inbox", true);
2021-12-13 07:36:14 +00:00
boolean protocol = prefs.getBoolean("protocol", false);
2021-11-10 07:26:08 +00:00
boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.DEBUG);
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");
2022-07-21 19:52:43 +00:00
if (!message.content)
throw new IllegalArgumentException("Message body missing");
EntityAccount account = db.account().getAccount(message.account);
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");
if (!ident.synchronize)
throw new IllegalArgumentException("Identity is disabled");
2019-06-25 10:48:04 +00:00
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));
}
2021-02-20 13:29:37 +00:00
// Set sent time
message.sent = new Date().getTime();
db.message().setMessageSent(message.id, message.sent);
// Create message
2022-08-12 20:09:50 +00:00
Properties props = MessageHelper.getSessionProperties(ident.unicode);
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 = null;
if (reply_move && !TextUtils.isEmpty(message.inreplyto)) {
List<EntityMessage> replied = db.message().getMessagesByMsgId(message.account, message.inreplyto);
if (replied != null)
for (EntityMessage m : replied)
if (!m.ui_hide) {
EntityFolder folder = db.folder().getFolder(m.folder);
if (folder != null &&
2022-08-26 16:19:14 +00:00
((EntityFolder.INBOX.equals(folder.type) && reply_move_inbox) ||
EntityFolder.ARCHIVE.equals(folder.type) ||
EntityFolder.USER.equals(folder.type))) {
sent = folder;
break;
}
}
}
if (sent == null)
sent = db.folder().getFolderByType(message.account, EntityFolder.SENT);
2019-09-01 07:22:25 +00:00
if (sent != null) {
2021-04-04 07:52:32 +00:00
EntityLog.log(this, sent.name + " Preparing sent message" +
2021-04-04 05:18:04 +00:00
" folder=" + sent.name + "/" + sent.type);
2019-09-01 07:22:25 +00:00
long id = message.id;
2019-11-30 12:57:14 +00:00
imessage.saveChanges();
2021-04-04 07:52:32 +00:00
MessageHelper helper = new MessageHelper(imessage, 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);
2022-02-13 10:36:38 +00:00
Integer plain = parts.isPlainOnly();
if (plain != null && (plain & 1) != 0)
body = body.replace("<div x-plain=\"true\">", "<div>");
2019-11-30 12:57:14 +00:00
2021-01-19 10:57:59 +00:00
String text = HtmlHelper.getFullText(body);
2021-01-25 13:49:56 +00:00
String language = HtmlHelper.getLanguage(this, message.subject, text);
2021-01-19 11:18:03 +00:00
String preview = HtmlHelper.getPreview(text);
2020-11-16 18:06:55 +00:00
2019-11-30 12:57:14 +00:00
try {
db.beginTransaction();
message.id = null;
message.folder = sent.id;
2022-07-21 19:52:43 +00:00
if (account != null && account.protocol == EntityAccount.TYPE_IMAP)
message.identity = null;
2019-11-30 12:57:14 +00:00
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();
2020-11-18 12:23:43 +00:00
message.subject = helper.getSubject(); // Subject encryption
message.encrypt = parts.getEncryption();
message.ui_encrypt = message.encrypt;
2022-04-01 09:13:40 +00:00
message.received = message.sent; // now
2019-11-30 12:57:14 +00:00
message.seen = true;
message.ui_seen = true;
message.ui_hide = true;
2021-05-10 15:51:54 +00:00
message.ui_busy = Long.MAX_VALUE; // Needed to keep messages in user folders
message.raw = null;
2019-11-30 12:57:14 +00:00
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-11-16 18:06:55 +00:00
language,
2019-11-30 12:57:14 +00:00
parts.isPlainOnly(),
2020-11-16 18:06:55 +00:00
preview,
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
2024-03-13 12:31:55 +00:00
long start = 0;
long end = 0;
Long max_size = null;
2024-03-13 12:31:55 +00:00
SMTPSendFailedException partial = null;
2023-03-04 07:57:12 +00:00
if (ident.auth_type == AUTH_TYPE_GRAPH) {
2023-06-12 12:18:11 +00:00
start = new Date().getTime();
MicrosoftGraph.send(ServiceSend.this, ident, imessage);
end = new Date().getTime();
2023-03-04 07:57:12 +00:00
} else {
EmailService iservice = new EmailService(this, ident, EmailService.PURPOSE_USE, debug);
2023-03-04 07:57:12 +00:00
try {
2024-04-24 12:20:03 +00:00
if (ident.envelopeFrom != null)
iservice.setMailFrom(ident.envelopeFrom);
2024-03-13 12:31:55 +00:00
if (send_partial)
iservice.setSendPartial(true);
2023-03-04 07:57:12 +00:00
iservice.setUseIp(ident.use_ip, ident.ehlo);
if (!message.isSigned() && !message.isEncrypted())
iservice.set8BitMime(ident.octetmime);
// 0=Read receipt
// 1=Delivery receipt
// 2=Read+delivery receipt
if (message.receipt_request != null && message.receipt_request) {
int receipt_type = prefs.getInt("receipt_type", 2);
if (ident.receipt_type != null)
receipt_type = ident.receipt_type;
2023-03-04 07:57:12 +00:00
if (receipt_type == 1 || receipt_type == 2) // Delivery receipt
iservice.setDsnNotify("SUCCESS,FAILURE,DELAY");
}
2021-12-12 18:00:58 +00:00
2023-03-04 07:57:12 +00:00
// Connect transport
db.identity().setIdentityState(ident.id, "connecting");
iservice.connect(ident);
if (BuildConfig.DEBUG && false)
throw new IOException("Test");
db.identity().setIdentityState(ident.id, "connected");
2023-12-14 16:06:40 +00:00
EntityLog.log(this, EntityLog.Type.Protocol, ident.email + " " +
TextUtils.join(" ", iservice.getCapabilities()));
2023-03-30 19:51:18 +00:00
max_size = iservice.getMaxSize();
2023-03-04 07:57:12 +00:00
List<Address> recipients = new ArrayList<>();
if (message.headers == null || !Boolean.TRUE.equals(message.resend)) {
Address[] all = imessage.getAllRecipients();
if (all != null)
recipients.addAll(Arrays.asList(all));
} else {
String to = imessage.getHeader("Resent-To", ",");
if (to != null)
for (Address a : InternetAddress.parse(to))
recipients.add(a);
String cc = imessage.getHeader("Resent-Cc", ",");
if (cc != null)
for (Address a : InternetAddress.parse(cc))
recipients.add(a);
String bcc = imessage.getHeader("Resent-Bcc", ",");
if (bcc != null)
for (Address a : InternetAddress.parse(bcc))
recipients.add(a);
}
2021-12-13 07:36:14 +00:00
2024-03-13 12:31:55 +00:00
if (BuildConfig.DEBUG && false) {
InternetAddress invalid = new InternetAddress();
invalid.setAddress("invalid");
recipients.add(invalid);
}
2023-03-04 07:57:12 +00:00
if (protocol && BuildConfig.DEBUG) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
imessage.writeTo(bos);
for (String line : bos.toString().split("\n"))
EntityLog.log(this, line);
}
2024-02-06 10:21:13 +00:00
Address[] rcptto = MessageHelper.removeGroups(recipients.toArray(new Address[0]));
2023-03-04 07:57:12 +00:00
String via = "via " + ident.host + "/" + ident.user +
2024-02-06 10:21:13 +00:00
" rcptto=" + TextUtils.join(", ", recipients);
2023-03-04 07:57:12 +00:00
iservice.setReporter(new TraceOutputStream.IReport() {
private int progress = -1;
private long last = SystemClock.elapsedRealtime();
@Override
public void report(int pos, int total) {
int p = (total == 0 ? 0 : 100 * pos / total);
if (p > progress) {
progress = p;
long now = SystemClock.elapsedRealtime();
if (now > last + PROGRESS_UPDATE_INTERVAL) {
last = now;
lastProgress = progress;
if (NotificationHelper.areNotificationsEnabled(nm))
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
}
2021-10-07 14:27:01 +00:00
}
}
2023-03-04 07:57:12 +00:00
});
// Send message
EntityLog.log(this, "Sending " + via);
start = new Date().getTime();
2024-03-13 12:31:55 +00:00
try {
iservice.getTransport().sendMessage(imessage, rcptto);
} finally {
end = new Date().getTime();
}
2023-03-04 07:57:12 +00:00
EntityLog.log(this, "Sent " + via + " elapse=" + (end - start) + " ms");
} catch (MessagingException ex) {
2024-03-13 12:31:55 +00:00
2023-03-04 07:57:12 +00:00
iservice.dump(ident.email);
Log.e(ex);
if (ex instanceof SMTPSendFailedException) {
SMTPSendFailedException sem = (SMTPSendFailedException) ex;
2024-03-13 12:31:55 +00:00
if (send_partial &&
sem.getInvalidAddresses() != null &&
sem.getValidSentAddresses() != null &&
sem.getValidUnsentAddresses() != null &&
sem.getValidSentAddresses().length > 0 &&
sem.getInvalidAddresses().length + sem.getValidUnsentAddresses().length > 0) {
partial = sem;
}
2023-03-04 07:57:12 +00:00
ex = new SMTPSendFailedException(
sem.getCommand(),
sem.getReturnCode(),
getString(R.string.title_service_auth, sem.getMessage()),
sem.getNextException(),
sem.getValidSentAddresses(),
sem.getValidUnsentAddresses(),
sem.getInvalidAddresses());
2021-10-07 14:27:01 +00:00
}
2021-01-12 10:59:45 +00:00
2024-03-13 12:31:55 +00:00
if (sid != null && partial == null)
2023-03-04 07:57:12 +00:00
db.message().deleteMessage(sid);
2023-03-04 07:57:12 +00:00
db.identity().setIdentityError(ident.id, Log.formatThrowable(ex));
2019-09-01 07:22:25 +00:00
2024-03-13 12:31:55 +00:00
if (partial == null)
throw ex;
2023-03-04 07:57:12 +00:00
} catch (Throwable ex) {
iservice.dump(ident.email);
throw ex;
} finally {
iservice.close();
if (lastProgress >= 0) {
lastProgress = -1;
if (NotificationHelper.areNotificationsEnabled(nm))
nm.notify(NotificationHelper.NOTIFICATION_SEND, getNotificationService(false));
}
db.identity().setIdentityState(ident.id, null);
2021-10-07 14:27:01 +00:00
}
}
try {
db.beginTransaction();
2019-03-05 12:46:25 +00:00
// Delete from outbox
2024-03-13 12:31:55 +00:00
if (partial == null)
db.message().deleteMessage(message.id);
else {
Throwable ex = new Throwable(getString(R.string.title_advanced_sent_partially), partial);
db.message().setMessageWarning(message.id, Log.formatThrowable(ex));
if (NotificationHelper.areNotificationsEnabled(nm)) {
NotificationCompat.Builder builder = getNotificationError(
MessageHelper.formatAddressesShort(message.to),
ex, 0);
nm.notify("partial:" + message.id,
NotificationHelper.NOTIFICATION_TAGGED,
builder.build());
}
}
2020-05-06 12:50:30 +00:00
// Show in sent folder
if (sid != null) {
2021-12-19 11:57:18 +00:00
if (EntityMessage.PGP_SIGNENCRYPT.equals(message.ui_encrypt) ||
EntityMessage.SMIME_SIGNENCRYPT.equals(message.ui_encrypt))
db.attachment().deleteAttachments(sid,
new int[]{EntityAttachment.PGP_MESSAGE, EntityAttachment.SMIME_MESSAGE});
2024-03-13 12:31:55 +00:00
if (partial != null) {
List<Address> unsent = new ArrayList<>();
if (partial.getInvalidAddresses() != null)
unsent.addAll(Arrays.asList(partial.getInvalidAddresses()));
if (partial.getValidUnsentAddresses() != null)
unsent.addAll(Arrays.asList(partial.getValidUnsentAddresses()));
db.message().setMessageTo(sid,
DB.Converters.encodeAddresses(MessageHelper.removeAddresses(message.to, unsent)));
db.message().setMessageCc(sid,
DB.Converters.encodeAddresses(MessageHelper.removeAddresses(message.cc, unsent)));
db.message().setMessageBcc(sid,
DB.Converters.encodeAddresses(MessageHelper.removeAddresses(message.bcc, unsent)));
}
db.message().setMessageReceived(sid, start);
db.message().setMessageSent(sid, end);
db.message().setMessageUiHide(sid, false);
}
2019-12-07 19:32:58 +00:00
// Mark replied
if (message.inreplyto != null) {
List<EntityMessage> replieds = db.message().getMessagesByMsgId(message.account, message.inreplyto);
for (EntityMessage replied : replieds)
EntityOperation.queue(this, replied, EntityOperation.ANSWERED, true);
}
// Mark forwarded
if (message.wasforwardedfrom != null) {
List<EntityMessage> forwardeds = db.message().getMessagesByMsgId(message.account, message.wasforwardedfrom);
for (EntityMessage forwarded : forwardeds)
EntityOperation.queue(this, forwarded,
EntityOperation.KEYWORD, MessageHelper.FLAG_FORWARDED, true);
}
// Update identity
2023-03-30 19:51:18 +00:00
db.identity().setIdentityMaxSize(ident.id, max_size);
db.identity().setIdentityConnected(ident.id, new Date().getTime());
db.identity().setIdentityError(ident.id, null);
2020-08-09 17:43:51 +00:00
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2020-08-09 17:43:51 +00:00
2021-07-15 16:36:39 +00:00
nm.cancel("send:" + message.id, NotificationHelper.NOTIFICATION_TAGGED);
2020-08-09 17:43:51 +00:00
2022-03-23 08:24:54 +00:00
// Play sent sound
String sound = prefs.getString("sound_sent", null);
if (!TextUtils.isEmpty(sound))
MediaPlayerHelper.queue(ServiceSend.this, sound);
// Check sent message
if (sid != null) {
try {
db.beginTransaction();
2019-09-01 07:22:25 +00:00
// Message could have been deleted
EntityMessage orphan = db.message().getMessage(sid);
2022-11-03 18:05:51 +00:00
if (orphan != null)
2023-01-25 11:24:46 +00:00
if (account == null || account.protocol == EntityAccount.TYPE_IMAP)
EntityOperation.queue(this, orphan, EntityOperation.EXISTS);
else if (sent != null)
EntityContact.received(this, account, sent, message);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2022-11-03 16:40:45 +00:00
checkICalendar(sid);
}
2021-05-09 11:39:33 +00:00
ServiceSynchronize.eval(this, "sent");
}
2019-02-28 07:11:46 +00:00
2022-11-03 16:40:45 +00:00
private void checkICalendar(long sid) {
boolean permission = Helper.hasPermission(this, Manifest.permission.WRITE_CALENDAR);
if (!permission)
return;
DB db = DB.getInstance(this);
List<EntityAttachment> attachments = db.attachment().getAttachments(sid);
if (attachments == null || attachments.size() == 0)
return;
for (EntityAttachment attachment : attachments)
if ("text/calendar".equals(attachment.type))
try {
File ics = attachment.getFile(this);
2023-10-27 15:26:58 +00:00
ICalendar icalendar = CalendarHelper.parse(ServiceSend.this, ics);
2022-11-03 16:40:45 +00:00
Method method = icalendar.getMethod();
if (method == null || !method.isReply())
return;
2023-10-26 10:42:12 +00:00
VEvent event = icalendar.getEvents().get(0);
EntityMessage message = db.message().getMessage(sid);
CalendarHelper.update(this, event, message);
2023-05-05 05:31:51 +00:00
2022-11-03 16:40:45 +00:00
break;
} catch (Throwable ex) {
Log.e(ex);
}
}
2019-02-28 07:55:08 +00:00
static void boot(final Context context) {
2022-12-13 09:52:39 +00:00
Helper.getSerialExecutor().submit(new Runnable() {
2019-08-01 07:08:28 +00:00
@Override
public void run() {
try {
2020-12-31 11:29:58 +00:00
EntityLog.log(context, "Boot send service");
2019-08-01 07:08:28 +00:00
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) {
2020-10-04 14:57:19 +00:00
int operations = db.operation().getOperations(EntityOperation.SEND).size();
2019-12-13 07:08:18 +00:00
if (operations > 0)
start(context);
2020-09-26 16:14:47 +00:00
else {
db.folder().setFolderState(outbox.id, null);
db.folder().setFolderSyncState(outbox.id, null);
}
2019-12-13 07:08:18 +00:00
}
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
}
2020-12-31 11:29:58 +00:00
});
2019-02-28 07:55:08 +00:00
}
2019-02-28 07:11:46 +00:00
static void start(Context context) {
2021-03-24 17:55:18 +00:00
try {
ContextCompat.startForegroundService(context, new Intent(context, ServiceSend.class));
} catch (Throwable ex) {
Log.e(ex);
}
2019-02-28 07:11:46 +00:00
}
2019-08-03 14:05:30 +00:00
static void stop(Context context) {
context.stopService(new Intent(context, ServiceSend.class));
}
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;
2022-04-13 20:27:33 +00:00
AlarmManager am = Helper.getSystemService(context, AlarmManager.class);
2020-02-21 16:38:46 +00:00
am.cancel(pi);
2021-04-27 05:53:09 +00:00
AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, trigger, pi);
2020-02-21 16:38:46 +00:00
}
2019-08-03 14:05:30 +00:00
static void watchdog(Context context) {
boot(context);
}
}