2019-02-27 15:05:15 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
2019-05-04 20:49:22 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
|
|
|
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
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;
|
|
|
|
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;
|
2019-07-09 19:07:56 +00:00
|
|
|
import android.util.Pair;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2019-05-10 13:59:50 +00:00
|
|
|
import com.bugsnag.android.BreadcrumbType;
|
|
|
|
import com.bugsnag.android.Bugsnag;
|
2019-04-29 20:05:12 +00:00
|
|
|
import com.sun.mail.iap.CommandFailedException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.iap.ConnectionException;
|
|
|
|
import com.sun.mail.iap.Response;
|
|
|
|
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 org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
2019-02-27 17:33:46 +00:00
|
|
|
import org.jsoup.Jsoup;
|
2019-07-06 12:18:14 +00:00
|
|
|
import org.jsoup.nodes.Element;
|
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;
|
2019-07-16 17:28:42 +00:00
|
|
|
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;
|
2019-07-06 08:16:42 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2019-07-09 05:48:57 +00:00
|
|
|
import java.util.regex.Pattern;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
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;
|
2019-07-06 12:18:14 +00:00
|
|
|
import javax.mail.Part;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Store;
|
|
|
|
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;
|
|
|
|
|
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;
|
2019-07-12 09:20:29 +00:00
|
|
|
import static androidx.core.app.NotificationCompat.DEFAULT_LIGHTS;
|
2019-07-15 18:39:24 +00:00
|
|
|
import static androidx.core.app.NotificationCompat.DEFAULT_SOUND;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
class Core {
|
2019-05-21 12:29:45 +00:00
|
|
|
private static int lastUnseen = -1;
|
|
|
|
|
2019-05-18 19:37:09 +00:00
|
|
|
private static final int MAX_NOTIFICATION_COUNT = 10; // per group
|
2019-07-09 19:07:56 +00:00
|
|
|
private static final int SYNC_CHUNCK_SIZE = 200;
|
2019-02-27 15:05:15 +00:00
|
|
|
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-06-23 11:23:29 +00:00
|
|
|
private static final long MIN_HIDE = 60 * 1000L; // milliseconds
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-07-24 06:42:37 +00:00
|
|
|
private static final ExecutorService executor =
|
|
|
|
Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
|
2019-07-06 08:16:42 +00:00
|
|
|
|
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 +
|
2019-04-28 07:01:53 +00:00
|
|
|
" folder=" + op.folder +
|
2019-02-27 15:05:15 +00:00
|
|
|
" msg=" + op.message +
|
|
|
|
" args=" + op.args);
|
|
|
|
|
2019-05-10 13:59:50 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("name", op.name);
|
|
|
|
crumb.put("args", op.args);
|
|
|
|
crumb.put("folder", folder.type);
|
2019-05-22 13:41:54 +00:00
|
|
|
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
|
2019-05-16 20:06:13 +00:00
|
|
|
Bugsnag.leaveBreadcrumb("operation", BreadcrumbType.LOG, crumb);
|
2019-05-10 13:59:50 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// 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 {
|
|
|
|
db.operation().setOperationError(op.id, null);
|
2019-06-14 08:42:54 +00:00
|
|
|
if (!EntityOperation.SYNC.equals(op.name))
|
|
|
|
db.operation().setOperationState(op.id, "executing");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 08:42:54 +00:00
|
|
|
if (message == null) {
|
|
|
|
if (!EntityOperation.SYNC.equals(op.name) &&
|
|
|
|
!EntityOperation.SUBSCRIBE.equals(op.name))
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
} else {
|
2019-06-14 08:41:34 +00:00
|
|
|
db.message().setMessageError(message.id, null);
|
2019-06-15 16:10:33 +00:00
|
|
|
ensureUid(context, folder, message, op, (IMAPFolder) ifolder);
|
2019-06-14 08:41:34 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-06-19 05:23:46 +00:00
|
|
|
if (next.message != null &&
|
|
|
|
next.message.equals(op.message) &&
|
2019-06-20 07:09:05 +00:00
|
|
|
(EntityOperation.ADD.equals(next.name) ||
|
|
|
|
EntityOperation.DELETE.equals(next.name))) {
|
2019-04-04 13:21:55 +00:00
|
|
|
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-04-25 16:47:52 +00:00
|
|
|
case EntityOperation.SUBSCRIBE:
|
|
|
|
onSubscribeFolder(context, jargs, folder, (IMAPFolder) ifolder);
|
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);
|
2019-06-26 14:51:29 +00:00
|
|
|
EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex, false));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-18 18:00:22 +00:00
|
|
|
db.operation().setOperationError(op.id, Helper.formatThrowable(ex));
|
2019-05-10 19:10:00 +00:00
|
|
|
if (message != null && !(ex instanceof IllegalArgumentException))
|
2019-06-23 18:56:07 +00:00
|
|
|
db.message().setMessageError(message.id, Helper.formatThrowable(ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-04-19 08:18:10 +00:00
|
|
|
if (ex instanceof OutOfMemoryError ||
|
|
|
|
ex instanceof MessageRemovedException ||
|
2019-02-27 15:05:15 +00:00
|
|
|
ex instanceof FolderNotFoundException ||
|
2019-04-29 20:05:12 +00:00
|
|
|
ex instanceof IllegalArgumentException ||
|
|
|
|
ex.getCause() instanceof CommandFailedException) {
|
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);
|
|
|
|
|
2019-04-30 10:21:26 +00:00
|
|
|
// Cleanup
|
|
|
|
if (EntityOperation.SYNC.equals(op.name))
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// 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);
|
2019-06-23 11:23:29 +00:00
|
|
|
db.message().setMessageUiHide(message.id, 0L);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw ex;
|
2019-05-11 19:04:27 +00:00
|
|
|
} finally {
|
|
|
|
db.operation().setOperationState(op.id, null);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end op=" + op.id + "/" + op.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end process state=" + state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
private static void ensureUid(Context context, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws MessagingException {
|
|
|
|
if (message.uid != null)
|
|
|
|
return;
|
|
|
|
if (EntityOperation.ADD.equals(op.name))
|
|
|
|
return;
|
2019-06-20 07:09:05 +00:00
|
|
|
if (EntityOperation.DELETE.equals(op.name) && !TextUtils.isEmpty(message.msgid))
|
|
|
|
return;
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
Log.i(folder.name + " ensure uid op=" + op.name + " msgid=" + message.msgid);
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
|
|
|
throw new IllegalArgumentException("Message without ID for " + op.name);
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
Message[] imessages = ifolder.search(new MessageIDTerm(message.msgid));
|
|
|
|
if (imessages == null || imessages.length == 0)
|
|
|
|
throw new IllegalArgumentException("Message not found for " + op.name);
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
long uid = -1;
|
|
|
|
for (Message iexisting : imessages) {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
Log.i(folder.name + " found uid=" + muid);
|
|
|
|
// RFC3501: Unique identifiers are assigned in a strictly ascending fashion
|
|
|
|
if (muid > uid)
|
|
|
|
uid = muid;
|
2019-06-14 08:41:34 +00:00
|
|
|
}
|
2019-06-15 16:10:33 +00:00
|
|
|
|
|
|
|
message.uid = uid;
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.message().setMessageUid(message.id, uid);
|
2019-06-14 08:41:34 +00:00
|
|
|
}
|
|
|
|
|
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-05-14 08:40:48 +00:00
|
|
|
// Get arguments
|
2019-06-23 08:28:33 +00:00
|
|
|
long target = jargs.optLong(0, folder.id);
|
|
|
|
boolean autoread = jargs.optBoolean(1, false);
|
2019-05-14 08:40:48 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
if (target != folder.id)
|
|
|
|
throw new IllegalArgumentException("Invalid folder");
|
2019-05-14 08:40:48 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Prevent async deletion
|
|
|
|
if (folder.id.equals(message.folder)) {
|
|
|
|
if (message.uid != null)
|
2019-06-13 19:43:00 +00:00
|
|
|
db.message().setMessageUid(message.id, null);
|
2019-06-14 06:31:37 +00:00
|
|
|
}
|
2019-04-19 11:33:41 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// External draft might have a uid only
|
|
|
|
if (TextUtils.isEmpty(message.msgid)) {
|
|
|
|
message.msgid = EntityMessage.generateMessageId();
|
|
|
|
db.message().setMessageMsgId(message.id, message.msgid);
|
|
|
|
}
|
2019-03-25 16:26:37 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Get raw message
|
|
|
|
MimeMessage imessage;
|
|
|
|
if (folder.id.equals(message.folder)) {
|
|
|
|
// Pre flight checks
|
|
|
|
if (!message.content)
|
|
|
|
throw new IllegalArgumentException("Message body missing");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
EntityIdentity identity =
|
|
|
|
(message.identity == null ? null : db.identity().getIdentity(message.identity));
|
2019-04-19 11:33:41 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
imessage = MessageHelper.from(context, message, identity, isession);
|
|
|
|
} else {
|
|
|
|
// Cross account move
|
|
|
|
File file = message.getRawFile(context);
|
|
|
|
if (!file.exists())
|
|
|
|
throw new IllegalArgumentException("raw message file not found");
|
2019-03-25 15:14:51 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
Log.i(folder.name + " reading " + file);
|
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
|
|
|
imessage = new MimeMessage(isession, is);
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 15:14:51 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Handle auto read
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Handle draft
|
2019-06-24 06:34:56 +00:00
|
|
|
if (ifolder.getPermanentFlags().contains(Flags.Flag.DRAFT))
|
|
|
|
imessage.setFlag(Flags.Flag.DRAFT, EntityFolder.DRAFTS.equals(folder.type));
|
2019-06-13 10:41:50 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Add message
|
|
|
|
ifolder.appendMessages(new Message[]{imessage});
|
|
|
|
|
|
|
|
if (folder.id.equals(message.folder)) {
|
|
|
|
// External draft might have a uid only
|
|
|
|
if (message.uid != null) {
|
|
|
|
Message iexisting = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (iexisting == null)
|
|
|
|
Log.w(folder.name + " existing not found uid=" + message.uid);
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " deleting uid=" + message.uid);
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
Log.w(folder.name + " existing gone uid=" + message.uid);
|
2019-04-19 11:33:41 +00:00
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
Log.i(folder.name + " searching for msgid=" + message.msgid);
|
|
|
|
Message[] imessages = ifolder.search(new MessageIDTerm(message.msgid));
|
|
|
|
if (imessages == null)
|
|
|
|
Log.w(folder.name + " search for msgid=" + message.msgid + " returned null");
|
|
|
|
else {
|
|
|
|
long uid = -1;
|
|
|
|
|
|
|
|
for (Message iexisting : imessages) {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
Log.i(folder.name + " found uid=" + muid);
|
|
|
|
// RFC3501: Unique identifiers are assigned in a strictly ascending fashion
|
|
|
|
if (muid > uid)
|
|
|
|
uid = muid;
|
2019-04-19 11:33:41 +00:00
|
|
|
}
|
2019-06-13 10:41:50 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
if (uid < 0)
|
|
|
|
Log.w(folder.name + " appended msgid=" + message.msgid + " not found");
|
|
|
|
else {
|
|
|
|
Log.i(folder.name + " appended uid=" + uid);
|
|
|
|
|
|
|
|
for (Message iexisting : imessages) {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
if (muid != uid)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " deleting uid=" + muid);
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
Log.w(folder.name + " existing gone uid=" + muid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-13 10:41:50 +00:00
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
|
|
|
|
ifolder.expunge();
|
|
|
|
} else {
|
|
|
|
// Mark source read
|
|
|
|
if (autoread)
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.SEEN, true);
|
|
|
|
|
|
|
|
// Delete source
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.DELETE);
|
2019-04-19 11:33:41 +00:00
|
|
|
}
|
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-06-23 08:28:33 +00:00
|
|
|
boolean autoread = jargs.optBoolean(1, false);
|
2019-03-14 11:08:19 +00:00
|
|
|
|
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-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-06-24 07:07:43 +00:00
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type) || EntityFolder.DRAFTS.equals(target.type)) {
|
|
|
|
Log.i(folder.name + " move from " + folder.type + " to " + target.type);
|
|
|
|
|
|
|
|
File file = message.getRawFile(context);
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
Message icopy;
|
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
|
|
|
icopy = new MimeMessage(isession, is);
|
|
|
|
}
|
|
|
|
|
|
|
|
file.delete();
|
|
|
|
|
2019-06-24 11:16:32 +00:00
|
|
|
// Auto read
|
|
|
|
if (autoread)
|
|
|
|
icopy.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
|
|
|
|
// Set drafts flag
|
2019-06-24 07:07:43 +00:00
|
|
|
icopy.setFlag(Flags.Flag.DRAFT, EntityFolder.DRAFTS.equals(target.type));
|
2019-06-24 11:16:32 +00:00
|
|
|
|
2019-06-24 07:07:43 +00:00
|
|
|
itarget.appendMessages(new Message[]{icopy});
|
2019-06-24 11:16:32 +00:00
|
|
|
} else {
|
|
|
|
// Auto read
|
|
|
|
if (autoread && ifolder.getPermanentFlags().contains(Flags.Flag.SEEN))
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, true);
|
|
|
|
|
2019-06-24 07:07:43 +00:00
|
|
|
ifolder.copyMessages(new Message[]{imessage}, itarget);
|
2019-06-24 11:16:32 +00:00
|
|
|
}
|
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-06-14 08:12:08 +00:00
|
|
|
if (message.uid != null) {
|
|
|
|
Message iexisting = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (iexisting == null)
|
|
|
|
Log.w(folder.name + " existing not found uid=" + message.uid);
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " deleting uid=" + message.uid);
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
Log.w(folder.name + " existing gone uid=" + message.uid);
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 08:12:08 +00:00
|
|
|
if (!TextUtils.isEmpty(message.msgid)) {
|
|
|
|
Message[] imessages = ifolder.search(new MessageIDTerm(message.msgid));
|
|
|
|
if (imessages == null)
|
|
|
|
Log.w(folder.name + " search for msgid=" + message.msgid + " returned null");
|
|
|
|
else
|
|
|
|
for (Message iexisting : imessages) {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
|
|
|
Log.i(folder.name + " deleting uid=" + muid);
|
|
|
|
try {
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
Log.w(folder.name + " existing gone uid=" + muid);
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2019-04-28 06:28:41 +00:00
|
|
|
// Cross account move
|
2019-06-14 06:31:37 +00:00
|
|
|
long target = jargs.getLong(0);
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target);
|
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
2019-06-13 11:43:34 +00:00
|
|
|
operation.account = message.account;
|
2019-02-27 15:05:15 +00:00
|
|
|
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-05-04 18:52:21 +00:00
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
parts.isPlainOnly(),
|
|
|
|
HtmlHelper.getPreview(body),
|
|
|
|
parts.getWarnings(message.warning));
|
2019-07-06 12:18:14 +00:00
|
|
|
|
2019-07-06 16:33:21 +00:00
|
|
|
if (!TextUtils.isEmpty(body))
|
|
|
|
fixAttachments(context, message.id, body);
|
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-05-08 08:32:37 +00:00
|
|
|
long id = jargs.getLong(0);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// Get attachment
|
2019-06-26 19:21:09 +00:00
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id);
|
|
|
|
if (attachment == null)
|
|
|
|
attachment = db.attachment().getAttachment(message.id, (int) id); // legacy
|
|
|
|
if (attachment == null)
|
2019-05-16 18:44:42 +00:00
|
|
|
throw new IllegalArgumentException("Local attachment not found");
|
2019-06-26 19:21:09 +00:00
|
|
|
if (attachment.available)
|
2019-02-27 15:05:15 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Get message
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2019-05-12 13:29:18 +00:00
|
|
|
// Get message parts
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
2019-06-26 19:21:09 +00:00
|
|
|
// Download attachment
|
|
|
|
parts.downloadAttachment(context, attachment);
|
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);
|
|
|
|
|
2019-05-23 08:02:43 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-05-23 17:26:07 +00:00
|
|
|
boolean subscribed_only = prefs.getBoolean("subscribed_only", false);
|
2019-06-08 14:33:25 +00:00
|
|
|
boolean sync_folders = prefs.getBoolean("sync_folders", true);
|
2019-04-04 16:18:13 +00:00
|
|
|
|
2019-05-01 07:23:31 +00:00
|
|
|
// Get folder names
|
2019-06-25 14:34:30 +00:00
|
|
|
Map<String, EntityFolder> local = new HashMap<>();
|
2019-07-07 07:25:52 +00:00
|
|
|
for (EntityFolder folder : db.folder().getFolders(account.id, false, false))
|
2019-05-01 07:23:31 +00:00
|
|
|
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);
|
2019-06-25 14:34:30 +00:00
|
|
|
local.put(folder.name, folder);
|
2019-06-08 14:33:25 +00:00
|
|
|
sync_folders = true;
|
2019-05-01 07:23:31 +00:00
|
|
|
} 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);
|
2019-06-08 14:33:25 +00:00
|
|
|
sync_folders = true;
|
|
|
|
} else {
|
2019-06-25 14:34:30 +00:00
|
|
|
local.put(folder.name, folder);
|
2019-06-24 10:45:52 +00:00
|
|
|
if (folder.initialize != 0)
|
2019-06-08 14:33:25 +00:00
|
|
|
sync_folders = true;
|
|
|
|
}
|
2019-06-25 14:34:30 +00:00
|
|
|
Log.i("Local folder count=" + local.size());
|
2019-04-04 16:18:13 +00:00
|
|
|
|
2019-06-08 14:33:25 +00:00
|
|
|
if (!sync_folders)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Log.i("Start sync folders account=" + account.name);
|
|
|
|
|
2019-05-25 14:21:25 +00:00
|
|
|
// Get default folder
|
|
|
|
Folder defaultFolder = istore.getDefaultFolder();
|
|
|
|
char separator = defaultFolder.getSeparator();
|
|
|
|
EntityLog.log(context, account.name + " folder separator=" + separator);
|
2019-06-26 06:13:39 +00:00
|
|
|
db.account().setFolderSeparator(account.id, separator);
|
2019-05-25 14:21:25 +00:00
|
|
|
|
|
|
|
// Get remote folders
|
2019-05-26 12:07:10 +00:00
|
|
|
long start = new Date().getTime();
|
2019-05-25 14:21:25 +00:00
|
|
|
Folder[] ifolders = (subscribed_only
|
|
|
|
? defaultFolder.listSubscribed("*")
|
|
|
|
: defaultFolder.list("*"));
|
2019-05-26 12:07:10 +00:00
|
|
|
|
|
|
|
// Get subscribed folders
|
|
|
|
List<String> subscription = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
Folder[] isubscribed = (subscribed_only ? ifolders : defaultFolder.listSubscribed("*"));
|
|
|
|
for (Folder ifolder : isubscribed)
|
|
|
|
subscription.add(ifolder.getFullName());
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
2019-05-25 14:21:25 +00:00
|
|
|
if (subscribed_only && ifolders.length == 0) {
|
|
|
|
Log.i("No subscribed folders");
|
|
|
|
ifolders = defaultFolder.list("*");
|
|
|
|
}
|
2019-05-26 12:07:10 +00:00
|
|
|
long duration = new Date().getTime() - start;
|
2019-05-25 14:21:25 +00:00
|
|
|
|
2019-05-26 12:07:10 +00:00
|
|
|
Log.i("Remote folder count=" + ifolders.length +
|
|
|
|
" subscribed=" + subscription.size() +
|
|
|
|
" separator=" + separator +
|
|
|
|
" fetched in " + duration + " ms");
|
2019-05-25 14:21:25 +00:00
|
|
|
|
2019-05-22 12:59:21 +00:00
|
|
|
Map<String, EntityFolder> nameFolder = new HashMap<>();
|
|
|
|
Map<String, List<EntityFolder>> parentFolders = new HashMap<>();
|
|
|
|
for (Folder ifolder : ifolders) {
|
|
|
|
String fullName = ifolder.getFullName();
|
2019-07-09 05:48:57 +00:00
|
|
|
String[] name = fullName.split(Pattern.quote(Character.toString(separator)));
|
2019-05-28 11:42:19 +00:00
|
|
|
String childName = name[name.length - 1];
|
2019-05-26 12:07:10 +00:00
|
|
|
boolean subscribed = subscription.contains(fullName);
|
2019-05-22 12:59:21 +00:00
|
|
|
String[] attr = ((IMAPFolder) ifolder).getAttributes();
|
2019-07-07 06:17:27 +00:00
|
|
|
String type = EntityFolder.getType(attr, fullName, false);
|
|
|
|
boolean selectable = !Arrays.asList(attr).contains("\\Noselect");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-28 11:42:19 +00:00
|
|
|
if (EntityFolder.INBOX.equals(type))
|
|
|
|
childName = null;
|
|
|
|
|
2019-05-23 08:02:43 +00:00
|
|
|
Log.i(account.name + ":" + fullName + " subscribed=" + subscribed +
|
|
|
|
" type=" + type + " attrs=" + TextUtils.join(" ", attr));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-22 12:59:21 +00:00
|
|
|
if (type != null) {
|
2019-06-25 14:34:30 +00:00
|
|
|
local.remove(fullName);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-22 12:59:21 +00:00
|
|
|
EntityFolder folder;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
folder = db.folder().getFolderByName(account.id, fullName);
|
2019-02-27 15:05:15 +00:00
|
|
|
if (folder == null) {
|
2019-05-23 16:13:03 +00:00
|
|
|
folder = new EntityFolder();
|
|
|
|
folder.account = account.id;
|
|
|
|
folder.name = fullName;
|
2019-05-28 11:42:19 +00:00
|
|
|
folder.display = childName;
|
2019-05-23 16:13:03 +00:00
|
|
|
folder.type = (EntityFolder.SYSTEM.equals(type) ? type : EntityFolder.USER);
|
|
|
|
folder.synchronize = false;
|
|
|
|
folder.subscribed = subscribed;
|
|
|
|
folder.poll = ("imap.gmail.com".equals(account.host));
|
|
|
|
folder.sync_days = EntityFolder.DEFAULT_SYNC;
|
|
|
|
folder.keep_days = EntityFolder.DEFAULT_KEEP;
|
2019-07-07 06:17:27 +00:00
|
|
|
folder.selectable = selectable;
|
2019-05-23 16:13:03 +00:00
|
|
|
folder.id = db.folder().insertFolder(folder);
|
|
|
|
Log.i(folder.name + " added type=" + folder.type);
|
2019-02-27 15:05:15 +00:00
|
|
|
} else {
|
2019-05-23 16:13:03 +00:00
|
|
|
Log.i(folder.name + " exists type=" + folder.type);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
if (folder.subscribed == null || !folder.subscribed.equals(subscribed))
|
|
|
|
db.folder().setFolderSubscribed(folder.id, subscribed);
|
2019-04-25 15:23:56 +00:00
|
|
|
|
2019-05-28 11:42:19 +00:00
|
|
|
if (folder.display == null && childName != null)
|
|
|
|
db.folder().setFolderDisplay(folder.id, childName);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
// Compatibility
|
2019-05-28 11:42:19 +00:00
|
|
|
if (EntityFolder.USER.equals(folder.type) && EntityFolder.SYSTEM.equals(type))
|
2019-05-23 16:13:03 +00:00
|
|
|
db.folder().setFolderType(folder.id, type);
|
|
|
|
else if (EntityFolder.SYSTEM.equals(folder.type) && EntityFolder.USER.equals(type))
|
|
|
|
db.folder().setFolderType(folder.id, type);
|
|
|
|
else if (EntityFolder.INBOX.equals(type) && !EntityFolder.INBOX.equals(folder.type)) {
|
|
|
|
if (db.folder().getFolderByType(folder.account, EntityFolder.INBOX) == null)
|
2019-03-03 16:48:39 +00:00
|
|
|
db.folder().setFolderType(folder.id, type);
|
2019-05-23 16:13:03 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-05-22 12:59:21 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
Log.i("End sync folder");
|
2019-03-17 19:32:57 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 16:13:03 +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);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-05-22 12:59:21 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-05-22 12:59:21 +00:00
|
|
|
Log.i("Updating folder parents=" + parentFolders.size());
|
|
|
|
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
|
|
|
|
2019-06-25 14:34:30 +00:00
|
|
|
Log.i("Delete local count=" + local.size());
|
|
|
|
for (String name : local.keySet()) {
|
|
|
|
EntityFolder folder = local.get(name);
|
|
|
|
if (EntityFolder.USER.equals(folder.type)) {
|
|
|
|
Log.i(name + " delete");
|
|
|
|
db.folder().deleteFolder(account.id, name);
|
|
|
|
} else
|
|
|
|
Log.i(name + " keep type=" + folder.type);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 16:47:52 +00:00
|
|
|
private static void onSubscribeFolder(Context context, JSONArray jargs, EntityFolder folder, IMAPFolder ifolder)
|
|
|
|
throws JSONException, MessagingException {
|
|
|
|
boolean subscribe = jargs.getBoolean(0);
|
|
|
|
ifolder.setSubscribed(subscribe);
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.folder().setFolderSubscribed(folder.id, subscribe);
|
|
|
|
|
|
|
|
Log.i(folder.name + " subscribed=" + subscribe);
|
|
|
|
}
|
|
|
|
|
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);
|
2019-06-23 08:28:33 +00:00
|
|
|
boolean download = jargs.optBoolean(2, false);
|
|
|
|
boolean auto_delete = jargs.optBoolean(3, false);
|
2019-07-19 05:11:05 +00:00
|
|
|
int initialize = jargs.optInt(4, folder.initialize);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-24 10:45:52 +00:00
|
|
|
if (keep_days == sync_days && keep_days != Integer.MAX_VALUE)
|
2019-02-27 15:05:15 +00:00
|
|
|
keep_days++;
|
|
|
|
|
2019-05-06 13:25:11 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean sync_unseen = prefs.getBoolean("sync_unseen", false);
|
|
|
|
boolean sync_flagged = prefs.getBoolean("sync_flagged", true);
|
2019-06-15 11:14:41 +00:00
|
|
|
boolean sync_kept = prefs.getBoolean("sync_kept", true);
|
2019-07-06 16:13:38 +00:00
|
|
|
boolean delete_unseen = prefs.getBoolean("delete_unseen", false);
|
2019-05-06 13:25:11 +00:00
|
|
|
|
|
|
|
Log.i(folder.name + " start sync after=" + sync_days + "/" + keep_days +
|
2019-07-06 16:13:38 +00:00
|
|
|
" sync unseen=" + sync_unseen + " flagged=" + sync_flagged +
|
|
|
|
" delete unseen=" + delete_unseen + " kept=" + sync_kept);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
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-07-08 07:19:16 +00:00
|
|
|
if (auto_delete) {
|
2019-07-06 16:13:38 +00:00
|
|
|
List<Long> tbds = db.message().getMessagesBefore(folder.id, keep_time, delete_unseen);
|
2019-05-01 09:26:32 +00:00
|
|
|
Log.i(folder.name + " local tbd=" + tbds.size());
|
2019-07-08 07:19:16 +00:00
|
|
|
EntityFolder trash = db.folder().getFolderByType(folder.account, EntityFolder.TRASH);
|
2019-05-01 09:26:32 +00:00
|
|
|
for (Long tbd : tbds) {
|
|
|
|
EntityMessage message = db.message().getMessage(tbd);
|
2019-07-08 07:19:16 +00:00
|
|
|
if (message != null && trash != null)
|
|
|
|
if (EntityFolder.TRASH.equals(folder.type))
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.DELETE);
|
|
|
|
else
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, trash.id);
|
2019-05-01 09:26:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-07-06 16:13:38 +00:00
|
|
|
int old = db.message().deleteMessagesBefore(folder.id, keep_time, delete_unseen);
|
2019-05-01 09:26:32 +00:00
|
|
|
Log.i(folder.name + " local old=" + old);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
// Get list of local uids
|
2019-06-02 06:26:12 +00:00
|
|
|
final List<Long> uids = db.message().getUids(folder.id, sync_kept ? null : sync_time);
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " local count=" + uids.size());
|
|
|
|
|
|
|
|
// Reduce list of local uids
|
|
|
|
SearchTerm searchTerm = new ReceivedDateTerm(ComparisonTerm.GE, new Date(sync_time));
|
2019-05-06 13:25:11 +00:00
|
|
|
if (sync_unseen)
|
|
|
|
searchTerm = new OrTerm(searchTerm, new FlagTerm(new Flags(Flags.Flag.SEEN), false));
|
|
|
|
if (sync_flagged && ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED))
|
2019-02-27 15:05:15 +00:00
|
|
|
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) {
|
2019-05-05 11:11:19 +00:00
|
|
|
Log.w(ex.getMessage());
|
|
|
|
// Fallback to date only search
|
|
|
|
imessages = ifolder.search(new ReceivedDateTerm(ComparisonTerm.GE, new Date(sync_time)));
|
2019-03-30 17:45:54 +00:00
|
|
|
}
|
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();
|
2019-05-17 15:41:23 +00:00
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID); // To check if message exists
|
|
|
|
fp.add(FetchProfile.Item.FLAGS); // To update existing messages
|
2019-02-27 15:05:15 +00:00
|
|
|
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);
|
2019-06-26 14:51:29 +00:00
|
|
|
EntityLog.log(context, folder.name + " " + Helper.formatThrowable(ex, false));
|
2019-06-23 18:56:07 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uids.size() > 0) {
|
2019-07-09 19:07:56 +00:00
|
|
|
// This is done outside of JavaMail to prevent changed notifications
|
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) {
|
2019-07-09 19:07:56 +00:00
|
|
|
// Build ranges
|
|
|
|
List<Pair<Long, Long>> ranges = new ArrayList<>();
|
|
|
|
long first = -1;
|
|
|
|
long last = -1;
|
|
|
|
for (long uid : uids)
|
|
|
|
if (first < 0)
|
|
|
|
first = uid;
|
|
|
|
else if ((last < 0 ? first : last) + 1 == uid)
|
|
|
|
last = uid;
|
|
|
|
else {
|
|
|
|
ranges.add(new Pair<>(first, last < 0 ? first : last));
|
|
|
|
first = uid;
|
|
|
|
last = -1;
|
|
|
|
}
|
|
|
|
if (first > 0)
|
|
|
|
ranges.add(new Pair<>(first, last < 0 ? first : last));
|
|
|
|
|
|
|
|
List<List<Pair<Long, Long>>> chunks = Helper.chunkList(ranges, SYNC_CHUNCK_SIZE);
|
|
|
|
|
|
|
|
Log.i(folder.name + " executing uid fetch count=" + uids.size() +
|
|
|
|
" ranges=" + ranges.size() + " chunks=" + chunks.size());
|
|
|
|
for (int c = 0; c < chunks.size(); c++) {
|
|
|
|
List<Pair<Long, Long>> chunk = chunks.get(c);
|
|
|
|
Log.i(folder.name + " chunk #" + c + " size=" + chunk.size());
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (Pair<Long, Long> range : chunk) {
|
|
|
|
if (sb.length() > 0)
|
|
|
|
sb.append(',');
|
|
|
|
if (range.first.equals(range.second))
|
|
|
|
sb.append(range.first);
|
|
|
|
else
|
|
|
|
sb.append(range.first).append(':').append(range.second);
|
|
|
|
}
|
|
|
|
Response[] responses = protocol.command("UID FETCH " + sb + " (UID)", null);
|
2019-05-22 11:17:42 +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);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (Response response : responses)
|
2019-06-01 11:17:32 +00:00
|
|
|
if (response.isBYE())
|
|
|
|
return new MessagingException("UID FETCH", new IOException(response.toString()));
|
|
|
|
else if (response.isNO() || response.isBAD())
|
2019-05-22 11:17:42 +00:00
|
|
|
return new MessagingException(response.toString());
|
|
|
|
return new MessagingException("UID FETCH failed");
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-07-09 19:07:56 +00:00
|
|
|
|
2019-05-22 11:17:42 +00:00
|
|
|
return null;
|
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-05-22 13:41:54 +00:00
|
|
|
int free = Log.getFreeMemMb();
|
2019-05-16 20:06:13 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("start", Integer.toString(from));
|
|
|
|
crumb.put("end", Integer.toString(i));
|
|
|
|
crumb.put("free", Integer.toString(free));
|
|
|
|
Bugsnag.leaveBreadcrumb("sync", BreadcrumbType.LOG, crumb);
|
|
|
|
Log.i("Sync " + from + ".." + i + " free=" + free);
|
|
|
|
|
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-07-15 10:54:53 +00:00
|
|
|
false, download,
|
2019-07-15 10:27:58 +00:00
|
|
|
rules, state);
|
2019-02-27 15:05:15 +00:00
|
|
|
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);
|
2019-06-23 18:56:07 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
} else
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
2019-06-23 18:56:07 +00:00
|
|
|
db.folder().setFolderError(folder.id, Helper.formatThrowable(ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
} finally {
|
2019-05-17 15:41:23 +00:00
|
|
|
// Free memory
|
2019-02-27 15:05:15 +00:00
|
|
|
((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);
|
2019-05-18 06:49:20 +00:00
|
|
|
EntityOperation.queue(context, orphan, EntityOperation.ADD);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 08:20:46 +00:00
|
|
|
int count = ifolder.getMessageCount();
|
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
|
|
|
|
2019-07-19 05:11:05 +00:00
|
|
|
if (download && initialize == 0) {
|
2019-02-27 15:05:15 +00:00
|
|
|
db.folder().setFolderSyncState(folder.id, "downloading");
|
|
|
|
|
|
|
|
// 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-05-22 13:41:54 +00:00
|
|
|
int free = Log.getFreeMemMb();
|
2019-05-16 20:06:13 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("start", Integer.toString(from));
|
|
|
|
crumb.put("end", Integer.toString(i));
|
|
|
|
crumb.put("free", Integer.toString(free));
|
|
|
|
Bugsnag.leaveBreadcrumb("download", BreadcrumbType.LOG, crumb);
|
|
|
|
Log.i("Download " + from + ".." + i + " free=" + free);
|
|
|
|
|
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 (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
} finally {
|
|
|
|
// Free memory
|
|
|
|
((IMAPMessage) isub[j]).invalidateHeaders();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 05:11:05 +00:00
|
|
|
if (state.running && initialize != 0) {
|
|
|
|
jargs.put(4, 0);
|
2019-06-24 10:45:52 +00:00
|
|
|
folder.initialize = 0;
|
|
|
|
db.folder().setFolderInitialize(folder.id, 0);
|
2019-07-11 09:51:21 +00:00
|
|
|
|
|
|
|
// Schedule download
|
|
|
|
if (download) {
|
|
|
|
EntityOperation operation = new EntityOperation();
|
|
|
|
operation.account = folder.account;
|
|
|
|
operation.folder = folder.id;
|
|
|
|
operation.message = null;
|
|
|
|
operation.name = EntityOperation.SYNC;
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
}
|
2019-05-22 07:33:25 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
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-07-15 10:54:53 +00:00
|
|
|
boolean browsed, boolean download,
|
2019-07-15 10:27:58 +00:00
|
|
|
List<EntityRule> rules, State state) throws MessagingException, IOException {
|
2019-06-14 09:25:56 +00:00
|
|
|
long uid = ifolder.getUID(imessage);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (imessage.isExpunged()) {
|
|
|
|
Log.i(folder.name + " expunged uid=" + uid);
|
2019-06-25 14:43:23 +00:00
|
|
|
throw new MessageRemovedException("Expunged");
|
2019-06-14 09:25:56 +00:00
|
|
|
}
|
|
|
|
if (imessage.isSet(Flags.Flag.DELETED)) {
|
|
|
|
Log.i(folder.name + " deleted uid=" + uid);
|
2019-06-25 14:43:23 +00:00
|
|
|
throw new MessageRemovedException("Flagged deleted");
|
2019-06-14 09:25:56 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +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 process = false;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2019-07-16 12:44:21 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +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-07-15 11:48:27 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (dup.size == null)
|
|
|
|
dup.size = helper.getSize();
|
2019-07-15 11:48:27 +00:00
|
|
|
|
|
|
|
if (EntityFolder.OUTBOX.equals(dfolder.type)) {
|
|
|
|
dup.received = helper.getReceived();
|
|
|
|
dup.sent = helper.getSent();
|
|
|
|
}
|
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
dup.error = null;
|
|
|
|
|
|
|
|
message = dup;
|
|
|
|
process = true;
|
|
|
|
} else if (dup.uid < 0)
|
|
|
|
throw new MessageRemovedException();
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-06-14 09:25:56 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (message == null) {
|
|
|
|
String authentication = helper.getAuthentication();
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.uid = uid;
|
2019-06-13 18:45:35 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
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);
|
|
|
|
message.receipt_request = helper.getReceiptRequested();
|
|
|
|
message.receipt_to = helper.getReceiptTo();
|
|
|
|
message.dkim = MessageHelper.getAuthentication("dkim", authentication);
|
|
|
|
message.spf = MessageHelper.getAuthentication("spf", authentication);
|
|
|
|
message.dmarc = MessageHelper.getAuthentication("dmarc", authentication);
|
|
|
|
message.from = helper.getFrom();
|
|
|
|
message.to = helper.getTo();
|
|
|
|
message.cc = helper.getCc();
|
|
|
|
message.bcc = helper.getBcc();
|
|
|
|
message.reply = helper.getReply();
|
|
|
|
message.list_post = helper.getListPost();
|
|
|
|
message.subject = helper.getSubject();
|
|
|
|
message.size = helper.getSize();
|
|
|
|
message.content = false;
|
|
|
|
message.received = helper.getReceived();
|
|
|
|
message.sent = helper.getSent();
|
|
|
|
message.seen = seen;
|
|
|
|
message.answered = answered;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.flags = flags;
|
|
|
|
message.keywords = keywords;
|
|
|
|
message.ui_seen = seen;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
message.ui_flagged = flagged;
|
2019-06-23 11:23:29 +00:00
|
|
|
message.ui_hide = 0L;
|
2019-06-14 09:25:56 +00:00
|
|
|
message.ui_found = false;
|
|
|
|
message.ui_ignored = seen;
|
|
|
|
message.ui_browsed = browsed;
|
|
|
|
|
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
message.identity = (identity == null ? null : identity.id);
|
|
|
|
|
|
|
|
message.sender = MessageHelper.getSortKey(message.from);
|
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(context, message.from);
|
|
|
|
message.avatar = (lookupUri == null ? null : lookupUri.toString());
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-07-16 12:44:21 +00:00
|
|
|
boolean check_mx = prefs.getBoolean("check_mx", false);
|
|
|
|
if (check_mx)
|
|
|
|
try {
|
2019-07-16 17:28:42 +00:00
|
|
|
if (ConnectionHelper.lookupMx(
|
2019-07-16 12:44:21 +00:00
|
|
|
message.reply == null || message.reply.length == 0
|
2019-07-16 17:28:42 +00:00
|
|
|
? message.from : message.reply, context))
|
|
|
|
message.mx = true;
|
|
|
|
} catch (UnknownHostException ex) {
|
2019-07-16 12:44:21 +00:00
|
|
|
message.mx = false;
|
2019-07-16 17:28:42 +00:00
|
|
|
message.warning = ex.getMessage();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
message.warning = Helper.formatThrowable(ex, false);
|
2019-07-16 12:44:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-14 19:34:43 +00:00
|
|
|
/*
|
2019-04-21 05:32:54 +00:00
|
|
|
// Authentication is more reliable
|
2019-03-31 18:54:01 +00:00
|
|
|
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-07-14 19:34:43 +00:00
|
|
|
*/
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-05-01 07:26:39 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
// Check if message was added in the meantime
|
|
|
|
EntityMessage existing = db.message().getMessageByUid(message.folder, message.uid);
|
|
|
|
if (existing != null) {
|
|
|
|
Log.i("Message was already added");
|
|
|
|
return existing;
|
2019-06-13 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 09:25:56 +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-31 18:54:01 +00:00
|
|
|
}
|
2019-03-30 13:49:51 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
runRules(context, imessage, message, rules);
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (message.received > account.created)
|
|
|
|
updateContactInfo(context, folder, message);
|
2019-07-14 19:34:43 +00:00
|
|
|
|
|
|
|
// Download small messages inline
|
2019-07-15 10:54:53 +00:00
|
|
|
if (download && message.size != null) {
|
2019-07-15 10:27:58 +00:00
|
|
|
long maxSize;
|
|
|
|
if (state == null || state.networkState.isUnmetered())
|
|
|
|
maxSize = MessageHelper.SMALL_MESSAGE_SIZE;
|
|
|
|
else {
|
2019-07-15 10:54:53 +00:00
|
|
|
int downloadSize = prefs.getInt("download", 0);
|
|
|
|
maxSize = (downloadSize == 0
|
2019-07-15 10:27:58 +00:00
|
|
|
? MessageHelper.SMALL_MESSAGE_SIZE
|
2019-07-15 10:54:53 +00:00
|
|
|
: Math.min(downloadSize, MessageHelper.SMALL_MESSAGE_SIZE));
|
2019-07-15 10:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (message.size < maxSize) {
|
|
|
|
String body = parts.getHtml(context);
|
|
|
|
Helper.writeText(message.getFile(context), body);
|
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
parts.isPlainOnly(),
|
|
|
|
HtmlHelper.getPreview(body),
|
|
|
|
parts.getWarnings(message.warning));
|
|
|
|
Log.i(folder.name + " inline downloaded message id=" + message.id +
|
|
|
|
" size=" + message.size + "/" + (body == null ? null : body.length()));
|
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(body))
|
|
|
|
fixAttachments(context, message.id, body);
|
|
|
|
}
|
2019-07-14 19:34:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
} else {
|
|
|
|
if (process) {
|
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
if (identity != null) {
|
|
|
|
message.identity = identity.id;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " identity=" + identity.id);
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-06-14 09:25:56 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
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-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +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);
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-06-14 09:25:56 +00:00
|
|
|
update = true;
|
|
|
|
message.answered = answered;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " answered=" + answered);
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (!Objects.equals(flags, message.flags)) {
|
|
|
|
update = true;
|
|
|
|
message.flags = flags;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " flags=" + flags);
|
|
|
|
}
|
2019-05-06 17:19:37 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
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));
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 11:23:29 +00:00
|
|
|
if (message.ui_hide != 0 && message.ui_hide + MIN_HIDE < new Date().getTime() &&
|
2019-06-23 07:50:30 +00:00
|
|
|
db.operation().getOperationCount(folder.id, message.id) == 0) {
|
2019-06-14 09:25:56 +00:00
|
|
|
update = true;
|
2019-06-23 11:23:29 +00:00
|
|
|
message.ui_hide = 0L;
|
2019-06-14 09:25:56 +00:00
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " unhide");
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 10:37:26 +00:00
|
|
|
if (message.ui_browsed != browsed) {
|
2019-06-14 09:25:56 +00:00
|
|
|
update = true;
|
2019-06-23 10:37:26 +00:00
|
|
|
message.ui_browsed = browsed;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " browsed=" + browsed);
|
2019-06-14 09:25:56 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
Uri uri = ContactInfo.getLookupUri(context, message.from);
|
|
|
|
String avatar = (uri == null ? null : uri.toString());
|
|
|
|
if (!Objects.equals(message.avatar, avatar)) {
|
|
|
|
update = true;
|
|
|
|
message.avatar = avatar;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " avatar=" + avatar);
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (update || process)
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-06-13 18:45:35 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
db.message().updateMessage(message);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (process)
|
|
|
|
runRules(context, imessage, message, rules);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (process)
|
|
|
|
updateContactInfo(context, folder, message);
|
|
|
|
|
|
|
|
else if (BuildConfig.DEBUG)
|
|
|
|
Log.i(folder.name + " unchanged uid=" + uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> fkeywords = new ArrayList<>(Arrays.asList(folder.keywords));
|
|
|
|
|
|
|
|
for (String keyword : keywords)
|
|
|
|
if (!fkeywords.contains(keyword)) {
|
|
|
|
Log.i(folder.name + " adding keyword=" + keyword);
|
|
|
|
fkeywords.add(keyword);
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
if (folder.keywords.length != fkeywords.size()) {
|
|
|
|
Collections.sort(fkeywords);
|
|
|
|
db.folder().setFolderKeywords(folder.id, DB.Converters.fromStringArray(fkeywords.toArray(new String[0])));
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-06-14 09:25:56 +00:00
|
|
|
|
|
|
|
return message;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
private static EntityIdentity matchIdentity(Context context, EntityFolder folder, EntityMessage message) {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
List<Address> addresses = new ArrayList<>();
|
|
|
|
if (folder.isOutgoing()) {
|
|
|
|
if (message.from != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.from));
|
|
|
|
} else {
|
|
|
|
if (message.to != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.to));
|
|
|
|
if (message.cc != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.cc));
|
|
|
|
if (EntityFolder.ARCHIVE.equals(folder.type) || BuildConfig.DEBUG) {
|
|
|
|
if (message.from != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.from));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for matching identity
|
|
|
|
for (Address address : addresses) {
|
|
|
|
String email = ((InternetAddress) address).getAddress();
|
|
|
|
if (!TextUtils.isEmpty(email)) {
|
|
|
|
EntityIdentity ident = db.identity().getIdentity(folder.account, email);
|
|
|
|
if (ident != null)
|
|
|
|
return ident;
|
|
|
|
|
|
|
|
String canonical = MessageHelper.canonicalAddress(email);
|
|
|
|
if (canonical.equals(email))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ident = db.identity().getIdentity(folder.account, canonical);
|
|
|
|
if (ident != null)
|
|
|
|
return ident;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-06-13 10:41:50 +00:00
|
|
|
private static void runRules(Context context, Message imessage, EntityMessage message, List<EntityRule> rules) {
|
2019-03-31 06:48:42 +00:00
|
|
|
if (!Helper.isPro(context))
|
|
|
|
return;
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
for (EntityRule rule : rules)
|
|
|
|
if (rule.matches(context, message, imessage)) {
|
2019-06-07 15:32:30 +00:00
|
|
|
rule.execute(context, message);
|
2019-03-31 06:48:42 +00:00
|
|
|
if (rule.stop)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
db.message().setMessageError(message.id, Helper.formatThrowable(ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-13 11:25:57 +00:00
|
|
|
private static void updateContactInfo(Context context, final EntityFolder folder, final EntityMessage message) {
|
|
|
|
final DB db = DB.getInstance(context);
|
2019-03-30 13:57:06 +00:00
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
if (EntityFolder.ARCHIVE.equals(folder.type) ||
|
|
|
|
EntityFolder.TRASH.equals(folder.type) ||
|
|
|
|
EntityFolder.JUNK.equals(folder.type))
|
|
|
|
return;
|
|
|
|
|
2019-05-13 11:25:57 +00:00
|
|
|
final int type = (folder.isOutgoing() ? EntityContact.TYPE_TO : EntityContact.TYPE_FROM);
|
2019-03-30 13:57:06 +00:00
|
|
|
Address[] recipients = (type == EntityContact.TYPE_TO
|
|
|
|
? message.to
|
|
|
|
: (message.reply != null ? message.reply : message.from));
|
2019-04-24 10:17:32 +00:00
|
|
|
|
|
|
|
// Check if from self
|
|
|
|
if (type == EntityContact.TYPE_FROM && recipients != null && recipients.length > 0) {
|
|
|
|
boolean me = true;
|
|
|
|
for (Address reply : recipients) {
|
|
|
|
String email = ((InternetAddress) reply).getAddress();
|
2019-05-12 16:22:41 +00:00
|
|
|
String canonical = MessageHelper.canonicalAddress(email);
|
2019-04-24 10:17:32 +00:00
|
|
|
if (!TextUtils.isEmpty(email) &&
|
|
|
|
db.identity().getIdentity(folder.account, email) == null &&
|
|
|
|
(canonical.equals(email) ||
|
|
|
|
db.identity().getIdentity(folder.account, canonical) == null)) {
|
|
|
|
me = false;
|
|
|
|
break;
|
2019-03-30 13:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-24 10:17:32 +00:00
|
|
|
if (me)
|
|
|
|
recipients = message.to;
|
|
|
|
}
|
2019-03-30 13:57:06 +00:00
|
|
|
|
2019-04-24 10:17:32 +00:00
|
|
|
if (recipients != null) {
|
2019-03-30 13:57:06 +00:00
|
|
|
for (Address recipient : recipients) {
|
2019-05-13 11:25:57 +00:00
|
|
|
final String email = ((InternetAddress) recipient).getAddress();
|
|
|
|
final String name = ((InternetAddress) recipient).getPersonal();
|
|
|
|
final Uri avatar = ContactInfo.getLookupUri(context, new Address[]{recipient});
|
|
|
|
db.runInTransaction(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
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-03-30 13:57:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-19 16:54:40 +00:00
|
|
|
if (state.getNetworkState().isRoaming())
|
|
|
|
return;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
2019-05-17 15:41:23 +00:00
|
|
|
|
|
|
|
// Fetch on demand to prevent OOM
|
|
|
|
|
|
|
|
//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);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-07-14 19:34:43 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage);
|
2019-02-27 15:05:15 +00:00
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
if (!message.content) {
|
2019-07-14 19:34:43 +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-05-04 18:52:21 +00:00
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
parts.isPlainOnly(),
|
|
|
|
HtmlHelper.getPreview(body),
|
|
|
|
parts.getWarnings(message.warning));
|
2019-06-14 13:32:09 +00:00
|
|
|
Log.i(folder.name + " downloaded message id=" + message.id +
|
|
|
|
" size=" + message.size + "/" + (body == null ? null : body.length()));
|
2019-07-06 16:33:21 +00:00
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(body))
|
|
|
|
fixAttachments(context, message.id, body);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-26 19:21:09 +00:00
|
|
|
for (EntityAttachment attachment : attachments)
|
2019-06-29 06:52:15 +00:00
|
|
|
if (!attachment.available && TextUtils.isEmpty(attachment.error))
|
|
|
|
if (state.getNetworkState().isUnmetered() ||
|
|
|
|
(attachment.size != null && attachment.size < maxSize))
|
2019-03-14 08:05:28 +00:00
|
|
|
try {
|
2019-06-26 19:21:09 +00:00
|
|
|
parts.downloadAttachment(context, attachment);
|
2019-03-14 08:05:28 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2019-06-29 06:52:15 +00:00
|
|
|
db.attachment().setError(attachment.id, Helper.formatThrowable(ex, false));
|
2019-03-14 08:05:28 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:18:14 +00:00
|
|
|
private static void fixAttachments(Context context, long id, String body) {
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
for (Element element : Jsoup.parse(body).select("img")) {
|
|
|
|
String src = element.attr("src");
|
|
|
|
if (src.startsWith("cid:")) {
|
|
|
|
EntityAttachment attachment = db.attachment().getAttachment(id, "<" + src.substring(4) + ">");
|
|
|
|
if (attachment != null && !attachment.isInline()) {
|
|
|
|
Log.i("Setting attachment type to inline id=" + attachment.id);
|
|
|
|
db.attachment().setDisposition(attachment.id, Part.INLINE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-21 12:29:45 +00:00
|
|
|
static void notifyReset(Context context) {
|
|
|
|
lastUnseen = -1;
|
|
|
|
Widget.update(context, -1);
|
|
|
|
try {
|
|
|
|
ShortcutBadger.removeCount(context);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 08:16:42 +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-05-19 06:26:47 +00:00
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
2019-06-14 09:25:56 +00:00
|
|
|
if (nm == null)
|
|
|
|
return;
|
2019-05-19 06:26:47 +00:00
|
|
|
|
2019-03-02 09:45:14 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-03-30 07:43:36 +00:00
|
|
|
boolean badge = prefs.getBoolean("badge", true);
|
2019-06-01 12:19:14 +00:00
|
|
|
boolean pro = Helper.isPro(context);
|
2019-03-30 07:43:36 +00:00
|
|
|
|
2019-02-27 18:29:58 +00:00
|
|
|
// Current
|
2019-07-06 08:16:42 +00:00
|
|
|
int unseen = 0;
|
|
|
|
final Map<String, List<Long>> groupNotifying = new HashMap<>();
|
2019-05-19 06:26:47 +00:00
|
|
|
Map<String, List<TupleMessageEx>> groupMessages = new HashMap<>();
|
2019-02-27 18:29:58 +00:00
|
|
|
for (TupleMessageEx message : messages) {
|
2019-07-06 08:16:42 +00:00
|
|
|
if (!(message.ui_seen || message.ui_ignored || message.ui_hide != 0))
|
|
|
|
unseen++;
|
|
|
|
|
2019-04-17 08:23:51 +00:00
|
|
|
// Check if notification channel enabled
|
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O &&
|
2019-07-06 08:16:42 +00:00
|
|
|
message.notifying == 0 && message.from != null && message.from.length > 0) {
|
2019-04-17 08:23:51 +00:00
|
|
|
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-06-01 12:19:14 +00:00
|
|
|
String group = Long.toString(pro && message.accountNotify ? message.account : 0);
|
2019-03-07 12:29:03 +00:00
|
|
|
if (!groupMessages.containsKey(group)) {
|
2019-07-06 08:16:42 +00:00
|
|
|
groupNotifying.put(group, new ArrayList<Long>());
|
2019-03-07 12:29:03 +00:00
|
|
|
groupMessages.put(group, new ArrayList<TupleMessageEx>());
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
2019-03-07 12:29:03 +00:00
|
|
|
|
2019-07-06 08:16:42 +00:00
|
|
|
if (message.notifying != 0)
|
|
|
|
groupNotifying.get(group).add(message.id * message.notifying);
|
|
|
|
|
|
|
|
if (!(message.ui_seen || message.ui_ignored || message.ui_hide != 0)) {
|
|
|
|
// This assumes the messages are properly ordered
|
|
|
|
if (groupMessages.get(group).size() < MAX_NOTIFICATION_COUNT)
|
|
|
|
groupMessages.get(group).add(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update widget/badge count
|
|
|
|
if (lastUnseen < 0 || unseen != lastUnseen) {
|
|
|
|
lastUnseen = unseen;
|
|
|
|
Widget.update(context, unseen);
|
|
|
|
try {
|
|
|
|
ShortcutBadger.applyCount(context, badge ? unseen : 0);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Difference
|
2019-07-06 08:16:42 +00:00
|
|
|
for (String group : groupMessages.keySet()) {
|
2019-03-07 12:29:03 +00:00
|
|
|
List<Notification> notifications = getNotificationUnseen(context, group, groupMessages.get(group));
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2019-07-06 08:16:42 +00:00
|
|
|
final List<Long> add = new ArrayList<>();
|
|
|
|
final 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);
|
2019-07-06 08:16:42 +00:00
|
|
|
if (id != 0)
|
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-07-15 18:39:24 +00:00
|
|
|
remove.remove(-id);
|
2019-05-18 19:37:09 +00:00
|
|
|
add.add(id);
|
2019-02-27 18:29:58 +00:00
|
|
|
Log.i("Notify adding=" + id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
Log.i("Notify group=" + group + " count=" + notifications.size() +
|
2019-07-15 18:39:24 +00:00
|
|
|
" added=" + add.size() + " removed=" + remove.size());
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
if (notifications.size() == 0) {
|
2019-07-10 16:33:25 +00:00
|
|
|
String tag = "unseen." + group + "." + 0;
|
2019-07-06 08:16:42 +00:00
|
|
|
Log.i("Notify cancel tag=" + tag);
|
2019-05-07 18:47:40 +00:00
|
|
|
nm.cancel(tag, 1);
|
|
|
|
}
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2019-05-07 18:47:40 +00:00
|
|
|
for (Long id : remove) {
|
2019-07-10 16:33:25 +00:00
|
|
|
String tag = "unseen." + group + "." + Math.abs(id);
|
2019-07-06 08:16:42 +00:00
|
|
|
Log.i("Notify cancel tag=" + tag);
|
2019-05-07 18:47:40 +00:00
|
|
|
nm.cancel(tag, 1);
|
|
|
|
}
|
2019-02-27 18:29:58 +00:00
|
|
|
|
|
|
|
for (Notification notification : notifications) {
|
|
|
|
long id = notification.extras.getLong("id", 0);
|
2019-07-11 08:33:51 +00:00
|
|
|
if ((id == 0 && add.size() + remove.size() > 0) || add.contains(id)) {
|
2019-07-10 16:33:25 +00:00
|
|
|
String tag = "unseen." + group + "." + Math.abs(id);
|
2019-05-07 18:47:40 +00:00
|
|
|
Log.i("Notifying tag=" + tag +
|
|
|
|
(Build.VERSION.SDK_INT < Build.VERSION_CODES.O ? "" : " channel=" + notification.getChannelId()));
|
|
|
|
nm.notify(tag, 1, notification);
|
|
|
|
}
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
2019-03-02 09:45:14 +00:00
|
|
|
|
2019-07-06 08:16:42 +00:00
|
|
|
if (remove.size() + add.size() > 0) {
|
|
|
|
final DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
executor.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
for (long id : remove)
|
|
|
|
db.message().setMessageNotifying(Math.abs(id), 0);
|
|
|
|
for (long id : add)
|
|
|
|
db.message().setMessageNotifying(Math.abs(id), (int) Math.signum(id));
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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-07-12 08:05:30 +00:00
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
// Android 7+ N https://developer.android.com/training/notify-user/group
|
|
|
|
// Android 8+ O https://developer.android.com/training/notify-user/channels
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-06-14 09:25:56 +00:00
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
if (messages == null || messages.size() == 0 || nm == null)
|
2019-02-27 17:33:46 +00:00
|
|
|
return notifications;
|
|
|
|
|
|
|
|
boolean pro = Helper.isPro(context);
|
2019-05-07 17:28:46 +00:00
|
|
|
|
2019-02-27 17:33:46 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2019-07-10 16:41:39 +00:00
|
|
|
boolean biometrics = prefs.getBoolean("biometrics", false);
|
2019-05-06 08:33:29 +00:00
|
|
|
boolean flags = prefs.getBoolean("flags", true);
|
2019-05-07 17:28:46 +00:00
|
|
|
boolean notify_preview = prefs.getBoolean("notify_preview", true);
|
2019-06-28 11:08:04 +00:00
|
|
|
boolean notify_trash = (prefs.getBoolean("notify_trash", true) || !pro);
|
|
|
|
boolean notify_archive = (prefs.getBoolean("notify_archive", true) || !pro);
|
|
|
|
boolean notify_reply = (prefs.getBoolean("notify_reply", false) && pro);
|
2019-07-10 16:31:03 +00:00
|
|
|
boolean notify_flag = (prefs.getBoolean("notify_flag", false) && flags && pro);
|
2019-06-28 11:08:04 +00:00
|
|
|
boolean notify_seen = (prefs.getBoolean("notify_seen", true) || !pro);
|
2019-07-09 20:44:02 +00:00
|
|
|
boolean light = prefs.getBoolean("light", false);
|
|
|
|
String sound = prefs.getString("sound", null);
|
2019-05-05 20:28:07 +00:00
|
|
|
|
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-07-15 18:39:24 +00:00
|
|
|
// Summary notification
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
// Build pending intents
|
|
|
|
Intent summary = new Intent(context, ActivityView.class).setAction("unified");
|
|
|
|
PendingIntent piSummary = PendingIntent.getActivity(context, ActivityView.REQUEST_UNIFIED, summary, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
Intent clear = new Intent(context, ServiceUI.class).setAction("clear");
|
|
|
|
PendingIntent piClear = PendingIntent.getService(context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
|
|
|
|
// Build title
|
|
|
|
String title = context.getResources().getQuantityString(
|
|
|
|
R.plurals.title_notification_unseen, messages.size(), messages.size());
|
|
|
|
|
|
|
|
// Build notification
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(context, "notification")
|
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
|
|
|
.setContentTitle(title)
|
|
|
|
.setContentIntent(piSummary)
|
|
|
|
.setNumber(messages.size())
|
|
|
|
.setShowWhen(false)
|
|
|
|
.setDeleteIntent(piClear)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_STATUS)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
|
.setGroup(group)
|
|
|
|
.setGroupSummary(true)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
builder.setSound(null);
|
|
|
|
|
|
|
|
Notification pub = builder.build();
|
|
|
|
builder
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
|
|
|
.setPublicVersion(pub);
|
2019-05-07 17:28:46 +00:00
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
if (!biometrics) {
|
2019-07-15 19:28:25 +00:00
|
|
|
DateFormat DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
|
2019-07-15 18:39:24 +00:00
|
|
|
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);
|
2019-07-15 19:28:25 +00:00
|
|
|
sb.append(" ").append(DTF.format(message.received));
|
2019-07-15 18:39:24 +00:00
|
|
|
sb.append("<br>");
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(HtmlHelper.fromHtml(sb.toString()))
|
|
|
|
.setSummaryText(title));
|
2019-07-12 09:11:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
notifications.add(builder.build());
|
2019-07-12 09:11:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-07 17:28:46 +00:00
|
|
|
// Message notifications
|
2019-02-27 17:33:46 +00:00
|
|
|
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);
|
|
|
|
|
2019-05-05 20:28:07 +00:00
|
|
|
Intent ignore = new Intent(context, ServiceUI.class).setAction("ignore:" + message.id);
|
|
|
|
PendingIntent piIgnore = PendingIntent.getService(context, ServiceUI.PI_IGNORED, ignore, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Get channel name
|
|
|
|
String channelName = null;
|
2019-05-16 08:04:20 +00:00
|
|
|
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
|
|
|
|
NotificationChannel channel = null;
|
|
|
|
|
|
|
|
if (message.from != null && message.from.length > 0) {
|
|
|
|
InternetAddress from = (InternetAddress) message.from[0];
|
|
|
|
channel = nm.getNotificationChannel("notification." + from.getAddress().toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channel == null)
|
|
|
|
channel = nm.getNotificationChannel(EntityFolder.getNotificationChannelId(message.folder));
|
|
|
|
|
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-06-01 12:19:14 +00:00
|
|
|
channelName = EntityAccount.getNotificationChannelId(
|
|
|
|
pro && message.accountNotify ? message.account : 0);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-06-23 08:23:49 +00:00
|
|
|
NotificationCompat.Builder mbuilder =
|
|
|
|
new NotificationCompat.Builder(context, channelName)
|
|
|
|
.addExtras(args)
|
|
|
|
.setSmallIcon(R.drawable.baseline_email_white_24)
|
|
|
|
.setContentIntent(piContent)
|
|
|
|
.setWhen(message.received)
|
|
|
|
.setDeleteIntent(piIgnore)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
2019-07-23 09:38:36 +00:00
|
|
|
.setCategory(NotificationCompat.CATEGORY_EMAIL)
|
2019-06-23 08:23:49 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
|
|
|
.setOnlyAlertOnce(true);
|
2019-05-05 20:28:07 +00:00
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
mbuilder
|
|
|
|
.setGroup(group)
|
|
|
|
.setGroupSummary(false)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
|
|
|
|
int def = 0;
|
|
|
|
|
|
|
|
if (light) {
|
|
|
|
def |= DEFAULT_LIGHTS;
|
|
|
|
Log.i("Notify light enabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
Uri uri = (sound == null ? null : Uri.parse(sound));
|
|
|
|
if (uri == null || "file".equals(uri.getScheme()))
|
|
|
|
uri = null;
|
|
|
|
Log.i("Notify sound=" + uri);
|
|
|
|
|
|
|
|
if (uri == null)
|
|
|
|
def |= DEFAULT_SOUND;
|
|
|
|
else
|
|
|
|
mbuilder.setSound(uri);
|
|
|
|
|
|
|
|
mbuilder.setDefaults(def);
|
|
|
|
}
|
|
|
|
|
2019-07-11 08:33:51 +00:00
|
|
|
if (biometrics)
|
|
|
|
mbuilder.setContentTitle(context.getResources().getQuantityString(
|
|
|
|
R.plurals.title_notification_unseen, 1, 1));
|
|
|
|
else {
|
|
|
|
String folderName = message.folderDisplay == null
|
|
|
|
? Helper.localizeFolderName(context, message.folderName)
|
|
|
|
: message.folderDisplay;
|
|
|
|
|
|
|
|
mbuilder.setContentTitle(info.getDisplayName(true))
|
|
|
|
.setSubText(message.accountName + " · " + folderName);
|
|
|
|
}
|
|
|
|
|
2019-05-05 20:28:07 +00:00
|
|
|
if (notify_trash) {
|
2019-05-21 13:55:10 +00:00
|
|
|
Intent trash = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("trash:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2019-05-05 20:28:07 +00:00
|
|
|
PendingIntent piTrash = PendingIntent.getService(context, ServiceUI.PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_delete_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_trash),
|
|
|
|
piTrash);
|
|
|
|
mbuilder.addAction(actionTrash.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (notify_archive) {
|
2019-05-21 13:55:10 +00:00
|
|
|
Intent archive = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("archive:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2019-05-05 20:28:07 +00:00
|
|
|
PendingIntent piArchive = PendingIntent.getService(context, ServiceUI.PI_ARCHIVE, archive, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_archive_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_archive),
|
|
|
|
piArchive);
|
|
|
|
mbuilder.addAction(actionArchive.build());
|
|
|
|
}
|
|
|
|
|
2019-05-06 05:36:11 +00:00
|
|
|
if (notify_reply && message.content) {
|
2019-05-08 11:15:02 +00:00
|
|
|
Intent reply = new Intent(context, ActivityCompose.class)
|
2019-06-24 09:58:44 +00:00
|
|
|
.putExtra("action", "reply")
|
2019-05-08 11:15:02 +00:00
|
|
|
.putExtra("reference", message.id);
|
|
|
|
reply.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
PendingIntent piReply = PendingIntent.getActivity(context, ActivityCompose.PI_REPLY, reply, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-05-05 20:28:07 +00:00
|
|
|
NotificationCompat.Action.Builder actionReply = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_reply_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_reply),
|
|
|
|
piReply);
|
|
|
|
mbuilder.addAction(actionReply.build());
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:31:03 +00:00
|
|
|
if (notify_flag) {
|
2019-05-21 13:55:10 +00:00
|
|
|
Intent flag = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("flag:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2019-05-06 08:33:29 +00:00
|
|
|
PendingIntent piFlag = PendingIntent.getService(context, ServiceUI.PI_FLAG, flag, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
NotificationCompat.Action.Builder actionFlag = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_star_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_flag),
|
|
|
|
piFlag);
|
|
|
|
mbuilder.addAction(actionFlag.build());
|
|
|
|
}
|
|
|
|
|
2019-05-05 20:28:07 +00:00
|
|
|
if (notify_seen) {
|
2019-05-21 13:55:10 +00:00
|
|
|
Intent seen = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("seen:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2019-05-05 20:28:07 +00:00
|
|
|
PendingIntent piSeen = PendingIntent.getService(context, ServiceUI.PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.baseline_visibility_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_seen),
|
|
|
|
piSeen);
|
|
|
|
mbuilder.addAction(actionSeen.build());
|
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-07-11 08:33:51 +00:00
|
|
|
if (!biometrics) {
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
mbuilder.setContentText(message.subject);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-07-11 08:33:51 +00:00
|
|
|
if (message.content && notify_preview)
|
|
|
|
try {
|
|
|
|
String body = Helper.readText(message.getFile(context));
|
|
|
|
StringBuilder sbm = new StringBuilder();
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
sbm.append(message.subject).append("<br>");
|
|
|
|
String text = Jsoup.parse(body).text();
|
|
|
|
if (!TextUtils.isEmpty(text)) {
|
|
|
|
sbm.append("<em>");
|
|
|
|
if (text.length() > HtmlHelper.PREVIEW_SIZE) {
|
|
|
|
sbm.append(text.substring(0, HtmlHelper.PREVIEW_SIZE));
|
|
|
|
sbm.append("…");
|
|
|
|
} else
|
|
|
|
sbm.append(text);
|
|
|
|
sbm.append("</em>");
|
|
|
|
}
|
|
|
|
mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(HtmlHelper.fromHtml(sbm.toString())));
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
mbuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(ex.toString()));
|
2019-02-27 17:33:46 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 08:33:51 +00:00
|
|
|
if (info.hasPhoto())
|
|
|
|
mbuilder.setLargeIcon(info.getPhotoBitmap());
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-07-11 08:33:51 +00:00
|
|
|
if (info.hasLookupUri())
|
|
|
|
mbuilder.addPerson(info.getLookupUri().toString());
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-07-11 08:33:51 +00:00
|
|
|
if (pro && message.accountColor != null) {
|
|
|
|
mbuilder.setColor(message.accountColor);
|
|
|
|
mbuilder.setColorized(true);
|
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
notifications.add(mbuilder.build());
|
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
}
|
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// FolderClosedException: can happen when no connectivity
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// IllegalStateException:
|
|
|
|
// - "This operation is not allowed on a closed folder"
|
|
|
|
// - can happen when syncing message
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// ConnectionException
|
|
|
|
// - failed to create new store connection (connectivity)
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-23 18:56:07 +00:00
|
|
|
// MailConnectException
|
|
|
|
// - on connectivity problems when connecting to store
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-06-20 06:39:37 +00:00
|
|
|
static NotificationCompat.Builder getNotificationError(Context context, String channel, String title, Throwable ex) {
|
2019-02-27 15:05:15 +00:00
|
|
|
// 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
|
|
|
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
|
2019-06-23 08:23:49 +00:00
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(context, channel)
|
|
|
|
.setSmallIcon(R.drawable.baseline_warning_white_24)
|
|
|
|
.setContentTitle(context.getString(R.string.title_notification_failed, title))
|
2019-06-26 14:51:29 +00:00
|
|
|
.setContentText(Helper.formatThrowable(ex, false))
|
2019-06-23 08:23:49 +00:00
|
|
|
.setContentIntent(pi)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MAX)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_ERROR)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
2019-06-26 14:51:29 +00:00
|
|
|
.bigText(Helper.formatThrowable(ex, "\n", false)));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
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-05-12 16:41:51 +00:00
|
|
|
private ConnectionHelper.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-05-12 16:41:51 +00:00
|
|
|
State(ConnectionHelper.NetworkState networkState) {
|
2019-03-16 13:12:31 +00:00
|
|
|
this.networkState = networkState;
|
|
|
|
}
|
|
|
|
|
|
|
|
State(State parent) {
|
|
|
|
this(parent.networkState);
|
|
|
|
}
|
|
|
|
|
2019-05-12 16:41:51 +00:00
|
|
|
ConnectionHelper.NetworkState getNetworkState() {
|
2019-03-16 13:12:31 +00:00
|
|
|
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-05-15 19:21:05 +00:00
|
|
|
if (ex instanceof MessagingException &&
|
2019-06-13 12:31:46 +00:00
|
|
|
("connection failure".equals(ex.getMessage()) ||
|
|
|
|
ex.getCause() instanceof SocketException ||
|
2019-05-15 19:21:05 +00:00
|
|
|
ex.getCause() instanceof ConnectionException))
|
|
|
|
recoverable = false;
|
|
|
|
|
2019-06-27 07:18:57 +00:00
|
|
|
if (ex instanceof ConnectionException)
|
|
|
|
// failed to create new store connection
|
|
|
|
// BYE, Socket is closed
|
|
|
|
recoverable = false;
|
|
|
|
|
2019-06-24 06:35:13 +00:00
|
|
|
if (ex instanceof FolderClosedException ||
|
|
|
|
ex instanceof FolderNotFoundException)
|
2019-05-10 13:08:29 +00:00
|
|
|
recoverable = false;
|
|
|
|
|
2019-05-10 13:22:31 +00:00
|
|
|
if (ex instanceof IllegalStateException && (
|
|
|
|
"Not connected".equals(ex.getMessage()) ||
|
|
|
|
"This operation is not allowed on a closed folder".equals(ex.getMessage())))
|
2019-05-10 13:08:29 +00:00
|
|
|
recoverable = false;
|
|
|
|
|
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() {
|
2019-04-18 09:47:45 +00:00
|
|
|
return "[running=" + running + ",recoverable=" + recoverable + "]";
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|