2019-02-27 15:05:15 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
import android.app.Notification;
|
2019-03-07 12:29:03 +00:00
|
|
|
import android.app.NotificationChannel;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2019-02-27 17:33:46 +00:00
|
|
|
import android.graphics.Color;
|
|
|
|
import android.media.RingtoneManager;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.net.Uri;
|
2019-02-27 17:33:46 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import com.sun.mail.iap.ConnectionException;
|
|
|
|
import com.sun.mail.iap.Response;
|
2019-03-14 11:08:19 +00:00
|
|
|
import com.sun.mail.imap.AppendUID;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
|
import com.sun.mail.imap.IMAPStore;
|
|
|
|
import com.sun.mail.imap.protocol.FetchResponse;
|
|
|
|
import com.sun.mail.imap.protocol.IMAPProtocol;
|
|
|
|
import com.sun.mail.imap.protocol.UID;
|
|
|
|
import com.sun.mail.util.FolderClosedIOException;
|
|
|
|
import com.sun.mail.util.MailConnectException;
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
2019-02-27 17:33:46 +00:00
|
|
|
import org.jsoup.Jsoup;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.SocketException;
|
|
|
|
import java.net.SocketTimeoutException;
|
|
|
|
import java.net.UnknownHostException;
|
2019-02-27 17:33:46 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Collections;
|
2019-03-30 10:57:29 +00:00
|
|
|
import java.util.Comparator;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.Date;
|
2019-02-27 17:33:46 +00:00
|
|
|
import java.util.HashMap;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.List;
|
2019-02-27 17:33:46 +00:00
|
|
|
import java.util.Map;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
import javax.mail.AuthenticationFailedException;
|
|
|
|
import javax.mail.FetchProfile;
|
|
|
|
import javax.mail.Flags;
|
|
|
|
import javax.mail.Folder;
|
|
|
|
import javax.mail.FolderClosedException;
|
|
|
|
import javax.mail.FolderNotFoundException;
|
|
|
|
import javax.mail.Message;
|
|
|
|
import javax.mail.MessageRemovedException;
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.SendFailedException;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Store;
|
|
|
|
import javax.mail.StoreClosedException;
|
|
|
|
import javax.mail.UIDFolder;
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
import javax.mail.search.ComparisonTerm;
|
|
|
|
import javax.mail.search.FlagTerm;
|
|
|
|
import javax.mail.search.MessageIDTerm;
|
|
|
|
import javax.mail.search.OrTerm;
|
|
|
|
import javax.mail.search.ReceivedDateTerm;
|
|
|
|
import javax.mail.search.SearchTerm;
|
|
|
|
import javax.net.ssl.SSLException;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
2019-03-15 13:54:25 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-03-27 09:48:20 +00:00
|
|
|
import me.leolin.shortcutbadger.ShortcutBadger;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
|
|
|
|
|
|
|
|
class Core {
|
|
|
|
private static final int SYNC_BATCH_SIZE = 20;
|
|
|
|
private static final int DOWNLOAD_BATCH_SIZE = 20;
|
|
|
|
private static final long YIELD_DURATION = 200L; // milliseconds
|
|
|
|
|
2019-03-02 15:03:09 +00:00
|
|
|
static void init(Context context) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
for (String key : prefs.getAll().keySet())
|
|
|
|
if (key.startsWith("notifying:"))
|
|
|
|
editor.remove(key);
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
static void processOperations(
|
|
|
|
Context context,
|
|
|
|
EntityAccount account, EntityFolder folder,
|
|
|
|
Session isession, Store istore, Folder ifolder,
|
|
|
|
State state)
|
|
|
|
throws MessagingException, JSONException, IOException {
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " start process");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
List<EntityOperation> ops = db.operation().getOperations(folder.id);
|
|
|
|
Log.i(folder.name + " pending operations=" + ops.size());
|
2019-04-17 06:40:52 +00:00
|
|
|
for (int i = 0; i < ops.size() && state.running() && state.recoverable(); i++) {
|
2019-02-27 15:05:15 +00:00
|
|
|
EntityOperation op = ops.get(i);
|
|
|
|
try {
|
|
|
|
Log.i(folder.name +
|
|
|
|
" start op=" + op.id + "/" + op.name +
|
|
|
|
" msg=" + op.message +
|
|
|
|
" args=" + op.args);
|
|
|
|
|
|
|
|
// Fetch most recent copy of message
|
|
|
|
EntityMessage message = null;
|
|
|
|
if (op.message != null)
|
|
|
|
message = db.message().getMessage(op.message);
|
|
|
|
|
|
|
|
JSONArray jargs = new JSONArray(op.args);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (message == null && !EntityOperation.SYNC.equals(op.name))
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
db.operation().setOperationError(op.id, null);
|
|
|
|
if (message != null)
|
|
|
|
db.message().setMessageError(message.id, null);
|
|
|
|
|
|
|
|
if (message != null && message.uid == null &&
|
|
|
|
!(EntityOperation.ADD.equals(op.name) ||
|
|
|
|
EntityOperation.DELETE.equals(op.name) ||
|
|
|
|
EntityOperation.SEND.equals(op.name) ||
|
2019-03-05 12:46:25 +00:00
|
|
|
EntityOperation.SYNC.equals(op.name) ||
|
|
|
|
EntityOperation.WAIT.equals(op.name)))
|
2019-02-27 15:05:15 +00:00
|
|
|
throw new IllegalArgumentException(op.name + " without uid " + op.args);
|
|
|
|
|
|
|
|
// Operations should use database transaction when needed
|
|
|
|
|
|
|
|
switch (op.name) {
|
|
|
|
case EntityOperation.SEEN:
|
2019-02-27 17:02:30 +00:00
|
|
|
onSeen(context, jargs, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.FLAG:
|
2019-02-27 17:02:30 +00:00
|
|
|
onFlag(context, jargs, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.ANSWERED:
|
2019-02-27 17:02:30 +00:00
|
|
|
onAnswered(context, jargs, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.KEYWORD:
|
2019-02-27 17:02:30 +00:00
|
|
|
onKeyword(context, jargs, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.ADD:
|
2019-04-04 13:21:55 +00:00
|
|
|
boolean squash = false;
|
|
|
|
for (int j = i + 1; j < ops.size(); j++) {
|
|
|
|
EntityOperation next = ops.get(j);
|
|
|
|
if (next.message != null && next.message.equals(op.message) &&
|
|
|
|
(EntityOperation.ADD.equals(next.name) || EntityOperation.DELETE.equals(next.name))) {
|
|
|
|
squash = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (squash)
|
|
|
|
Log.i(folder.name +
|
|
|
|
" squashing op=" + op.id + "/" + op.name +
|
|
|
|
" msg=" + op.message +
|
|
|
|
" args=" + op.args);
|
|
|
|
else
|
|
|
|
onAdd(context, jargs, folder, message, isession, (IMAPStore) istore, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.MOVE:
|
2019-03-02 14:32:19 +00:00
|
|
|
onMove(context, jargs, false, folder, message, isession, (IMAPStore) istore, (IMAPFolder) ifolder);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.COPY:
|
|
|
|
onMove(context, jargs, true, folder, message, isession, (IMAPStore) istore, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.DELETE:
|
2019-02-27 17:02:30 +00:00
|
|
|
onDelete(context, jargs, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.HEADERS:
|
2019-02-27 17:02:30 +00:00
|
|
|
onHeaders(context, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.RAW:
|
2019-02-27 17:02:30 +00:00
|
|
|
onRaw(context, jargs, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.BODY:
|
2019-02-27 17:02:30 +00:00
|
|
|
onBody(context, folder, message, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.ATTACHMENT:
|
2019-02-27 17:02:30 +00:00
|
|
|
onAttachment(context, jargs, folder, message, op, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.SYNC:
|
2019-02-27 19:04:26 +00:00
|
|
|
onSynchronizeMessages(context, jargs, account, folder, (IMAPFolder) ifolder, state);
|
2019-02-27 15:05:15 +00:00
|
|
|
break;
|
|
|
|
|
2019-03-05 12:46:25 +00:00
|
|
|
case EntityOperation.WAIT:
|
2019-03-08 15:26:12 +00:00
|
|
|
break;
|
2019-03-05 12:46:25 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown operation=" + op.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Operation succeeded
|
|
|
|
db.operation().deleteOperation(op.id);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
reportError(context, account, folder, ex);
|
|
|
|
|
2019-04-11 10:10:47 +00:00
|
|
|
db.operation().setOperationError(op.id, Helper.formatThrowable(ex));
|
|
|
|
if (message != null)
|
|
|
|
db.message().setMessageError(message.id, Helper.formatThrowable(ex, true));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
if (ex instanceof MessageRemovedException ||
|
|
|
|
ex instanceof FolderNotFoundException ||
|
|
|
|
ex instanceof IllegalArgumentException) {
|
2019-03-02 17:46:49 +00:00
|
|
|
Log.w("Unrecoverable");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// There is no use in repeating
|
|
|
|
db.operation().deleteOperation(op.id);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
if (message != null) {
|
|
|
|
if (ex instanceof MessageRemovedException)
|
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
|
|
|
|
Long newid = null;
|
|
|
|
|
|
|
|
if (EntityOperation.MOVE.equals(op.name) &&
|
2019-03-10 07:15:53 +00:00
|
|
|
jargs.length() > 2 && !jargs.isNull(2))
|
2019-02-27 15:05:15 +00:00
|
|
|
newid = jargs.getLong(2);
|
|
|
|
|
|
|
|
if ((EntityOperation.ADD.equals(op.name) ||
|
|
|
|
EntityOperation.RAW.equals(op.name)) &&
|
|
|
|
jargs.length() > 0 && !jargs.isNull(0))
|
|
|
|
newid = jargs.getLong(0);
|
|
|
|
|
|
|
|
// Delete temporary copy in target folder
|
|
|
|
if (newid != null) {
|
|
|
|
db.message().deleteMessage(newid);
|
|
|
|
db.message().setMessageUiHide(message.id, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end op=" + op.id + "/" + op.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end process state=" + state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onSeen(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Mark message (un)seen
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.SEEN)) {
|
|
|
|
db.message().setMessageSeen(message.id, false);
|
|
|
|
db.message().setMessageUiSeen(message.id, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean seen = jargs.getBoolean(0);
|
|
|
|
if (message.seen.equals(seen))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, seen);
|
|
|
|
|
|
|
|
db.message().setMessageSeen(message.id, seen);
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onFlag(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Star/unstar message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED)) {
|
|
|
|
db.message().setMessageFlagged(message.id, false);
|
|
|
|
db.message().setMessageUiFlagged(message.id, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean flagged = jargs.getBoolean(0);
|
|
|
|
if (message.flagged.equals(flagged))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
imessage.setFlag(Flags.Flag.FLAGGED, flagged);
|
|
|
|
|
|
|
|
db.message().setMessageFlagged(message.id, flagged);
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onAnswered(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Mark message (un)answered
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.ANSWERED)) {
|
|
|
|
db.message().setMessageAnswered(message.id, false);
|
|
|
|
db.message().setMessageUiAnswered(message.id, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean answered = jargs.getBoolean(0);
|
|
|
|
if (message.answered.equals(answered))
|
|
|
|
return;
|
|
|
|
|
2019-03-08 15:26:12 +00:00
|
|
|
// This will be fixed when synchronizing the message
|
|
|
|
if (message.uid == null)
|
|
|
|
return;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
imessage.setFlag(Flags.Flag.ANSWERED, answered);
|
|
|
|
|
|
|
|
db.message().setMessageAnswered(message.id, answered);
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onKeyword(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Set/reset user flag
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
|
|
|
|
db.message().setMessageKeywords(message.id, DB.Converters.fromStringArray(null));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc3501#section-2.3.2
|
|
|
|
String keyword = jargs.getString(0);
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
Flags flags = new Flags(keyword);
|
|
|
|
imessage.setFlags(flags, set);
|
|
|
|
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
message = db.message().getMessage(message.id);
|
|
|
|
|
|
|
|
List<String> keywords = new ArrayList<>(Arrays.asList(message.keywords));
|
|
|
|
if (set) {
|
|
|
|
if (!keywords.contains(keyword))
|
|
|
|
keywords.add(keyword);
|
|
|
|
} else
|
|
|
|
keywords.remove(keyword);
|
|
|
|
db.message().setMessageKeywords(message.id, DB.Converters.fromStringArray(keywords.toArray(new String[0])));
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onAdd(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, Session isession, IMAPStore istore, IMAPFolder ifolder) throws MessagingException, JSONException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Add message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
|
|
|
throw new IllegalArgumentException("Message ID missing");
|
|
|
|
|
2019-03-25 16:26:37 +00:00
|
|
|
// Delete previous message(s) with same ID
|
|
|
|
if (folder.id.equals(message.folder)) {
|
2019-03-27 11:56:38 +00:00
|
|
|
// Prevent deleting message
|
|
|
|
db.message().setMessageUid(message.id, -1L);
|
2019-03-25 17:39:28 +00:00
|
|
|
|
2019-03-25 16:26:37 +00:00
|
|
|
Message[] ideletes = ifolder.search(new MessageIDTerm(message.msgid));
|
|
|
|
for (Message idelete : ideletes) {
|
|
|
|
long uid = ifolder.getUID(idelete);
|
|
|
|
Log.i(folder.name + " deleting previous uid=" + uid + " msgid=" + message.msgid);
|
|
|
|
try {
|
|
|
|
idelete.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ifolder.expunge();
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// Get message
|
|
|
|
MimeMessage imessage;
|
|
|
|
if (folder.id.equals(message.folder)) {
|
|
|
|
// Pre flight checks
|
|
|
|
if (!message.content)
|
|
|
|
throw new IllegalArgumentException("Message body missing");
|
|
|
|
|
|
|
|
EntityIdentity identity =
|
|
|
|
(message.identity == null ? null : db.identity().getIdentity(message.identity));
|
|
|
|
|
2019-04-17 15:27:57 +00:00
|
|
|
imessage = MessageHelper.from(context, message, identity, isession);
|
2019-02-27 15:05:15 +00:00
|
|
|
} else {
|
|
|
|
// Cross account move
|
2019-03-14 07:45:13 +00:00
|
|
|
File file = message.getRawFile(context);
|
2019-02-27 15:05:15 +00:00
|
|
|
if (!file.exists())
|
|
|
|
throw new IllegalArgumentException("raw message file not found");
|
|
|
|
|
|
|
|
Log.i(folder.name + " reading " + file);
|
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
|
|
|
imessage = new MimeMessage(isession, is);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle auto read
|
|
|
|
boolean autoread = false;
|
|
|
|
if (jargs.length() > 1) {
|
|
|
|
autoread = jargs.getBoolean(1);
|
|
|
|
if (ifolder.getPermanentFlags().contains(Flags.Flag.SEEN)) {
|
|
|
|
if (autoread && !imessage.isSet(Flags.Flag.SEEN)) {
|
|
|
|
Log.i(folder.name + " autoread");
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle draft
|
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
if (ifolder.getPermanentFlags().contains(Flags.Flag.DRAFT))
|
|
|
|
imessage.setFlag(Flags.Flag.DRAFT, true);
|
|
|
|
|
|
|
|
// Add message
|
2019-03-25 15:14:51 +00:00
|
|
|
long uid = -1;
|
|
|
|
if (istore.hasCapability("UIDPLUS")) {
|
|
|
|
AppendUID[] uids = ifolder.appendUIDMessages(new Message[]{imessage});
|
|
|
|
if (uids != null && uids.length > 0) {
|
|
|
|
Log.i("Appended uid=" + uids[0].uid);
|
|
|
|
uid = uids[0].uid;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ifolder.appendMessages(new Message[]{imessage});
|
|
|
|
|
|
|
|
// Lookup uid
|
|
|
|
if (uid <= 0) {
|
|
|
|
Log.i("Searching for appended msgid=" + message.msgid);
|
|
|
|
Message[] messages = ifolder.search(new MessageIDTerm(message.msgid));
|
|
|
|
if (messages != null)
|
|
|
|
for (Message iappended : messages) {
|
|
|
|
long muid = ifolder.getUID(iappended);
|
|
|
|
Log.i("Found appended uid=" + muid);
|
|
|
|
// RFC3501: Unique identifiers are assigned in a strictly ascending fashion
|
|
|
|
if (muid > uid)
|
|
|
|
uid = muid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uid <= 0)
|
|
|
|
throw new IllegalArgumentException("uid not found");
|
|
|
|
|
2019-03-16 09:54:10 +00:00
|
|
|
Log.i(folder.name + " appended id=" + message.id + " uid=" + uid);
|
2019-02-27 15:05:15 +00:00
|
|
|
db.message().setMessageUid(message.id, uid);
|
|
|
|
|
2019-03-25 16:26:37 +00:00
|
|
|
if (!folder.id.equals(message.folder))
|
2019-03-06 09:59:11 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
// Cross account move
|
|
|
|
if (autoread) {
|
|
|
|
Log.i(folder.name + " queuing SEEN id=" + message.id);
|
|
|
|
EntityOperation.queue(context, db, message, EntityOperation.SEEN, true);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-06 09:59:11 +00:00
|
|
|
Log.i(folder.name + " queuing DELETE id=" + message.id);
|
|
|
|
EntityOperation.queue(context, db, message, EntityOperation.DELETE);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 14:32:19 +00:00
|
|
|
private static void onMove(Context context, JSONArray jargs, boolean copy, EntityFolder folder, EntityMessage message, Session isession, IMAPStore istore, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Move message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-03-14 11:08:19 +00:00
|
|
|
// Get arguments
|
2019-02-27 15:05:15 +00:00
|
|
|
long id = jargs.getLong(0);
|
2019-03-14 11:08:19 +00:00
|
|
|
boolean autoread = (jargs.length() > 1 && jargs.getBoolean(1));
|
|
|
|
|
2019-03-15 16:38:26 +00:00
|
|
|
// Get source message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2019-03-26 09:07:34 +00:00
|
|
|
// Auto read
|
|
|
|
if (autoread && ifolder.getPermanentFlags().contains(Flags.Flag.SEEN))
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
|
2019-03-14 11:08:19 +00:00
|
|
|
// Get target folder
|
2019-02-27 15:05:15 +00:00
|
|
|
EntityFolder target = db.folder().getFolder(id);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
|
|
|
IMAPFolder itarget = (IMAPFolder) istore.getFolder(target.name);
|
|
|
|
|
2019-03-25 15:14:51 +00:00
|
|
|
ifolder.copyMessages(new Message[]{imessage}, itarget);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-25 15:14:51 +00:00
|
|
|
// Delete source
|
|
|
|
if (!copy) {
|
|
|
|
try {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-03-25 15:14:51 +00:00
|
|
|
ifolder.expunge();
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onDelete(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Delete message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-04-11 11:02:56 +00:00
|
|
|
Message[] imessages;
|
2019-02-27 15:05:15 +00:00
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
2019-04-11 11:02:56 +00:00
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("Delete without ID");
|
|
|
|
else
|
|
|
|
imessages = new Message[]{ifolder.getMessageByUID(message.uid)};
|
|
|
|
else
|
|
|
|
imessages = ifolder.search(new MessageIDTerm(message.msgid));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
for (Message imessage : imessages) {
|
|
|
|
Log.i(folder.name + " deleting uid=" + message.uid + " msgid=" + message.msgid);
|
2019-03-16 10:38:40 +00:00
|
|
|
try {
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-04-11 11:02:56 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
ifolder.expunge();
|
|
|
|
|
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onHeaders(Context context, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
|
|
|
|
// Download headers
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (message.headers != null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
MessageHelper helper = new MessageHelper(imessage);
|
|
|
|
db.message().setMessageHeaders(message.id, helper.getHeaders());
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onRaw(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException, JSONException {
|
|
|
|
// Download raw message
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (message.raw == null || !message.raw) {
|
|
|
|
IMAPMessage imessage = (IMAPMessage) ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2019-03-14 07:45:13 +00:00
|
|
|
File file = message.getRawFile(context);
|
2019-02-27 15:05:15 +00:00
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
db.message().setMessageRaw(message.id, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jargs.length() > 0) {
|
|
|
|
long target = jargs.getLong(2);
|
|
|
|
jargs.remove(2);
|
|
|
|
Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target);
|
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
|
|
|
operation.folder = target;
|
|
|
|
operation.message = message.id;
|
|
|
|
operation.name = EntityOperation.ADD;
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onBody(Context context, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Download message body
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (message.content)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
String body = parts.getHtml(context);
|
2019-03-14 07:45:13 +00:00
|
|
|
Helper.writeText(message.getFile(context), body);
|
2019-03-02 10:49:58 +00:00
|
|
|
db.message().setMessageContent(message.id, true,
|
|
|
|
HtmlHelper.getPreview(body), parts.getWarnings(message.warning));
|
2019-04-05 08:02:29 +00:00
|
|
|
|
|
|
|
updateMessageSize(context, message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
private static void onAttachment(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Download attachment
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
int sequence = jargs.getInt(0);
|
|
|
|
|
|
|
|
// Get attachment
|
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(op.message, sequence);
|
|
|
|
if (attachment.available)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
// Download attachment
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-03-10 20:26:56 +00:00
|
|
|
parts.downloadAttachment(context, sequence - 1, attachment.id);
|
2019-04-05 08:02:29 +00:00
|
|
|
|
|
|
|
updateMessageSize(context, message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
static void onSynchronizeFolders(Context context, EntityAccount account, Store istore, State state) throws MessagingException {
|
2019-02-27 15:05:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
Log.i("Start sync folders account=" + account.name);
|
|
|
|
|
2019-04-04 16:18:13 +00:00
|
|
|
// Get remote folders
|
|
|
|
Folder defaultFolder = istore.getDefaultFolder();
|
|
|
|
char separator = defaultFolder.getSeparator();
|
|
|
|
EntityLog.log(context, account.name + " folder separator=" + separator);
|
|
|
|
|
|
|
|
Folder[] ifolders = defaultFolder.list("*");
|
|
|
|
Map<Folder, String[]> attrs = new HashMap<>();
|
|
|
|
for (Folder ifolder : ifolders)
|
|
|
|
attrs.put(ifolder, ((IMAPFolder) ifolder).getAttributes());
|
|
|
|
Log.i("Remote folder count=" + ifolders.length + " separator=" + separator);
|
|
|
|
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
List<String> names = new ArrayList<>();
|
|
|
|
for (EntityFolder folder : db.folder().getFolders(account.id))
|
|
|
|
if (folder.tbc != null) {
|
|
|
|
Log.i(folder.name + " creating");
|
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (!ifolder.exists())
|
|
|
|
ifolder.create(Folder.HOLDS_MESSAGES);
|
|
|
|
db.folder().resetFolderTbc(folder.id);
|
|
|
|
} else if (folder.tbd != null && folder.tbd) {
|
|
|
|
Log.i(folder.name + " deleting");
|
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (ifolder.exists())
|
|
|
|
ifolder.delete(false);
|
|
|
|
db.folder().deleteFolder(folder.id);
|
|
|
|
} else
|
|
|
|
names.add(folder.name);
|
|
|
|
Log.i("Local folder count=" + names.size());
|
|
|
|
|
2019-03-17 19:32:57 +00:00
|
|
|
Map<String, EntityFolder> nameFolder = new HashMap<>();
|
|
|
|
Map<String, List<EntityFolder>> parentFolders = new HashMap<>();
|
2019-02-27 15:05:15 +00:00
|
|
|
for (Folder ifolder : ifolders) {
|
|
|
|
String fullName = ifolder.getFullName();
|
2019-04-04 16:18:13 +00:00
|
|
|
String[] attr = attrs.get(ifolder);
|
|
|
|
String type = EntityFolder.getType(attr, fullName);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
EntityLog.log(context, account.name + ":" + fullName +
|
2019-04-04 16:18:13 +00:00
|
|
|
" attrs=" + TextUtils.join(" ", attr) + " type=" + type);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
if (type != null) {
|
|
|
|
names.remove(fullName);
|
|
|
|
|
|
|
|
String display = null;
|
|
|
|
if (account.prefix != null && fullName.startsWith(account.prefix + separator))
|
|
|
|
display = fullName.substring(account.prefix.length() + 1);
|
|
|
|
|
|
|
|
EntityFolder folder = db.folder().getFolderByName(account.id, fullName);
|
|
|
|
if (folder == null) {
|
|
|
|
folder = new EntityFolder();
|
|
|
|
folder.account = account.id;
|
|
|
|
folder.name = fullName;
|
|
|
|
folder.display = display;
|
|
|
|
folder.type = (EntityFolder.SYSTEM.equals(type) ? type : EntityFolder.USER);
|
|
|
|
folder.synchronize = false;
|
|
|
|
folder.poll = ("imap.gmail.com".equals(account.host));
|
|
|
|
folder.sync_days = EntityFolder.DEFAULT_SYNC;
|
|
|
|
folder.keep_days = EntityFolder.DEFAULT_KEEP;
|
2019-03-19 19:09:27 +00:00
|
|
|
folder.id = db.folder().insertFolder(folder);
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " added type=" + folder.type);
|
|
|
|
} else {
|
|
|
|
Log.i(folder.name + " exists type=" + folder.type);
|
|
|
|
|
|
|
|
if (folder.display == null) {
|
|
|
|
if (display != null) {
|
|
|
|
db.folder().setFolderDisplay(folder.id, display);
|
|
|
|
EntityLog.log(context, account.name + ":" + folder.name +
|
|
|
|
" removed prefix display=" + display + " separator=" + separator);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (account.prefix == null && folder.name.endsWith(separator + folder.display)) {
|
|
|
|
db.folder().setFolderDisplay(folder.id, null);
|
|
|
|
EntityLog.log(context, account.name + ":" + folder.name +
|
|
|
|
" restored prefix display=" + folder.display + " separator=" + separator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compatibility
|
|
|
|
if ("Inbox_sub".equals(folder.type))
|
|
|
|
db.folder().setFolderType(folder.id, EntityFolder.USER);
|
|
|
|
else if (EntityFolder.USER.equals(folder.type) && EntityFolder.SYSTEM.equals(type))
|
|
|
|
db.folder().setFolderType(folder.id, type);
|
|
|
|
else if (EntityFolder.SYSTEM.equals(folder.type) && EntityFolder.USER.equals(type))
|
|
|
|
db.folder().setFolderType(folder.id, type);
|
2019-03-03 16:48:39 +00:00
|
|
|
else if (EntityFolder.INBOX.equals(type) && !EntityFolder.INBOX.equals(folder.type)) {
|
|
|
|
if (db.folder().getFolderByType(folder.account, EntityFolder.INBOX) == null)
|
|
|
|
db.folder().setFolderType(folder.id, type);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-03-17 19:32:57 +00:00
|
|
|
|
|
|
|
nameFolder.put(folder.name, folder);
|
|
|
|
String parentName = folder.getParentName(separator);
|
|
|
|
if (!parentFolders.containsKey(parentName))
|
|
|
|
parentFolders.put(parentName, new ArrayList<EntityFolder>());
|
|
|
|
parentFolders.get(parentName).add(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String parentName : parentFolders.keySet()) {
|
|
|
|
EntityFolder parent = nameFolder.get(parentName);
|
|
|
|
for (EntityFolder child : parentFolders.get(parentName))
|
|
|
|
db.folder().setFolderParent(child.id, parent == null ? null : parent.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("Delete local count=" + names.size());
|
|
|
|
for (String name : names) {
|
|
|
|
Log.i(name + " delete");
|
|
|
|
db.folder().deleteFolder(account.id, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
Log.i("End sync folder");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:02:42 +00:00
|
|
|
private static void onSynchronizeMessages(
|
2019-02-27 19:04:26 +00:00
|
|
|
Context context, JSONArray jargs,
|
|
|
|
EntityAccount account, final EntityFolder folder,
|
2019-03-30 10:57:29 +00:00
|
|
|
final IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
final DB db = DB.getInstance(context);
|
|
|
|
try {
|
2019-03-25 15:02:27 +00:00
|
|
|
// Legacy
|
|
|
|
if (jargs.length() == 0)
|
|
|
|
jargs = folder.getSyncArgs();
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
int sync_days = jargs.getInt(0);
|
|
|
|
int keep_days = jargs.getInt(1);
|
|
|
|
boolean download = jargs.getBoolean(2);
|
|
|
|
|
|
|
|
if (keep_days == sync_days)
|
|
|
|
keep_days++;
|
|
|
|
|
|
|
|
Log.i(folder.name + " start sync after=" + sync_days + "/" + keep_days);
|
|
|
|
|
|
|
|
db.folder().setFolderSyncState(folder.id, "syncing");
|
|
|
|
|
|
|
|
// Get reference times
|
|
|
|
Calendar cal_sync = Calendar.getInstance();
|
|
|
|
cal_sync.add(Calendar.DAY_OF_MONTH, -sync_days);
|
|
|
|
cal_sync.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal_sync.set(Calendar.MINUTE, 0);
|
|
|
|
cal_sync.set(Calendar.SECOND, 0);
|
|
|
|
cal_sync.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
Calendar cal_keep = Calendar.getInstance();
|
|
|
|
cal_keep.add(Calendar.DAY_OF_MONTH, -keep_days);
|
|
|
|
cal_keep.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
cal_keep.set(Calendar.MINUTE, 0);
|
|
|
|
cal_keep.set(Calendar.SECOND, 0);
|
|
|
|
cal_keep.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
|
|
|
long sync_time = cal_sync.getTimeInMillis();
|
|
|
|
if (sync_time < 0)
|
|
|
|
sync_time = 0;
|
|
|
|
|
|
|
|
long keep_time = cal_keep.getTimeInMillis();
|
|
|
|
if (keep_time < 0)
|
|
|
|
keep_time = 0;
|
|
|
|
|
|
|
|
Log.i(folder.name + " sync=" + new Date(sync_time) + " keep=" + new Date(keep_time));
|
|
|
|
|
|
|
|
// Delete old local messages
|
2019-03-09 14:09:30 +00:00
|
|
|
int old = db.message().deleteMessagesBefore(folder.id, keep_time);
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " local old=" + old);
|
|
|
|
|
|
|
|
// Get list of local uids
|
|
|
|
final List<Long> uids = db.message().getUids(folder.id, null);
|
|
|
|
Log.i(folder.name + " local count=" + uids.size());
|
|
|
|
|
|
|
|
// Reduce list of local uids
|
|
|
|
SearchTerm searchTerm = new ReceivedDateTerm(ComparisonTerm.GE, new Date(sync_time));
|
|
|
|
if (ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED))
|
|
|
|
searchTerm = new OrTerm(searchTerm, new FlagTerm(new Flags(Flags.Flag.FLAGGED), true));
|
|
|
|
|
|
|
|
long search = SystemClock.elapsedRealtime();
|
2019-03-30 17:45:54 +00:00
|
|
|
Message[] imessages;
|
|
|
|
try {
|
|
|
|
imessages = ifolder.search(searchTerm);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
if (ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED)) {
|
|
|
|
Log.w(ex.getMessage());
|
|
|
|
imessages = ifolder.search(new ReceivedDateTerm(ComparisonTerm.GE, new Date(sync_time)));
|
|
|
|
} else
|
|
|
|
throw ex;
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " remote count=" + imessages.length +
|
|
|
|
" search=" + (SystemClock.elapsedRealtime() - search) + " ms");
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(FetchProfile.Item.FLAGS);
|
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
long fetch = SystemClock.elapsedRealtime();
|
|
|
|
Log.i(folder.name + " remote fetched=" + (SystemClock.elapsedRealtime() - fetch) + " ms");
|
|
|
|
|
2019-03-30 10:57:29 +00:00
|
|
|
// Sort for finding references messages
|
|
|
|
Arrays.sort(imessages, new Comparator<Message>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Message m1, Message m2) {
|
|
|
|
try {
|
|
|
|
return Long.compare(ifolder.getUID(m1), ifolder.getUID(m2));
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-17 06:40:52 +00:00
|
|
|
for (int i = 0; i < imessages.length && state.running() && state.recoverable(); i++)
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
uids.remove(ifolder.getUID(imessages[i]));
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
reportError(context, account, folder, ex);
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uids.size() > 0) {
|
2019-03-08 12:26:46 +00:00
|
|
|
MessagingException ex = (MessagingException) ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
|
2019-02-27 15:05:15 +00:00
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) {
|
|
|
|
Log.i("Executing uid fetch count=" + uids.size());
|
|
|
|
Response[] responses = protocol.command(
|
|
|
|
"UID FETCH " + TextUtils.join(",", uids) + " (UID)", null);
|
|
|
|
|
2019-03-08 12:26:46 +00:00
|
|
|
if (responses.length > 0 && responses[responses.length - 1].isOK()) {
|
|
|
|
for (Response response : responses)
|
|
|
|
if (response instanceof FetchResponse) {
|
|
|
|
FetchResponse fr = (FetchResponse) response;
|
|
|
|
UID uid = fr.getItem(UID.class);
|
|
|
|
if (uid != null)
|
|
|
|
uids.remove(uid.uid);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-03-08 12:26:46 +00:00
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
for (Response response : responses)
|
|
|
|
if (response.isNO() || response.isBAD() || response.isBYE())
|
|
|
|
return new MessagingException(response.toString());
|
|
|
|
return new MessagingException("UID FETCH failed");
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-03-08 12:26:46 +00:00
|
|
|
if (ex != null)
|
|
|
|
throw ex;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
long getuid = SystemClock.elapsedRealtime();
|
|
|
|
Log.i(folder.name + " remote uids=" + (SystemClock.elapsedRealtime() - getuid) + " ms");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete local messages not at remote
|
|
|
|
Log.i(folder.name + " delete=" + uids.size());
|
|
|
|
for (Long uid : uids) {
|
|
|
|
int count = db.message().deleteMessage(folder.id, uid);
|
|
|
|
Log.i(folder.name + " delete local uid=" + uid + " count=" + count);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id);
|
|
|
|
|
|
|
|
fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
// fp.add(FetchProfile.Item.FLAGS);
|
|
|
|
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
|
|
|
// fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
|
|
|
|
// fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
fp.add(FetchProfile.Item.SIZE);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
|
|
|
|
|
|
|
|
// Add/update local messages
|
|
|
|
Long[] ids = new Long[imessages.length];
|
|
|
|
Log.i(folder.name + " add=" + imessages.length);
|
2019-04-17 06:40:52 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0 && state.running() && state.recoverable(); i -= SYNC_BATCH_SIZE) {
|
2019-02-27 15:05:15 +00:00
|
|
|
int from = Math.max(0, i - SYNC_BATCH_SIZE + 1);
|
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
|
|
|
|
|
|
|
// Full fetch new/changed messages only
|
|
|
|
List<Message> full = new ArrayList<>();
|
|
|
|
for (Message imessage : isub) {
|
2019-04-17 06:40:52 +00:00
|
|
|
long uid = ifolder.getUID(imessage); // already fetched
|
2019-02-27 15:05:15 +00:00
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
if (message == null)
|
|
|
|
full.add(imessage);
|
|
|
|
}
|
|
|
|
if (full.size() > 0) {
|
|
|
|
long headers = SystemClock.elapsedRealtime();
|
|
|
|
ifolder.fetch(full.toArray(new Message[0]), fp);
|
|
|
|
Log.i(folder.name + " fetched headers=" + full.size() +
|
|
|
|
" " + (SystemClock.elapsedRealtime() - headers) + " ms");
|
|
|
|
}
|
|
|
|
|
2019-04-17 06:40:52 +00:00
|
|
|
for (int j = isub.length - 1; j >= 0 && state.running() && state.recoverable(); j--)
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
EntityMessage message = synchronizeMessage(
|
|
|
|
context,
|
2019-03-18 09:52:20 +00:00
|
|
|
account, folder,
|
|
|
|
ifolder, (IMAPMessage) isub[j],
|
2019-02-27 15:05:15 +00:00
|
|
|
false,
|
|
|
|
rules);
|
|
|
|
ids[from + j] = message.id;
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (IOException ex) {
|
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
|
|
|
} else
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex, true));
|
|
|
|
} finally {
|
|
|
|
// Reduce memory usage
|
|
|
|
((IMAPMessage) isub[j]).invalidateHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete not synchronized messages without uid
|
|
|
|
db.message().deleteOrphans(folder.id);
|
|
|
|
|
|
|
|
// Add local sent messages to remote sent folder
|
|
|
|
if (EntityFolder.SENT.equals(folder.type)) {
|
|
|
|
List<EntityMessage> orphans = db.message().getSentOrphans(folder.account);
|
|
|
|
Log.i(folder.name + " sent orphans=" + orphans.size() + " account=" + folder.account);
|
|
|
|
for (EntityMessage orphan : orphans) {
|
|
|
|
Log.i(folder.name + " adding orphan id=" + orphan.id + " sent=" + new Date(orphan.sent));
|
|
|
|
orphan.folder = folder.id;
|
|
|
|
db.message().updateMessage(orphan);
|
|
|
|
EntityOperation.queue(context, db, orphan, EntityOperation.ADD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 08:20:46 +00:00
|
|
|
int count = ifolder.getMessageCount();
|
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
if (download) {
|
|
|
|
db.folder().setFolderSyncState(folder.id, "downloading");
|
|
|
|
|
|
|
|
//fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
|
|
|
|
// Download messages/attachments
|
|
|
|
Log.i(folder.name + " download=" + imessages.length);
|
2019-04-17 06:40:52 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0 && state.running() && state.recoverable(); i -= DOWNLOAD_BATCH_SIZE) {
|
2019-02-27 15:05:15 +00:00
|
|
|
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1);
|
|
|
|
|
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
|
|
|
// Fetch on demand
|
|
|
|
|
2019-04-17 06:40:52 +00:00
|
|
|
for (int j = isub.length - 1; j >= 0 && state.running() && state.recoverable(); j--)
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
if (ids[from + j] != null)
|
|
|
|
downloadMessage(
|
|
|
|
context,
|
|
|
|
folder, ifolder,
|
2019-03-16 13:12:31 +00:00
|
|
|
(IMAPMessage) isub[j], ids[from + j], state);
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (FolderClosedIOException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
} finally {
|
|
|
|
// Free memory
|
|
|
|
((IMAPMessage) isub[j]).invalidateHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.running)
|
|
|
|
db.folder().setFolderInitialized(folder.id);
|
|
|
|
|
|
|
|
db.folder().setFolderSync(folder.id, new Date().getTime());
|
|
|
|
db.folder().setFolderError(folder.id, null);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end sync state=" + state);
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static EntityMessage synchronizeMessage(
|
|
|
|
Context context,
|
2019-03-18 09:52:20 +00:00
|
|
|
EntityAccount account, EntityFolder folder,
|
|
|
|
IMAPFolder ifolder, IMAPMessage imessage,
|
2019-02-27 15:05:15 +00:00
|
|
|
boolean browsed,
|
|
|
|
List<EntityRule> rules) throws MessagingException, IOException {
|
2019-03-31 18:54:01 +00:00
|
|
|
synchronized (folder) {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
if (imessage.isExpunged()) {
|
|
|
|
Log.i(folder.name + " expunged uid=" + uid);
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
}
|
|
|
|
if (imessage.isSet(Flags.Flag.DELETED)) {
|
|
|
|
Log.i(folder.name + " deleted uid=" + uid);
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage);
|
|
|
|
boolean seen = helper.getSeen();
|
|
|
|
boolean answered = helper.getAnsered();
|
|
|
|
boolean flagged = helper.getFlagged();
|
|
|
|
String flags = helper.getFlags();
|
|
|
|
String[] keywords = helper.getKeywords();
|
|
|
|
boolean update = false;
|
|
|
|
boolean filter = false;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
// Find message by uid (fast, no headers required)
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
|
|
|
|
// Find message by Message-ID (slow, headers required)
|
|
|
|
// - messages in inbox have same id as message sent to self
|
|
|
|
// - messages in archive have same id as original
|
|
|
|
if (message == null) {
|
|
|
|
String msgid = helper.getMessageID();
|
|
|
|
Log.i(folder.name + " searching for " + msgid);
|
|
|
|
for (EntityMessage dup : db.message().getMessageByMsgId(folder.account, msgid)) {
|
|
|
|
EntityFolder dfolder = db.folder().getFolder(dup.folder);
|
|
|
|
Log.i(folder.name + " found as id=" + dup.id + "/" + dup.uid +
|
|
|
|
" folder=" + dfolder.type + ":" + dup.folder + "/" + folder.type + ":" + folder.id +
|
|
|
|
" msgid=" + dup.msgid + " thread=" + dup.thread);
|
|
|
|
|
|
|
|
if (dup.folder.equals(folder.id) ||
|
|
|
|
(EntityFolder.OUTBOX.equals(dfolder.type) && EntityFolder.SENT.equals(folder.type))) {
|
|
|
|
String thread = helper.getThreadId(context, account.id, uid);
|
|
|
|
Log.i(folder.name + " found as id=" + dup.id +
|
|
|
|
" uid=" + dup.uid + "/" + uid +
|
|
|
|
" msgid=" + msgid + " thread=" + thread);
|
|
|
|
|
|
|
|
if (dup.uid == null) {
|
|
|
|
Log.i(folder.name + " set uid=" + uid);
|
|
|
|
dup.folder = folder.id; // outbox to sent
|
|
|
|
dup.uid = uid;
|
|
|
|
dup.msgid = msgid;
|
|
|
|
dup.thread = thread;
|
2019-04-05 07:24:30 +00:00
|
|
|
if (dup.size == null)
|
|
|
|
dup.size = helper.getSize();
|
2019-03-31 18:54:01 +00:00
|
|
|
dup.error = null;
|
|
|
|
message = dup;
|
|
|
|
update = true;
|
|
|
|
filter = true;
|
|
|
|
}
|
2019-03-31 14:36:41 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
if (message == null)
|
|
|
|
filter = true;
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
if (message == null) {
|
|
|
|
Address[] froms = helper.getFrom();
|
|
|
|
Address[] tos = helper.getTo();
|
|
|
|
Address[] ccs = helper.getCc();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
// Build ordered list of addresses
|
|
|
|
List<Address> addresses = new ArrayList<>();
|
|
|
|
if (folder.isOutgoing()) {
|
2019-03-31 06:35:31 +00:00
|
|
|
if (froms != null)
|
|
|
|
addresses.addAll(Arrays.asList(froms));
|
2019-03-31 18:54:01 +00:00
|
|
|
} else {
|
|
|
|
if (tos != null)
|
|
|
|
addresses.addAll(Arrays.asList(tos));
|
|
|
|
if (ccs != null)
|
|
|
|
addresses.addAll(Arrays.asList(ccs));
|
2019-04-04 16:35:49 +00:00
|
|
|
if (EntityFolder.ARCHIVE.equals(folder.type) || BuildConfig.DEBUG) {
|
2019-03-31 18:54:01 +00:00
|
|
|
if (froms != null)
|
|
|
|
addresses.addAll(Arrays.asList(froms));
|
2019-03-14 19:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
// Search for matching identity
|
|
|
|
EntityIdentity identity = null;
|
|
|
|
for (Address address : addresses) {
|
|
|
|
String email = ((InternetAddress) address).getAddress();
|
|
|
|
if (!TextUtils.isEmpty(email)) {
|
|
|
|
identity = db.identity().getIdentity(folder.account, email);
|
|
|
|
if (identity == null) {
|
|
|
|
String canonical = Helper.canonicalAddress(email);
|
|
|
|
if (!canonical.equals(email))
|
|
|
|
identity = db.identity().getIdentity(folder.account, canonical);
|
|
|
|
}
|
|
|
|
if (identity != null)
|
|
|
|
break;
|
|
|
|
}
|
2019-03-30 13:49:51 +00:00
|
|
|
}
|
2019-03-14 17:46:45 +00:00
|
|
|
|
2019-04-12 14:15:42 +00:00
|
|
|
String authentication = helper.getAuthentication();
|
2019-03-31 18:54:01 +00:00
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.identity = (identity == null ? null : identity.id);
|
|
|
|
message.uid = uid;
|
|
|
|
|
|
|
|
message.msgid = helper.getMessageID();
|
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
|
|
|
Log.w("No Message-ID id=" + message.id + " uid=" + message.uid);
|
|
|
|
|
|
|
|
message.references = TextUtils.join(" ", helper.getReferences());
|
|
|
|
message.inreplyto = helper.getInReplyTo();
|
|
|
|
// Local address contains control or whitespace in string ``mailing list someone@example.org''
|
|
|
|
message.deliveredto = helper.getDeliveredTo();
|
|
|
|
message.thread = helper.getThreadId(context, account.id, uid);
|
2019-04-12 18:41:06 +00:00
|
|
|
message.dkim = MessageHelper.getAuthentication("dkim", authentication);
|
|
|
|
message.spf = MessageHelper.getAuthentication("spf", authentication);
|
|
|
|
message.dmarc = MessageHelper.getAuthentication("dmarc", authentication);
|
2019-03-31 18:54:01 +00:00
|
|
|
message.from = froms;
|
|
|
|
message.to = tos;
|
|
|
|
message.cc = ccs;
|
|
|
|
message.bcc = helper.getBcc();
|
|
|
|
message.reply = helper.getReply();
|
|
|
|
message.subject = helper.getSubject();
|
|
|
|
message.size = helper.getSize();
|
|
|
|
message.content = false;
|
|
|
|
message.received = helper.getReceived();
|
|
|
|
message.sent = helper.getSent();
|
2019-02-27 15:05:15 +00:00
|
|
|
message.seen = seen;
|
|
|
|
message.answered = answered;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.flags = flags;
|
|
|
|
message.keywords = keywords;
|
2019-03-31 18:54:01 +00:00
|
|
|
message.ui_seen = seen;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
message.ui_flagged = flagged;
|
2019-02-27 15:05:15 +00:00
|
|
|
message.ui_hide = false;
|
2019-03-31 18:54:01 +00:00
|
|
|
message.ui_found = false;
|
|
|
|
message.ui_ignored = seen;
|
|
|
|
message.ui_browsed = browsed;
|
|
|
|
|
|
|
|
message.sender = MessageHelper.getSortKey(message.from);
|
2019-04-05 06:33:43 +00:00
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(context, message.from);
|
2019-03-31 18:54:01 +00:00
|
|
|
message.avatar = (lookupUri == null ? null : lookupUri.toString());
|
|
|
|
|
|
|
|
Address sender = helper.getSender(); // header
|
|
|
|
if (sender != null) {
|
|
|
|
String[] s = ((InternetAddress) sender).getAddress().split("@");
|
|
|
|
String[] f = (froms == null || froms.length == 0 ? null
|
|
|
|
: (((InternetAddress) froms[0]).getAddress()).split("@"));
|
|
|
|
if (s.length > 1 && (f == null || (f.length > 1 && !s[1].equals(f[1]))))
|
|
|
|
message.warning = context.getString(R.string.title_via, s[1]);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-30 13:49:51 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
|
|
|
Log.i(folder.name + " added id=" + message.id + " uid=" + message.uid);
|
|
|
|
|
|
|
|
int sequence = 1;
|
|
|
|
for (EntityAttachment attachment : parts.getAttachments()) {
|
|
|
|
Log.i(folder.name + " attachment seq=" + sequence +
|
|
|
|
" name=" + attachment.name + " type=" + attachment.type +
|
|
|
|
" cid=" + attachment.cid + " pgp=" + attachment.encryption);
|
|
|
|
attachment.message = message.id;
|
|
|
|
attachment.sequence = sequence++;
|
|
|
|
attachment.id = db.attachment().insertAttachment(attachment);
|
|
|
|
}
|
2019-03-30 13:49:51 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
if (message.received > account.created &&
|
|
|
|
!EntityFolder.ARCHIVE.equals(folder.type) &&
|
|
|
|
!EntityFolder.TRASH.equals(folder.type) &&
|
|
|
|
!EntityFolder.JUNK.equals(folder.type))
|
|
|
|
updateContactInfo(context, folder, message);
|
|
|
|
|
|
|
|
runRules(context, imessage, message, rules);
|
2019-03-30 13:49:51 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
} else {
|
|
|
|
if (!message.seen.equals(seen) || !message.seen.equals(message.ui_seen)) {
|
|
|
|
update = true;
|
|
|
|
message.seen = seen;
|
|
|
|
message.ui_seen = seen;
|
|
|
|
if (seen)
|
|
|
|
message.ui_ignored = true;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " seen=" + seen);
|
|
|
|
}
|
2019-03-30 13:49:51 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
if (!message.answered.equals(answered) || !message.answered.equals(message.ui_answered)) {
|
|
|
|
if (!answered && message.ui_answered && ifolder.getPermanentFlags().contains(Flags.Flag.ANSWERED)) {
|
|
|
|
// This can happen when the answered operation was skipped because the message was moving
|
|
|
|
answered = true;
|
|
|
|
imessage.setFlag(Flags.Flag.ANSWERED, answered);
|
|
|
|
}
|
|
|
|
update = true;
|
|
|
|
message.answered = answered;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " answered=" + answered);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!message.flagged.equals(flagged) || !message.flagged.equals(message.ui_flagged)) {
|
|
|
|
update = true;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.ui_flagged = flagged;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " flagged=" + flagged);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Objects.equals(flags, message.flags)) {
|
|
|
|
update = true;
|
|
|
|
message.flags = flags;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " flags=" + flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Helper.equal(message.keywords, keywords)) {
|
|
|
|
update = true;
|
|
|
|
message.keywords = keywords;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid +
|
|
|
|
" keywords=" + TextUtils.join(" ", keywords));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.ui_hide && db.operation().getOperationCount(folder.id, message.id) == 0) {
|
|
|
|
update = true;
|
|
|
|
message.ui_hide = false;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " unhide");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.ui_browsed) {
|
|
|
|
update = true;
|
|
|
|
message.ui_browsed = false;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " unbrowse");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update)
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
db.message().updateMessage(message);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
if (filter)
|
|
|
|
runRules(context, imessage, message, rules);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (BuildConfig.DEBUG)
|
|
|
|
Log.i(folder.name + " unchanged uid=" + uid);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
List<String> fkeywords = new ArrayList<>(Arrays.asList(folder.keywords));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-31 18:54:01 +00:00
|
|
|
for (String keyword : keywords)
|
|
|
|
if (!fkeywords.contains(keyword)) {
|
|
|
|
Log.i(folder.name + " adding keyword=" + keyword);
|
|
|
|
fkeywords.add(keyword);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (folder.keywords.length != fkeywords.size()) {
|
|
|
|
Collections.sort(fkeywords);
|
|
|
|
db.folder().setFolderKeywords(folder.id, DB.Converters.fromStringArray(fkeywords.toArray(new String[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 06:48:42 +00:00
|
|
|
private static void runRules(Context context, IMAPMessage imessage, EntityMessage message, List<EntityRule> rules) {
|
|
|
|
if (!Helper.isPro(context))
|
|
|
|
return;
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
for (EntityRule rule : rules)
|
|
|
|
if (rule.matches(context, message, imessage)) {
|
|
|
|
rule.execute(context, db, message);
|
|
|
|
if (rule.stop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
db.message().setMessageError(message.id, Helper.formatThrowable(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 13:57:06 +00:00
|
|
|
private static void updateContactInfo(Context context, EntityFolder folder, EntityMessage message) {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
int type = (folder.isOutgoing() ? EntityContact.TYPE_TO : EntityContact.TYPE_FROM);
|
|
|
|
Address[] recipients = (type == EntityContact.TYPE_TO
|
|
|
|
? message.to
|
|
|
|
: (message.reply != null ? message.reply : message.from));
|
|
|
|
if (recipients != null) {
|
|
|
|
// Check if from self
|
|
|
|
if (type == EntityContact.TYPE_FROM) {
|
|
|
|
boolean me = true;
|
|
|
|
for (Address reply : recipients) {
|
|
|
|
String email = ((InternetAddress) reply).getAddress();
|
|
|
|
String canonical = Helper.canonicalAddress(email);
|
|
|
|
if (!TextUtils.isEmpty(email) &&
|
|
|
|
db.identity().getIdentity(folder.account, email.toLowerCase()) == null &&
|
|
|
|
(canonical.equals(email) ||
|
|
|
|
db.identity().getIdentity(folder.account, canonical) == null)) {
|
|
|
|
me = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (me)
|
|
|
|
recipients = message.to;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Address recipient : recipients) {
|
2019-04-06 06:24:01 +00:00
|
|
|
String email = ((InternetAddress) recipient).getAddress().toLowerCase();
|
2019-03-30 13:57:06 +00:00
|
|
|
String name = ((InternetAddress) recipient).getPersonal();
|
2019-04-05 06:33:43 +00:00
|
|
|
Uri avatar = ContactInfo.getLookupUri(context, new Address[]{recipient});
|
2019-03-30 13:57:06 +00:00
|
|
|
EntityContact contact = db.contact().getContact(folder.account, type, email);
|
|
|
|
if (contact == null) {
|
|
|
|
contact = new EntityContact();
|
|
|
|
contact.account = folder.account;
|
|
|
|
contact.type = type;
|
|
|
|
contact.email = email;
|
|
|
|
contact.name = name;
|
|
|
|
contact.avatar = (avatar == null ? null : avatar.toString());
|
|
|
|
contact.times_contacted = 1;
|
|
|
|
contact.first_contacted = message.received;
|
|
|
|
contact.last_contacted = message.received;
|
|
|
|
contact.id = db.contact().insertContact(contact);
|
|
|
|
Log.i("Inserted contact=" + contact + " type=" + type);
|
|
|
|
} else {
|
|
|
|
contact.name = name;
|
|
|
|
contact.avatar = (avatar == null ? null : avatar.toString());
|
|
|
|
contact.times_contacted++;
|
|
|
|
contact.first_contacted = Math.min(contact.first_contacted, message.received);
|
|
|
|
contact.last_contacted = message.received;
|
|
|
|
db.contact().updateContact(contact);
|
|
|
|
Log.i("Updated contact=" + contact + " type=" + type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
static void downloadMessage(
|
|
|
|
Context context,
|
|
|
|
EntityFolder folder, IMAPFolder ifolder,
|
2019-03-16 13:12:31 +00:00
|
|
|
IMAPMessage imessage, long id, State state) throws MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
|
|
|
if (message == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-04-07 05:58:34 +00:00
|
|
|
long maxSize = prefs.getInt("download", MessageHelper.DEFAULT_ATTACHMENT_DOWNLOAD_SIZE);
|
2019-02-27 15:05:15 +00:00
|
|
|
if (maxSize == 0)
|
|
|
|
maxSize = Long.MAX_VALUE;
|
|
|
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
MessageHelper helper = new MessageHelper(imessage);
|
|
|
|
|
|
|
|
boolean fetch = false;
|
|
|
|
if (!message.content)
|
2019-03-16 13:12:31 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() || (message.size != null && message.size < maxSize))
|
2019-02-27 15:05:15 +00:00
|
|
|
fetch = true;
|
|
|
|
|
|
|
|
if (!fetch)
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
if (!attachment.available)
|
2019-03-16 13:12:31 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize)) {
|
2019-02-27 15:05:15 +00:00
|
|
|
fetch = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetch) {
|
|
|
|
Log.i(folder.name + " fetching message id=" + message.id);
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
fp.add(FetchProfile.Item.FLAGS);
|
|
|
|
fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.HEADERS);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.MESSAGE);
|
|
|
|
fp.add(FetchProfile.Item.SIZE);
|
|
|
|
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
|
|
|
|
ifolder.fetch(new Message[]{imessage}, fp);
|
|
|
|
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
if (!message.content) {
|
2019-03-16 13:12:31 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() || (message.size != null && message.size < maxSize)) {
|
2019-02-27 15:05:15 +00:00
|
|
|
String body = parts.getHtml(context);
|
2019-03-14 07:45:13 +00:00
|
|
|
Helper.writeText(message.getFile(context), body);
|
2019-03-02 10:49:58 +00:00
|
|
|
db.message().setMessageContent(message.id, true,
|
|
|
|
HtmlHelper.getPreview(body), parts.getWarnings(message.warning));
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " downloaded message id=" + message.id + " size=" + message.size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (EntityAttachment attachment : attachments)
|
|
|
|
if (!attachment.available)
|
2019-03-16 13:12:31 +00:00
|
|
|
if (state.getNetworkState().isUnmetered() || (attachment.size != null && attachment.size < maxSize))
|
2019-03-14 08:05:28 +00:00
|
|
|
try {
|
|
|
|
parts.downloadAttachment(context, attachment.sequence - 1, attachment.id);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-04-05 07:45:51 +00:00
|
|
|
|
|
|
|
updateMessageSize(context, message.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 08:02:29 +00:00
|
|
|
static void updateMessageSize(Context context, long id) {
|
2019-04-05 07:45:51 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
|
|
|
if (message == null || !message.content)
|
|
|
|
return;
|
|
|
|
|
2019-04-05 10:00:28 +00:00
|
|
|
long size = message.getFile(context).length();
|
2019-04-05 07:45:51 +00:00
|
|
|
if (size == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<EntityAttachment> attachments = db.attachment().getAttachments(message.id);
|
|
|
|
for (EntityAttachment attachment : attachments) {
|
|
|
|
if (!attachment.available)
|
|
|
|
return;
|
|
|
|
|
|
|
|
long asize = attachment.getFile(context).length();
|
|
|
|
if (asize == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size += asize;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("Setting message=" + id + " size=" + message.size + "/" + size);
|
|
|
|
db.message().setMessageSize(message.id, size);
|
|
|
|
}
|
|
|
|
|
2019-02-27 19:04:26 +00:00
|
|
|
static void notifyMessages(Context context, List<TupleMessageEx> messages) {
|
2019-02-27 18:29:58 +00:00
|
|
|
Log.i("Notify messages=" + messages.size());
|
|
|
|
|
2019-03-02 09:45:14 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
|
2019-03-30 07:43:36 +00:00
|
|
|
boolean badge = prefs.getBoolean("badge", true);
|
|
|
|
|
|
|
|
Widget.update(context, messages.size());
|
2019-04-05 12:38:54 +00:00
|
|
|
try {
|
|
|
|
ShortcutBadger.applyCount(context, badge ? messages.size() : 0);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-03-30 07:43:36 +00:00
|
|
|
|
2019-02-27 18:29:58 +00:00
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
Map<String, List<Long>> groupNotifying = new HashMap<>();
|
|
|
|
Map<String, List<TupleMessageEx>> groupMessages = new HashMap<>();
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2019-03-02 09:45:14 +00:00
|
|
|
// Previous
|
|
|
|
for (String key : prefs.getAll().keySet())
|
|
|
|
if (key.startsWith("notifying:")) {
|
2019-03-07 12:29:03 +00:00
|
|
|
String group = key.substring(key.indexOf(":") + 1);
|
|
|
|
groupNotifying.put(group, new ArrayList<Long>());
|
2019-03-02 09:45:14 +00:00
|
|
|
|
|
|
|
for (String id : prefs.getString(key, null).split(","))
|
2019-03-07 12:29:03 +00:00
|
|
|
groupNotifying.get(group).add(Long.parseLong(id));
|
|
|
|
|
|
|
|
Log.i("Notifying " + group + "=" + TextUtils.join(",", groupNotifying.get(group)));
|
2019-03-02 09:45:14 +00:00
|
|
|
|
|
|
|
editor.remove(key);
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Current
|
|
|
|
for (TupleMessageEx message : messages) {
|
2019-04-17 08:23:51 +00:00
|
|
|
// Check if notification channel enabled
|
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O &&
|
|
|
|
message.from != null && message.from.length > 0) {
|
|
|
|
InternetAddress from = (InternetAddress) message.from[0];
|
|
|
|
NotificationChannel channel = nm.getNotificationChannel("notification." + from.getAddress().toLowerCase());
|
|
|
|
if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE)
|
|
|
|
continue;
|
|
|
|
}
|
2019-03-07 12:29:03 +00:00
|
|
|
|
2019-04-17 08:23:51 +00:00
|
|
|
String group = Long.toString(message.accountNotify ? message.account : 0);
|
2019-03-07 12:29:03 +00:00
|
|
|
if (!groupMessages.containsKey(group)) {
|
|
|
|
groupMessages.put(group, new ArrayList<TupleMessageEx>());
|
|
|
|
if (!groupNotifying.containsKey(group))
|
|
|
|
groupNotifying.put(group, new ArrayList<Long>());
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
2019-03-07 12:29:03 +00:00
|
|
|
|
|
|
|
groupMessages.get(group).add(message);
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Difference
|
2019-03-07 12:29:03 +00:00
|
|
|
for (String group : groupNotifying.keySet()) {
|
|
|
|
List<Notification> notifications = getNotificationUnseen(context, group, groupMessages.get(group));
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2019-03-02 09:45:14 +00:00
|
|
|
List<String> all = new ArrayList<>();
|
2019-03-02 09:02:42 +00:00
|
|
|
List<Long> add = new ArrayList<>();
|
2019-03-07 12:29:03 +00:00
|
|
|
List<Long> remove = groupNotifying.get(group);
|
2019-02-27 18:29:58 +00:00
|
|
|
for (Notification notification : notifications) {
|
|
|
|
Long id = notification.extras.getLong("id", 0);
|
|
|
|
if (id != 0) {
|
2019-03-02 09:45:14 +00:00
|
|
|
all.add(Long.toString(id));
|
2019-03-02 09:02:42 +00:00
|
|
|
if (remove.contains(id)) {
|
|
|
|
remove.remove(id);
|
|
|
|
Log.i("Notify existing=" + id);
|
2019-02-27 18:29:58 +00:00
|
|
|
} else {
|
2019-03-02 09:02:42 +00:00
|
|
|
remove.remove(-id);
|
|
|
|
add.add(id);
|
2019-02-27 18:29:58 +00:00
|
|
|
Log.i("Notify adding=" + id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int headers = 0;
|
2019-03-02 09:02:42 +00:00
|
|
|
for (Long id : add)
|
2019-02-27 18:29:58 +00:00
|
|
|
if (id < 0)
|
|
|
|
headers++;
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
Log.i("Notify group=" + group + " count=" + notifications.size() +
|
2019-03-02 09:02:42 +00:00
|
|
|
" added=" + add.size() + " removed=" + remove.size() + " headers=" + headers);
|
2019-02-27 18:29:58 +00:00
|
|
|
|
|
|
|
if (notifications.size() == 0 ||
|
|
|
|
(Build.VERSION.SDK_INT < Build.VERSION_CODES.O && headers > 0))
|
2019-03-07 12:29:03 +00:00
|
|
|
nm.cancel("unseen." + group + ":0", 1);
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2019-03-02 09:02:42 +00:00
|
|
|
for (Long id : remove)
|
2019-03-07 12:29:03 +00:00
|
|
|
nm.cancel("unseen." + group + ":" + Math.abs(id), 1);
|
2019-02-27 18:29:58 +00:00
|
|
|
|
|
|
|
for (Notification notification : notifications) {
|
|
|
|
long id = notification.extras.getLong("id", 0);
|
2019-03-02 09:02:42 +00:00
|
|
|
if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id))
|
2019-03-07 12:29:03 +00:00
|
|
|
nm.notify("unseen." + group + ":" + Math.abs(id), 1, notification);
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
2019-03-02 09:45:14 +00:00
|
|
|
|
|
|
|
if (all.size() > 0)
|
2019-03-07 12:29:03 +00:00
|
|
|
editor.putString("notifying:" + group, TextUtils.join(",", all));
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
2019-03-02 09:45:14 +00:00
|
|
|
|
|
|
|
editor.apply();
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
private static List<Notification> getNotificationUnseen(Context context, String group, List<TupleMessageEx> messages) {
|
2019-02-27 17:33:46 +00:00
|
|
|
List<Notification> notifications = new ArrayList<>();
|
2019-03-07 12:29:03 +00:00
|
|
|
// https://developer.android.com/training/notify-user/group
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-03-02 09:45:14 +00:00
|
|
|
if (messages == null || messages.size() == 0)
|
2019-02-27 17:33:46 +00:00
|
|
|
return notifications;
|
|
|
|
|
|
|
|
boolean pro = Helper.isPro(context);
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-03-07 12:29:03 +00:00
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
|
|
|
// Get contact info
|
|
|
|
Map<TupleMessageEx, ContactInfo> messageContact = new HashMap<>();
|
|
|
|
for (TupleMessageEx message : messages)
|
|
|
|
messageContact.put(message, ContactInfo.get(context, message.from, false));
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build pending intents
|
2019-03-14 14:57:24 +00:00
|
|
|
Intent summary = new Intent(context, ActivityView.class);
|
|
|
|
summary.setAction("unified");
|
|
|
|
PendingIntent piSummary = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, summary, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
|
|
|
Intent clear = new Intent(context, ServiceUI.class);
|
|
|
|
clear.setAction("clear");
|
|
|
|
PendingIntent piClear = PendingIntent.getService(context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build title
|
|
|
|
String title = context.getResources().getQuantityString(
|
|
|
|
R.plurals.title_notification_unseen, messages.size(), messages.size());
|
2019-02-27 17:33:46 +00:00
|
|
|
|
|
|
|
// Build notification
|
2019-03-07 12:29:03 +00:00
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notification");
|
2019-02-27 17:33:46 +00:00
|
|
|
builder
|
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setContentTitle(title)
|
2019-02-27 17:33:46 +00:00
|
|
|
.setContentIntent(piSummary)
|
|
|
|
.setNumber(messages.size())
|
|
|
|
.setShowWhen(false)
|
|
|
|
.setDeleteIntent(piClear)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
2019-02-27 17:33:46 +00:00
|
|
|
.setGroup(group)
|
|
|
|
.setGroupSummary(true);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
Notification pub = builder.build();
|
|
|
|
builder
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
|
|
|
.setPublicVersion(pub);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
|
|
|
boolean light = prefs.getBoolean("light", false);
|
|
|
|
String sound = prefs.getString("sound", null);
|
|
|
|
|
|
|
|
if (light)
|
|
|
|
builder.setLights(Color.GREEN, 1000, 1000);
|
|
|
|
|
|
|
|
if (sound == null) {
|
|
|
|
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
|
|
|
builder.setSound(uri);
|
|
|
|
} else
|
|
|
|
builder.setSound(Uri.parse(sound));
|
|
|
|
|
|
|
|
builder.setOnlyAlertOnce(true);
|
|
|
|
} else
|
|
|
|
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
|
|
|
|
if (pro) {
|
|
|
|
DateFormat df = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (EntityMessage message : messages) {
|
|
|
|
sb.append("<strong>").append(messageContact.get(message).getDisplayName(true)).append("</strong>");
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
sb.append(": ").append(message.subject);
|
|
|
|
sb.append(" ").append(df.format(message.received));
|
|
|
|
sb.append("<br>");
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(HtmlHelper.fromHtml(sb.toString()))
|
|
|
|
.setSummaryText(title));
|
|
|
|
}
|
|
|
|
|
|
|
|
notifications.add(builder.build());
|
|
|
|
|
|
|
|
boolean preview = prefs.getBoolean("notify_preview", true);
|
|
|
|
for (TupleMessageEx message : messages) {
|
|
|
|
ContactInfo info = messageContact.get(message);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build arguments
|
2019-02-27 17:33:46 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putLong("id", message.content ? message.id : -message.id);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build pending intents
|
2019-02-27 17:33:46 +00:00
|
|
|
Intent thread = new Intent(context, ActivityView.class);
|
|
|
|
thread.setAction("thread:" + message.thread);
|
|
|
|
thread.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
thread.putExtra("account", message.account);
|
|
|
|
thread.putExtra("id", message.id);
|
|
|
|
PendingIntent piContent = PendingIntent.getActivity(
|
|
|
|
context, ActivityView.REQUEST_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
Intent ignored = new Intent(context, ServiceUI.class);
|
|
|
|
ignored.setAction("ignore:" + message.id);
|
|
|
|
PendingIntent piDelete = PendingIntent.getService(context, ServiceUI.PI_IGNORED, ignored, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
Intent seen = new Intent(context, ServiceUI.class);
|
|
|
|
seen.setAction("seen:" + message.id);
|
|
|
|
PendingIntent piSeen = PendingIntent.getService(context, ServiceUI.PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
Intent archive = new Intent(context, ServiceUI.class);
|
|
|
|
archive.setAction("archive:" + message.id);
|
|
|
|
PendingIntent piArchive = PendingIntent.getService(context, ServiceUI.PI_ARCHIVE, archive, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
Intent trash = new Intent(context, ServiceUI.class);
|
|
|
|
trash.setAction("trash:" + message.id);
|
|
|
|
PendingIntent piTrash = PendingIntent.getService(context, ServiceUI.PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build actions
|
2019-02-27 17:33:46 +00:00
|
|
|
NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_visibility_24,
|
|
|
|
context.getString(R.string.title_action_seen),
|
|
|
|
piSeen);
|
|
|
|
|
|
|
|
NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_archive_24,
|
|
|
|
context.getString(R.string.title_action_archive),
|
|
|
|
piArchive);
|
|
|
|
|
|
|
|
NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_delete_24,
|
|
|
|
context.getString(R.string.title_action_trash),
|
|
|
|
piTrash);
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Get channel name
|
|
|
|
String channelName = null;
|
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O &&
|
|
|
|
message.from != null && message.from.length > 0) {
|
|
|
|
InternetAddress from = (InternetAddress) message.from[0];
|
|
|
|
NotificationChannel channel = nm.getNotificationChannel("notification." + from.getAddress().toLowerCase());
|
2019-04-17 08:23:51 +00:00
|
|
|
if (channel != null)
|
2019-03-07 12:29:03 +00:00
|
|
|
channelName = channel.getId();
|
|
|
|
}
|
|
|
|
if (channelName == null)
|
2019-03-17 18:07:53 +00:00
|
|
|
channelName = EntityAccount.getNotificationChannelId(message.accountNotify ? message.account : 0);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Get folder name
|
2019-02-27 17:33:46 +00:00
|
|
|
String folderName = message.folderDisplay == null
|
|
|
|
? Helper.localizeFolderName(context, message.folderName)
|
|
|
|
: message.folderDisplay;
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
NotificationCompat.Builder mbuilder;
|
|
|
|
mbuilder = new NotificationCompat.Builder(context, channelName);
|
|
|
|
|
2019-02-27 17:33:46 +00:00
|
|
|
mbuilder
|
|
|
|
.addExtras(args)
|
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
|
|
|
.setContentTitle(info.getDisplayName(true))
|
|
|
|
.setSubText(message.accountName + " · " + folderName)
|
|
|
|
.setContentIntent(piContent)
|
|
|
|
.setWhen(message.received)
|
|
|
|
.setDeleteIntent(piDelete)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
2019-02-27 17:33:46 +00:00
|
|
|
.setGroup(group)
|
|
|
|
.setGroupSummary(false)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setOnlyAlertOnce(true)
|
2019-02-27 17:33:46 +00:00
|
|
|
.addAction(actionSeen.build())
|
|
|
|
.addAction(actionArchive.build())
|
|
|
|
.addAction(actionTrash.build());
|
|
|
|
|
|
|
|
if (pro) {
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
mbuilder.setContentText(message.subject);
|
|
|
|
|
|
|
|
if (message.content && preview)
|
|
|
|
try {
|
2019-03-14 07:45:13 +00:00
|
|
|
String body = Helper.readText(message.getFile(context));
|
2019-02-27 17:33:46 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
sb.append(message.subject).append("<br>");
|
|
|
|
String text = Jsoup.parse(body).text();
|
|
|
|
if (!TextUtils.isEmpty(text)) {
|
|
|
|
sb.append("<em>");
|
|
|
|
if (text.length() > HtmlHelper.PREVIEW_SIZE) {
|
|
|
|
sb.append(text.substring(0, HtmlHelper.PREVIEW_SIZE));
|
|
|
|
sb.append("…");
|
|
|
|
} else
|
|
|
|
sb.append(text);
|
|
|
|
sb.append("</em>");
|
|
|
|
}
|
|
|
|
mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sb.toString())));
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.hasPhoto())
|
|
|
|
mbuilder.setLargeIcon(info.getPhotoBitmap());
|
|
|
|
|
|
|
|
if (info.hasLookupUri())
|
|
|
|
mbuilder.addPerson(info.getLookupUri().toString());
|
|
|
|
|
|
|
|
if (message.accountColor != null) {
|
|
|
|
mbuilder.setColor(message.accountColor);
|
|
|
|
mbuilder.setColorized(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 18:40:41 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
mbuilder.setSound(null);
|
|
|
|
else
|
|
|
|
mbuilder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
|
|
|
notifications.add(mbuilder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
static void reportError(Context context, EntityAccount account, EntityFolder folder, Throwable ex) {
|
|
|
|
// FolderClosedException: can happen when no connectivity
|
|
|
|
|
|
|
|
// IllegalStateException:
|
|
|
|
// - "This operation is not allowed on a closed folder"
|
|
|
|
// - can happen when syncing message
|
|
|
|
|
|
|
|
// ConnectionException
|
|
|
|
// - failed to create new store connection (connectivity)
|
|
|
|
|
|
|
|
// MailConnectException
|
|
|
|
// - on connectivity problems when connecting to store
|
|
|
|
|
|
|
|
String title;
|
|
|
|
if (account == null)
|
|
|
|
title = folder.name;
|
|
|
|
else if (folder == null)
|
|
|
|
title = account.name;
|
|
|
|
else
|
|
|
|
title = account.name + "/" + folder.name;
|
|
|
|
|
|
|
|
String tag = "error:" + (account == null ? 0 : account.id) + ":" + (folder == null ? 0 : folder.id);
|
|
|
|
|
|
|
|
EntityLog.log(context, title + " " + Helper.formatThrowable(ex));
|
|
|
|
|
2019-03-28 06:46:24 +00:00
|
|
|
if (ex instanceof AuthenticationFailedException || // Also: Too many simultaneous connections
|
|
|
|
ex instanceof AlertException ||
|
|
|
|
ex instanceof SendFailedException) {
|
2019-02-27 15:05:15 +00:00
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(tag, 1, getNotificationError(context, title, ex).build());
|
|
|
|
}
|
|
|
|
|
|
|
|
// connection failure: Too many simultaneous connections
|
|
|
|
|
|
|
|
if (BuildConfig.DEBUG &&
|
|
|
|
!(ex instanceof SendFailedException) &&
|
|
|
|
!(ex instanceof MailConnectException) &&
|
|
|
|
!(ex instanceof FolderClosedException) &&
|
|
|
|
!(ex instanceof IllegalStateException) &&
|
|
|
|
!(ex instanceof StoreClosedException) &&
|
2019-03-25 17:46:15 +00:00
|
|
|
!(ex instanceof UnknownHostException) &&
|
2019-02-27 15:05:15 +00:00
|
|
|
!(ex instanceof MessageRemovedException) &&
|
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof UnknownHostException) &&
|
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof ConnectionException) &&
|
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof SocketException) &&
|
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof SocketTimeoutException) &&
|
|
|
|
!(ex instanceof MessagingException && ex.getCause() instanceof SSLException) &&
|
|
|
|
!(ex instanceof MessagingException && "connection failure".equals(ex.getMessage()))) {
|
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
nm.notify(tag, 1, getNotificationError(context, title, ex).build());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static NotificationCompat.Builder getNotificationError(Context context, String title, Throwable ex) {
|
|
|
|
return getNotificationError(context, "error", title, ex, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NotificationCompat.Builder getNotificationError(Context context, String channel, String title, Throwable ex, boolean debug) {
|
|
|
|
// Build pending intent
|
2019-03-19 07:05:00 +00:00
|
|
|
Intent intent = new Intent(context, ActivityView.class);
|
2019-02-27 15:05:15 +00:00
|
|
|
if (debug)
|
|
|
|
intent.setAction("error");
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
PendingIntent pi = PendingIntent.getActivity(
|
2019-03-19 07:05:00 +00:00
|
|
|
context, ActivityView.REQUEST_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// Build notification
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channel);
|
|
|
|
|
|
|
|
builder
|
|
|
|
.setSmallIcon(R.drawable.baseline_warning_white_24)
|
|
|
|
.setContentTitle(context.getString(R.string.title_notification_failed, title))
|
|
|
|
.setContentText(Helper.formatThrowable(ex))
|
|
|
|
.setContentIntent(pi)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(true)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setPriority(NotificationCompat.PRIORITY_MAX)
|
2019-02-27 15:05:15 +00:00
|
|
|
.setOnlyAlertOnce(true)
|
2019-03-07 12:29:03 +00:00
|
|
|
.setCategory(NotificationCompat.CATEGORY_ERROR)
|
2019-02-27 15:05:15 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
|
|
|
|
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(Helper.formatThrowable(ex, false, "\n")));
|
|
|
|
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class AlertException extends Throwable {
|
|
|
|
private String alert;
|
|
|
|
|
|
|
|
AlertException(String alert) {
|
|
|
|
this.alert = alert;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
|
|
|
return alert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class State {
|
2019-03-16 13:12:31 +00:00
|
|
|
private Helper.NetworkState networkState;
|
2019-02-27 15:05:15 +00:00
|
|
|
private Thread thread;
|
|
|
|
private Semaphore semaphore = new Semaphore(0);
|
|
|
|
private boolean running = true;
|
2019-04-10 18:17:58 +00:00
|
|
|
private boolean recoverable = true;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-03-16 13:12:31 +00:00
|
|
|
State(Helper.NetworkState networkState) {
|
|
|
|
this.networkState = networkState;
|
|
|
|
}
|
|
|
|
|
|
|
|
State(State parent) {
|
|
|
|
this(parent.networkState);
|
|
|
|
}
|
|
|
|
|
|
|
|
Helper.NetworkState getNetworkState() {
|
|
|
|
return networkState;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
void runnable(Runnable runnable, String name) {
|
|
|
|
thread = new Thread(runnable, name);
|
|
|
|
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
void release() {
|
|
|
|
semaphore.release();
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
|
|
|
void acquire() throws InterruptedException {
|
|
|
|
semaphore.acquire();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean acquire(long milliseconds) throws InterruptedException {
|
|
|
|
return semaphore.tryAcquire(milliseconds, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:17:58 +00:00
|
|
|
void error(Throwable ex) {
|
2019-04-11 07:31:02 +00:00
|
|
|
recoverable = (recoverable && !(ex instanceof FolderClosedException));
|
2019-02-27 15:05:15 +00:00
|
|
|
thread.interrupt();
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:17:58 +00:00
|
|
|
void reset() {
|
|
|
|
recoverable = true;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
private void yield() {
|
|
|
|
try {
|
|
|
|
// Give interrupted thread some time to acquire wake lock
|
|
|
|
Thread.sleep(YIELD_DURATION);
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void start() {
|
|
|
|
thread.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop() {
|
|
|
|
running = false;
|
|
|
|
semaphore.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void join() {
|
|
|
|
join(thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean running() {
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:17:58 +00:00
|
|
|
boolean recoverable() {
|
|
|
|
return recoverable;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
void join(Thread thread) {
|
|
|
|
boolean joined = false;
|
|
|
|
while (!joined)
|
|
|
|
try {
|
|
|
|
Log.i("Joining " + thread.getName());
|
|
|
|
thread.join();
|
|
|
|
joined = true;
|
|
|
|
Log.i("Joined " + thread.getName());
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(thread.getName() + " join " + ex.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "[running=" + running + "]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|