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/>.
|
|
|
|
|
2021-01-01 07:56:36 +00:00
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
2019-05-04 20:49:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
import android.app.AlarmManager;
|
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;
|
2019-09-21 08:05:24 +00:00
|
|
|
import android.database.sqlite.SQLiteConstraintException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.net.Uri;
|
2019-02-27 17:33:46 +00:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2020-02-11 13:56:22 +00:00
|
|
|
import android.os.OperationCanceledException;
|
2021-06-22 12:51:40 +00:00
|
|
|
import android.os.PowerManager;
|
2019-02-27 15:05:15 +00:00
|
|
|
import android.os.SystemClock;
|
2021-02-15 09:47:03 +00:00
|
|
|
import android.service.notification.StatusBarNotification;
|
2020-11-08 11:47:03 +00:00
|
|
|
import android.text.Html;
|
2019-02-27 15:05:15 +00:00
|
|
|
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;
|
2020-02-11 15:09:29 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.core.app.NotificationCompat;
|
2020-10-24 10:47:09 +00:00
|
|
|
import androidx.core.app.Person;
|
2019-08-24 13:00:14 +00:00
|
|
|
import androidx.core.app.RemoteInput;
|
2020-10-24 10:47:09 +00:00
|
|
|
import androidx.core.graphics.drawable.IconCompat;
|
2019-11-22 12:50:16 +00:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
|
|
|
|
2020-05-01 19:47:59 +00:00
|
|
|
import com.sun.mail.gimap.GmailFolder;
|
2020-06-25 07:14:05 +00:00
|
|
|
import com.sun.mail.gimap.GmailMessage;
|
2020-11-16 19:12:40 +00:00
|
|
|
import com.sun.mail.iap.BadCommandException;
|
|
|
|
import com.sun.mail.iap.CommandFailedException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.iap.ConnectionException;
|
2019-10-21 07:54:20 +00:00
|
|
|
import com.sun.mail.iap.ProtocolException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.iap.Response;
|
2020-07-02 15:32:49 +00:00
|
|
|
import com.sun.mail.imap.AppendUID;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.imap.IMAPFolder;
|
|
|
|
import com.sun.mail.imap.IMAPMessage;
|
|
|
|
import com.sun.mail.imap.IMAPStore;
|
2019-08-24 07:38:11 +00:00
|
|
|
import com.sun.mail.imap.protocol.FLAGS;
|
2019-02-27 15:05:15 +00:00
|
|
|
import com.sun.mail.imap.protocol.FetchResponse;
|
|
|
|
import com.sun.mail.imap.protocol.IMAPProtocol;
|
|
|
|
import com.sun.mail.imap.protocol.UID;
|
2019-09-19 15:41:26 +00:00
|
|
|
import com.sun.mail.pop3.POP3Folder;
|
|
|
|
import com.sun.mail.pop3.POP3Message;
|
|
|
|
import com.sun.mail.pop3.POP3Store;
|
2021-05-03 16:30:49 +00:00
|
|
|
import com.sun.mail.util.MessageRemovedIOException;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
2019-09-28 16:36:07 +00:00
|
|
|
import java.io.BufferedOutputStream;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2019-07-29 05:47:11 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2020-02-24 09:11:05 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.net.SocketException;
|
2019-07-16 17:28:42 +00:00
|
|
|
import java.net.UnknownHostException;
|
2020-02-24 09:11:05 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
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-10-05 18:44:45 +00:00
|
|
|
import java.util.LinkedHashMap;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.List;
|
2020-10-23 15:17:48 +00:00
|
|
|
import java.util.Locale;
|
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-29 09:17:12 +00:00
|
|
|
import java.util.Properties;
|
2019-02-27 15:05:15 +00:00
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
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;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Store;
|
2020-03-06 14:29:33 +00:00
|
|
|
import javax.mail.StoreClosedException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.UIDFolder;
|
2020-02-07 18:20:22 +00:00
|
|
|
import javax.mail.internet.AddressException;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
import javax.mail.internet.MimeMessage;
|
2020-06-08 17:36:45 +00:00
|
|
|
import javax.mail.search.AndTerm;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.search.ComparisonTerm;
|
|
|
|
import javax.mail.search.FlagTerm;
|
2020-06-08 17:36:45 +00:00
|
|
|
import javax.mail.search.HeaderTerm;
|
2019-02-27 15:05:15 +00:00
|
|
|
import javax.mail.search.MessageIDTerm;
|
|
|
|
import javax.mail.search.OrTerm;
|
|
|
|
import javax.mail.search.ReceivedDateTerm;
|
|
|
|
import javax.mail.search.SearchTerm;
|
2020-04-01 16:28:01 +00:00
|
|
|
import javax.mail.search.SentDateTerm;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-05-28 18:22:47 +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-09-27 12:14:04 +00:00
|
|
|
import static javax.mail.Folder.READ_WRITE;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
class Core {
|
2021-02-24 10:34:40 +00:00
|
|
|
private static final int MAX_NOTIFICATION_DISPLAY = 10; // per group
|
2021-02-14 20:14:59 +00:00
|
|
|
private static final int MAX_NOTIFICATION_COUNT = 100; // per group
|
2021-06-22 12:51:40 +00:00
|
|
|
private static final long SCREEN_ON_DURATION = 3000L; // milliseconds
|
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;
|
2020-09-02 08:12:43 +00:00
|
|
|
private static final int SYNC_YIELD_COUNT = 100;
|
|
|
|
private static final long SYNC_YIELD_DURATION = 1000; // milliseconds
|
|
|
|
private static final int DOWNLOAD_YIELD_COUNT = 25;
|
|
|
|
private static final long DOWNLOAD_YIELD_DURATION = 1000; // milliseconds
|
2019-02-27 15:05:15 +00:00
|
|
|
private static final long YIELD_DURATION = 200L; // milliseconds
|
2020-08-29 18:20:08 +00:00
|
|
|
private static final long JOIN_WAIT = 180 * 1000L; // milliseconds
|
2020-07-24 11:26:18 +00:00
|
|
|
private static final long FUTURE_RECEIVED = 30 * 24 * 3600 * 1000L; // milliseconds
|
2020-08-05 15:44:55 +00:00
|
|
|
private static final int LOCAL_RETRY_MAX = 2;
|
|
|
|
private static final long LOCAL_RETRY_DELAY = 5 * 1000L; // milliseconds
|
2020-09-22 06:37:25 +00:00
|
|
|
private static final int TOTAL_RETRY_MAX = LOCAL_RETRY_MAX * 5;
|
2020-10-13 15:24:39 +00:00
|
|
|
private static final int MAX_PREVIEW = 5000; // characters
|
2021-02-22 19:11:34 +00:00
|
|
|
private static final long EXISTS_RETRY_DELAY = 20 * 1000L; // milliseconds
|
2021-03-11 09:21:17 +00:00
|
|
|
private static final int FIND_RETRY_COUNT = 3; // times
|
|
|
|
private static final long FIND_RETRY_DELAY = 5 * 1000L; // milliseconds
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-25 10:59:11 +00:00
|
|
|
private static final Map<Long, List<EntityIdentity>> accountIdentities = new HashMap<>();
|
|
|
|
|
|
|
|
static void clearIdentities() {
|
|
|
|
synchronized (accountIdentities) {
|
|
|
|
accountIdentities.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static List<EntityIdentity> getIdentities(long account, Context context) {
|
|
|
|
synchronized (accountIdentities) {
|
|
|
|
if (!accountIdentities.containsKey(account))
|
|
|
|
accountIdentities.put(account,
|
|
|
|
DB.getInstance(context).identity().getSynchronizingIdentities(account));
|
|
|
|
return accountIdentities.get(account);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
static void processOperations(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context,
|
2020-01-25 11:38:38 +00:00
|
|
|
EntityAccount account, EntityFolder folder, List<TupleOperationEx> ops,
|
2019-07-29 09:17:12 +00:00
|
|
|
Store istore, Folder ifolder,
|
2020-09-22 12:04:26 +00:00
|
|
|
State state, int priority, long sequence)
|
2020-02-07 21:07:16 +00:00
|
|
|
throws JSONException {
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " start process");
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context);
|
2020-01-25 07:23:38 +00:00
|
|
|
|
2021-04-30 17:45:20 +00:00
|
|
|
NotificationManager nm =
|
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
2020-02-07 21:07:16 +00:00
|
|
|
int retry = 0;
|
|
|
|
boolean group = true;
|
2020-01-25 11:38:38 +00:00
|
|
|
Log.i(folder.name + " executing operations=" + ops.size());
|
2020-09-22 12:04:26 +00:00
|
|
|
while (retry < LOCAL_RETRY_MAX && ops.size() > 0 &&
|
|
|
|
state.isRunning() &&
|
|
|
|
state.batchCanRun(folder.id, priority, sequence)) {
|
2020-02-07 21:07:16 +00:00
|
|
|
TupleOperationEx op = ops.get(0);
|
2019-09-28 13:27:35 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
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 +
|
2020-02-07 21:07:16 +00:00
|
|
|
" args=" + op.args +
|
2020-02-08 11:49:12 +00:00
|
|
|
" group=" + group +
|
2020-02-07 21:07:16 +00:00
|
|
|
" retry=" + retry);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-30 17:45:20 +00:00
|
|
|
if (EntityOperation.HEADERS.equals(op.name) ||
|
|
|
|
EntityOperation.RAW.equals(op.name))
|
2021-07-15 16:36:39 +00:00
|
|
|
nm.cancel(op.name + ":" + op.message, NotificationHelper.NOTIFICATION_TAGGED);
|
2021-04-30 17:45:20 +00:00
|
|
|
|
2020-10-07 05:38:22 +00:00
|
|
|
if (!Objects.equals(folder.id, op.folder))
|
|
|
|
throw new IllegalArgumentException("Invalid folder=" + folder.id + "/" + op.folder);
|
|
|
|
|
2020-12-17 14:05:44 +00:00
|
|
|
if (account.protocol == EntityAccount.TYPE_IMAP &&
|
2020-12-17 14:27:12 +00:00
|
|
|
!folder.local && ifolder != null && !ifolder.isOpen()) {
|
|
|
|
Log.w(folder.name + " is closed");
|
2020-03-09 11:17:18 +00:00
|
|
|
break;
|
2020-12-17 14:27:12 +00:00
|
|
|
}
|
2020-03-05 10:16:25 +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);
|
2020-02-07 21:07:16 +00:00
|
|
|
Map<TupleOperationEx, EntityMessage> similar = new HashMap<>();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
// Operations should use database transaction when needed
|
|
|
|
|
2019-09-28 14:41:02 +00:00
|
|
|
if (message == null &&
|
|
|
|
!EntityOperation.FETCH.equals(op.name) &&
|
|
|
|
!EntityOperation.SYNC.equals(op.name) &&
|
2020-07-26 17:58:13 +00:00
|
|
|
!EntityOperation.SUBSCRIBE.equals(op.name) &&
|
2021-02-12 12:46:21 +00:00
|
|
|
!EntityOperation.PURGE.equals(op.name) &&
|
|
|
|
!EntityOperation.EXPUNGE.equals(op.name))
|
2019-09-28 14:41:02 +00:00
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
// Process similar operations
|
2019-09-17 14:36:19 +00:00
|
|
|
boolean skip = false;
|
2020-02-07 21:07:16 +00:00
|
|
|
for (int j = 1; j < ops.size(); j++) {
|
|
|
|
TupleOperationEx next = ops.get(j);
|
2019-09-17 14:36:19 +00:00
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
switch (op.name) {
|
|
|
|
case EntityOperation.ADD:
|
|
|
|
// Same message
|
|
|
|
if (Objects.equals(op.message, next.message) &&
|
|
|
|
(EntityOperation.ADD.equals(next.name) ||
|
|
|
|
EntityOperation.DELETE.equals(next.name)))
|
2019-09-17 14:36:19 +00:00
|
|
|
skip = true;
|
2019-09-28 13:27:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.FETCH:
|
|
|
|
if (EntityOperation.FETCH.equals(next.name)) {
|
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
|
|
|
// Same uid
|
|
|
|
if (jargs.getLong(0) == jnext.getLong(0))
|
|
|
|
skip = true;
|
2019-09-17 14:36:19 +00:00
|
|
|
}
|
2019-09-28 13:27:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EntityOperation.MOVE:
|
2020-02-07 21:07:16 +00:00
|
|
|
if (group &&
|
|
|
|
message.uid != null &&
|
2021-02-23 14:05:54 +00:00
|
|
|
op.name.equals(next.name) &&
|
2020-04-06 12:11:50 +00:00
|
|
|
account.protocol == EntityAccount.TYPE_IMAP) {
|
2019-09-28 13:27:35 +00:00
|
|
|
JSONArray jnext = new JSONArray(next.args);
|
2019-10-02 07:58:54 +00:00
|
|
|
// Same target
|
2019-10-01 15:52:41 +00:00
|
|
|
if (jargs.getLong(0) == jnext.getLong(0)) {
|
2019-09-28 13:27:35 +00:00
|
|
|
EntityMessage m = db.message().getMessage(next.message);
|
2020-02-07 21:07:16 +00:00
|
|
|
if (m != null && m.uid != null)
|
2019-09-28 13:27:35 +00:00
|
|
|
similar.put(next, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-09-17 14:36:19 +00:00
|
|
|
}
|
2019-09-21 08:05:24 +00:00
|
|
|
|
2019-09-17 14:36:19 +00:00
|
|
|
if (skip) {
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name +
|
2019-09-17 14:36:19 +00:00
|
|
|
" skipping op=" + op.id + "/" + op.name +
|
2019-09-17 11:38:23 +00:00
|
|
|
" msg=" + op.message + " args=" + op.args);
|
2019-09-17 14:36:19 +00:00
|
|
|
db.operation().deleteOperation(op.id);
|
2020-02-07 21:07:16 +00:00
|
|
|
ops.remove(op);
|
2019-09-17 11:38:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
List<Long> sids = new ArrayList<>();
|
2020-02-07 21:07:16 +00:00
|
|
|
for (TupleOperationEx s : similar.keySet())
|
2019-09-28 13:27:35 +00:00
|
|
|
sids.add(s.id);
|
|
|
|
|
|
|
|
if (similar.size() > 0)
|
|
|
|
Log.i(folder.name + " similar=" + TextUtils.join(",", sids));
|
|
|
|
|
2020-11-29 09:39:03 +00:00
|
|
|
op.tries++;
|
|
|
|
|
2019-09-28 13:27:35 +00:00
|
|
|
// Leave crumb
|
2019-09-21 08:05:24 +00:00
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("name", op.name);
|
|
|
|
crumb.put("args", op.args);
|
2020-11-05 09:33:37 +00:00
|
|
|
crumb.put("account", op.account + ":" + account.protocol);
|
|
|
|
crumb.put("folder", op.folder + ":" + folder.type);
|
2019-09-21 08:05:24 +00:00
|
|
|
if (op.message != null)
|
|
|
|
crumb.put("message", Long.toString(op.message));
|
2020-11-29 09:39:03 +00:00
|
|
|
crumb.put("tries", Integer.toString(op.tries));
|
2019-09-28 13:27:35 +00:00
|
|
|
crumb.put("similar", TextUtils.join(",", sids));
|
2019-11-09 13:13:18 +00:00
|
|
|
crumb.put("thread", Thread.currentThread().getName() + ":" + Thread.currentThread().getId());
|
2019-09-21 08:05:24 +00:00
|
|
|
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
|
|
|
|
Log.breadcrumb("start operation", crumb);
|
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
db.operation().setOperationError(op.id, null);
|
|
|
|
|
2020-02-08 11:49:12 +00:00
|
|
|
if (message != null)
|
2019-09-28 13:47:34 +00:00
|
|
|
db.message().setMessageError(message.id, null);
|
|
|
|
|
2020-02-12 16:34:31 +00:00
|
|
|
db.operation().setOperationState(op.id, "executing");
|
|
|
|
for (TupleOperationEx s : similar.keySet())
|
|
|
|
db.operation().setOperationState(s.id, "executing");
|
2019-09-28 13:47:34 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
if (istore instanceof POP3Store)
|
|
|
|
switch (op.name) {
|
|
|
|
case EntityOperation.SEEN:
|
2019-09-19 19:42:09 +00:00
|
|
|
onSeen(context, jargs, folder, message, (POP3Folder) ifolder);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-28 08:05:45 +00:00
|
|
|
|
|
|
|
case EntityOperation.FLAG:
|
|
|
|
onFlag(context, jargs, folder, message, (POP3Folder) ifolder);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ANSWERED:
|
|
|
|
case EntityOperation.ADD:
|
|
|
|
case EntityOperation.EXISTS:
|
2019-09-19 19:47:19 +00:00
|
|
|
// Do nothing
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-28 08:05:45 +00:00
|
|
|
|
2020-04-06 12:11:50 +00:00
|
|
|
case EntityOperation.MOVE:
|
|
|
|
onMove(context, jargs, folder, message);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.DELETE:
|
2020-09-21 19:46:39 +00:00
|
|
|
onDelete(context, jargs, account, folder, message, (POP3Folder) ifolder, (POP3Store) istore, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-28 08:05:45 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.SYNC:
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2019-09-24 17:33:20 +00:00
|
|
|
onSynchronizeMessages(context, jargs, account, folder, (POP3Folder) ifolder, (POP3Store) istore, state);
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-28 08:05:45 +00:00
|
|
|
|
2020-07-26 17:58:13 +00:00
|
|
|
case EntityOperation.PURGE:
|
2020-07-27 14:54:03 +00:00
|
|
|
onPurgeFolder(context, folder);
|
2020-07-26 17:58:13 +00:00
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
default:
|
|
|
|
Log.w(folder.name + " ignored=" + op.name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ensureUid(context, folder, message, op, (IMAPFolder) ifolder);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
switch (op.name) {
|
|
|
|
case EntityOperation.SEEN:
|
|
|
|
onSeen(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.FLAG:
|
|
|
|
onFlag(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ANSWERED:
|
|
|
|
onAnswered(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.KEYWORD:
|
|
|
|
onKeyword(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-06-25 07:14:05 +00:00
|
|
|
case EntityOperation.LABEL:
|
|
|
|
onLabel(context, jargs, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ADD:
|
2020-09-21 19:46:39 +00:00
|
|
|
onAdd(context, jargs, account, folder, message, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-03-02 14:32:19 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.MOVE:
|
2019-09-28 13:27:35 +00:00
|
|
|
List<EntityMessage> messages = new ArrayList<>();
|
|
|
|
messages.add(message);
|
|
|
|
messages.addAll(similar.values());
|
2021-06-23 21:03:55 +00:00
|
|
|
onMove(context, jargs, false, account, folder, messages, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.COPY:
|
2021-06-23 21:03:55 +00:00
|
|
|
onMove(context, jargs, true, account, folder, Arrays.asList(message), (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.FETCH:
|
2020-09-21 19:46:39 +00:00
|
|
|
onFetch(context, jargs, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.DELETE:
|
|
|
|
onDelete(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.HEADERS:
|
|
|
|
onHeaders(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.RAW:
|
|
|
|
onRaw(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.BODY:
|
|
|
|
onBody(context, jargs, folder, message, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.ATTACHMENT:
|
|
|
|
onAttachment(context, jargs, folder, message, op, (IMAPFolder) ifolder);
|
|
|
|
break;
|
2019-09-17 12:22:18 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.EXISTS:
|
2021-02-22 19:11:34 +00:00
|
|
|
onExists(context, jargs, account, folder, message, op, (IMAPFolder) ifolder);
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.SYNC:
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2020-09-21 19:46:39 +00:00
|
|
|
onSynchronizeMessages(context, jargs, account, folder, (IMAPStore) istore, (IMAPFolder) ifolder, state);
|
2021-03-06 10:14:23 +00:00
|
|
|
Helper.gc();
|
2019-09-19 15:41:26 +00:00
|
|
|
break;
|
2019-03-05 12:46:25 +00:00
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
case EntityOperation.SUBSCRIBE:
|
|
|
|
onSubscribeFolder(context, jargs, folder, (IMAPFolder) ifolder);
|
|
|
|
break;
|
|
|
|
|
2020-07-26 17:58:13 +00:00
|
|
|
case EntityOperation.PURGE:
|
|
|
|
onPurgeFolder(context, jargs, folder, (IMAPFolder) ifolder);
|
|
|
|
break;
|
|
|
|
|
2021-02-12 11:51:34 +00:00
|
|
|
case EntityOperation.EXPUNGE:
|
|
|
|
onExpungeFolder(context, jargs, folder, (IMAPFolder) ifolder);
|
|
|
|
break;
|
|
|
|
|
2020-07-13 14:34:55 +00:00
|
|
|
case EntityOperation.RULE:
|
|
|
|
onRule(context, jargs, message);
|
|
|
|
break;
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown operation=" + op.name);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2019-11-09 13:13:18 +00:00
|
|
|
crumb.put("thread", Thread.currentThread().getName() + ":" + Thread.currentThread().getId());
|
2019-09-21 08:05:24 +00:00
|
|
|
crumb.put("free", Integer.toString(Log.getFreeMemMb()));
|
|
|
|
Log.breadcrumb("end operation", crumb);
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// Operation succeeded
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
db.operation().deleteOperation(op.id);
|
2020-02-07 21:07:16 +00:00
|
|
|
for (TupleOperationEx s : similar.keySet())
|
2019-09-28 13:47:34 +00:00
|
|
|
db.operation().deleteOperation(s.id);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-02-07 21:07:16 +00:00
|
|
|
|
|
|
|
ops.remove(op);
|
|
|
|
for (TupleOperationEx s : similar.keySet())
|
|
|
|
ops.remove(s);
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (Throwable ex) {
|
2021-07-28 15:02:11 +00:00
|
|
|
if (ex instanceof OperationCanceledException)
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
else
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
|
2020-02-11 16:46:04 +00:00
|
|
|
EntityLog.log(context, folder.name +
|
2020-05-15 15:21:54 +00:00
|
|
|
" op=" + op.name +
|
|
|
|
" try=" + op.tries +
|
|
|
|
" " + Log.formatThrowable(ex, false));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-09-28 13:27:35 +00:00
|
|
|
|
2020-11-30 07:17:07 +00:00
|
|
|
db.operation().setOperationTries(op.id, op.tries);
|
2019-09-28 13:47:34 +00:00
|
|
|
|
2020-11-30 07:17:07 +00:00
|
|
|
op.error = Log.formatThrowable(ex);
|
|
|
|
db.operation().setOperationError(op.id, op.error);
|
|
|
|
|
|
|
|
if (message != null &&
|
2020-12-03 10:32:22 +00:00
|
|
|
!EntityOperation.FETCH.equals(op.name) &&
|
2021-04-30 17:45:20 +00:00
|
|
|
!EntityOperation.ATTACHMENT.equals(op.name) &&
|
2020-11-30 07:17:07 +00:00
|
|
|
!(ex instanceof IllegalArgumentException))
|
|
|
|
db.message().setMessageError(message.id, op.error);
|
2019-09-29 09:43:22 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-09-28 13:27:35 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-11-30 07:17:07 +00:00
|
|
|
if (similar.size() > 0) {
|
|
|
|
// Retry individually
|
|
|
|
group = false;
|
|
|
|
// Finally will reset state
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-11 17:21:50 +00:00
|
|
|
if (op.tries >= TOTAL_RETRY_MAX ||
|
|
|
|
ex instanceof OutOfMemoryError ||
|
2019-07-29 05:47:11 +00:00
|
|
|
ex instanceof FileNotFoundException ||
|
2019-02-27 15:05:15 +00:00
|
|
|
ex instanceof FolderNotFoundException ||
|
2019-04-29 20:05:12 +00:00
|
|
|
ex instanceof IllegalArgumentException ||
|
2019-11-10 10:25:13 +00:00
|
|
|
ex instanceof SQLiteConstraintException ||
|
2020-11-16 19:12:40 +00:00
|
|
|
(!ConnectionHelper.isIoError(ex) &&
|
|
|
|
(ex.getCause() instanceof BadCommandException ||
|
2021-05-12 15:14:27 +00:00
|
|
|
ex.getCause() instanceof CommandFailedException /* NO */) &&
|
|
|
|
// https://sebastian.marsching.com/wiki/Network/Zimbra#Mailbox_Selected_READ-ONLY_Error_in_Thunderbird
|
|
|
|
(ex.getMessage() == null ||
|
|
|
|
!ex.getMessage().contains("mailbox selected READ-ONLY"))) ||
|
2020-11-13 07:32:12 +00:00
|
|
|
MessageHelper.isRemoved(ex) ||
|
2021-04-30 17:45:20 +00:00
|
|
|
EntityOperation.HEADERS.equals(op.name) ||
|
|
|
|
EntityOperation.RAW.equals(op.name) ||
|
2020-10-12 15:36:11 +00:00
|
|
|
EntityOperation.ATTACHMENT.equals(op.name) ||
|
2020-11-25 21:37:44 +00:00
|
|
|
(EntityOperation.ADD.equals(op.name) &&
|
|
|
|
EntityFolder.DRAFTS.equals(folder.type))) {
|
2020-11-21 07:07:03 +00:00
|
|
|
// com.sun.mail.iap.BadCommandException: BAD [TOOBIG] Message too large
|
|
|
|
// com.sun.mail.iap.CommandFailedException: NO [CANNOT] Cannot APPEND to a SPAM folder
|
|
|
|
// com.sun.mail.iap.CommandFailedException: NO [ALERT] Cannot MOVE messages out of the Drafts folder
|
|
|
|
// com.sun.mail.iap.CommandFailedException: NO [OVERQUOTA] quota exceeded
|
2019-12-29 13:59:29 +00:00
|
|
|
// Drafts: javax.mail.FolderClosedException: * BYE Jakarta Mail Exception:
|
|
|
|
// javax.net.ssl.SSLException: Write error: ssl=0x8286cac0: I/O error during system call, Broken pipe
|
2020-09-11 10:41:42 +00:00
|
|
|
// Drafts: * BYE Jakarta Mail Exception: java.io.IOException: Connection dropped by server?
|
2020-12-05 06:42:00 +00:00
|
|
|
// Sync: BAD Could not parse command
|
2021-05-12 14:37:29 +00:00
|
|
|
// Sync: SEARCH not allowed now
|
2020-12-03 07:09:19 +00:00
|
|
|
// Seen: NO mailbox selected READ-ONLY
|
2021-02-19 12:21:33 +00:00
|
|
|
// Fetch: BAD Error in IMAP command FETCH: Invalid messageset (n.nnn + n.nnn secs).
|
2020-12-02 14:35:24 +00:00
|
|
|
// Fetch: NO all of the requested messages have been expunged
|
2020-12-06 08:28:45 +00:00
|
|
|
// Fetch: BAD parse error: invalid message sequence number:
|
2020-12-18 07:02:55 +00:00
|
|
|
// Fetch: NO The specified message set is invalid.
|
2020-12-06 08:28:45 +00:00
|
|
|
// Fetch: NO [SERVERBUG] SELECT Server error - Please try again later
|
|
|
|
// Fetch: NO [SERVERBUG] UID FETCH Server error - Please try again later
|
2021-01-30 08:41:33 +00:00
|
|
|
// Fetch: NO Invalid message number (took 123 ms)
|
2021-01-22 07:24:11 +00:00
|
|
|
// Fetch: BAD Internal Server Error
|
2021-05-12 14:37:29 +00:00
|
|
|
// Fetch: BAD Error in IMAP command FETCH: Invalid messageset (n.nnn + n .nnn secs).
|
2021-01-31 11:18:01 +00:00
|
|
|
// Fetch UID: NO Some messages could not be FETCHed (Failure)
|
2021-03-13 07:54:07 +00:00
|
|
|
// Fetch UID: NO [LIMIT] UID FETCH Rate limit hit.
|
|
|
|
// Fetch UID: NO Server Unavailable. 15
|
2021-03-15 20:24:15 +00:00
|
|
|
// Fetch UID: NO [UNAVAILABLE] Failed to open mailbox
|
2021-05-12 14:37:29 +00:00
|
|
|
// Fetch UID: NO [TEMPFAIL] SELECT completed
|
2020-12-06 08:28:45 +00:00
|
|
|
// Move: NO Over quota
|
2020-11-21 07:07:03 +00:00
|
|
|
// Move: NO No matching messages
|
2020-12-05 06:42:00 +00:00
|
|
|
// Move: NO [EXPUNGEISSUED] Some of the requested messages no longer exist
|
|
|
|
// Move: BAD parse error: invalid message sequence number:
|
2020-12-08 07:30:23 +00:00
|
|
|
// Move: NO MOVE failed or partially completed.
|
|
|
|
// Move: NO mailbox selected READ-ONLY
|
2020-12-20 17:02:42 +00:00
|
|
|
// Move: NO read only
|
2021-01-30 08:41:33 +00:00
|
|
|
// Move: NO COPY failed
|
2021-01-31 11:18:01 +00:00
|
|
|
// Move: NO [SERVERBUG] Internal error occurred. Refer to server log for more information.
|
2021-02-12 15:52:55 +00:00
|
|
|
// Move: NO STORE: mtd: internal error: Cannot set message attributes.<404, ebox: no such entity: LiteMessage 29215 does not exist>
|
|
|
|
// Move: NO mailbox selected READ-ONLY
|
|
|
|
// Move: NO System Error (Failure)
|
2021-02-13 15:51:36 +00:00
|
|
|
// Move: NO APPEND processing failed.
|
2021-05-12 14:37:29 +00:00
|
|
|
// Move: NO Server Unavailable. 15
|
|
|
|
// Move: NO [CANNOT] Operation is not supported on mailbox
|
2021-03-04 18:48:51 +00:00
|
|
|
// Copy: NO Client tried to access nonexistent namespace. (Mailbox name should probably be prefixed with: INBOX.) (n.nnn + n.nnn secs).
|
2021-01-10 16:14:55 +00:00
|
|
|
// Add: BAD Data length exceeds limit
|
2021-01-30 08:41:33 +00:00
|
|
|
// Add: NO [LIMIT] APPEND Command exceeds the maximum allowed size
|
|
|
|
// Add: NO APPEND failed: Unknown flag: SEEN
|
|
|
|
// Add: BAD mtd: internal error: APPEND Message too long. 12345678
|
2021-02-12 15:52:55 +00:00
|
|
|
// Add: NO [OVERQUOTA] Not enough disk quota (n.nnn + n.nnn + n.nnn secs).
|
2021-03-17 12:24:30 +00:00
|
|
|
// Add: NO [OVERQUOTA] Quota exceeded (mailbox for user is full) (n.nnn + n.nnn secs).
|
2021-02-13 15:51:36 +00:00
|
|
|
// Add: NO APPEND failed
|
2021-04-13 10:00:46 +00:00
|
|
|
// Add: BAD [TOOBIG] Message too large.
|
2020-11-23 08:34:59 +00:00
|
|
|
// Delete: NO [CANNOT] STORE It's not possible to perform specified operation
|
2020-12-02 14:35:24 +00:00
|
|
|
// Delete: NO [UNAVAILABLE] EXPUNGE Backend error
|
2020-12-08 07:30:23 +00:00
|
|
|
// Delete: NO mailbox selected READ-ONLY
|
2021-01-31 19:51:05 +00:00
|
|
|
// Flags: NO mailbox selected READ-ONLY
|
2021-01-30 08:41:33 +00:00
|
|
|
// Keyword: NO STORE completed
|
2021-04-05 16:34:35 +00:00
|
|
|
// Keyword: NO [CANNOT] Keyword length too long (n.nnn + n.nnn secs).
|
2021-01-31 11:18:01 +00:00
|
|
|
// Search: BAD command syntax error
|
2021-02-12 15:52:55 +00:00
|
|
|
// Search (sync): BAD Could not parse command
|
2020-12-08 09:45:31 +00:00
|
|
|
|
2020-11-13 07:32:12 +00:00
|
|
|
String msg = "Unrecoverable operation=" + op.name + " tries=" + op.tries + " created=" + new Date(op.created);
|
2020-12-08 09:45:31 +00:00
|
|
|
|
2020-11-13 07:32:12 +00:00
|
|
|
EntityLog.log(context, msg +
|
|
|
|
" folder=" + folder.id + ":" + folder.name +
|
|
|
|
" message=" + (message == null ? null : message.id + ":" + message.subject) +
|
|
|
|
" reason=" + Log.formatThrowable(ex, false));
|
2020-12-08 09:45:31 +00:00
|
|
|
|
|
|
|
if (ifolder != null && ifolder.isOpen() &&
|
|
|
|
(op.tries > 1 ||
|
|
|
|
ex.getCause() instanceof BadCommandException ||
|
|
|
|
ex.getCause() instanceof CommandFailedException))
|
2020-11-16 11:40:12 +00:00
|
|
|
Log.e(new Throwable(msg, ex));
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-02-08 11:49:12 +00:00
|
|
|
// Cleanup operation
|
2020-11-05 15:45:30 +00:00
|
|
|
op.cleanup(context, true);
|
2020-02-08 11:49:12 +00:00
|
|
|
|
2019-09-28 13:47:34 +00:00
|
|
|
// There is no use in repeating
|
|
|
|
db.operation().deleteOperation(op.id);
|
2019-04-30 10:21:26 +00:00
|
|
|
|
2019-09-28 14:16:55 +00:00
|
|
|
// Cleanup messages
|
2021-05-13 05:23:02 +00:00
|
|
|
if (message != null &&
|
|
|
|
MessageHelper.isRemoved(ex) &&
|
|
|
|
!EntityOperation.SEEN.equals(op.name))
|
2019-09-28 13:47:34 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:07:16 +00:00
|
|
|
ops.remove(op);
|
2021-04-30 17:45:20 +00:00
|
|
|
|
2021-07-19 05:11:42 +00:00
|
|
|
if (!MessageHelper.isRemoved(ex)) {
|
|
|
|
int resid = context.getResources().getIdentifier(
|
|
|
|
"title_op_title_" + op.name,
|
|
|
|
"string",
|
|
|
|
context.getPackageName());
|
|
|
|
String title = (resid == 0 ? null : context.getString(resid));
|
|
|
|
if (title != null) {
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
getNotificationError(context, "warning", title, ex);
|
|
|
|
nm.notify(op.name + ":" + op.message,
|
|
|
|
NotificationHelper.NOTIFICATION_TAGGED,
|
|
|
|
builder.build());
|
|
|
|
}
|
2021-04-30 17:45:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:07:16 +00:00
|
|
|
} else {
|
|
|
|
retry++;
|
2020-09-22 12:04:26 +00:00
|
|
|
if (retry < LOCAL_RETRY_MAX &&
|
|
|
|
state.isRunning() &&
|
|
|
|
state.batchCanRun(folder.id, priority, sequence))
|
2020-02-08 11:29:13 +00:00
|
|
|
try {
|
2020-02-11 16:46:04 +00:00
|
|
|
Thread.sleep(LOCAL_RETRY_DELAY);
|
2020-02-08 11:29:13 +00:00
|
|
|
} catch (InterruptedException ex1) {
|
|
|
|
Log.w(ex1);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-05-11 19:04:27 +00:00
|
|
|
} finally {
|
2020-02-08 11:49:12 +00:00
|
|
|
// Reset operation state
|
2019-09-28 13:47:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
db.operation().setOperationState(op.id, null);
|
2020-02-07 21:07:16 +00:00
|
|
|
for (TupleOperationEx s : similar.keySet())
|
2019-09-28 13:47:34 +00:00
|
|
|
db.operation().setOperationState(s.id, null);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end op=" + op.id + "/" + op.name);
|
|
|
|
}
|
|
|
|
}
|
2020-02-07 21:07:16 +00:00
|
|
|
|
2020-09-22 12:04:26 +00:00
|
|
|
if (ops.size() == 0)
|
|
|
|
state.batchCompleted(folder.id, priority, sequence);
|
2020-09-23 18:33:46 +00:00
|
|
|
else {
|
|
|
|
if (state.batchCanRun(folder.id, priority, sequence))
|
|
|
|
state.error(new OperationCanceledException("Processing"));
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
} finally {
|
2020-02-11 15:57:12 +00:00
|
|
|
Log.i(folder.name + " end process state=" + state + " pending=" + ops.size());
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
private static void ensureUid(Context context, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws MessagingException {
|
2020-12-17 14:05:44 +00:00
|
|
|
if (folder.local)
|
|
|
|
return;
|
2019-09-19 15:41:26 +00:00
|
|
|
if (message == null || message.uid != null)
|
2019-06-15 16:10:33 +00:00
|
|
|
return;
|
2019-09-27 11:05:13 +00:00
|
|
|
|
2019-06-15 16:10:33 +00:00
|
|
|
if (EntityOperation.ADD.equals(op.name))
|
|
|
|
return;
|
2019-09-17 14:01:29 +00:00
|
|
|
if (EntityOperation.FETCH.equals(op.name))
|
|
|
|
return;
|
2019-09-17 12:22:18 +00:00
|
|
|
if (EntityOperation.EXISTS.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))
|
2019-09-27 11:05:13 +00:00
|
|
|
throw new IllegalArgumentException("Message without msgid for " + op.name);
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2021-04-23 19:33:03 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2021-02-09 15:49:51 +00:00
|
|
|
Long uid = findUid(context, ifolder, message.msgid, false);
|
2021-04-23 19:33:03 +00:00
|
|
|
if (uid == null) {
|
2021-04-24 17:46:21 +00:00
|
|
|
if (EntityOperation.MOVE.equals(op.name) &&
|
|
|
|
EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
try {
|
|
|
|
long fid = new JSONArray(op.args).optLong(0, -1L);
|
|
|
|
EntityFolder target = db.folder().getFolder(fid);
|
2021-04-24 20:23:15 +00:00
|
|
|
if (target != null && EntityFolder.TRASH.equals(target.type)) {
|
|
|
|
Log.w(folder.name + " deleting id=" + message.id);
|
2021-04-24 17:46:21 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
2021-04-24 20:23:15 +00:00
|
|
|
}
|
2021-04-24 17:46:21 +00:00
|
|
|
} catch (JSONException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
2020-01-08 11:03:51 +00:00
|
|
|
throw new IllegalArgumentException("Message not found for " + op.name + " folder=" + folder.name);
|
2021-04-23 19:33:03 +00:00
|
|
|
}
|
2019-06-14 08:41:34 +00:00
|
|
|
|
2019-09-27 11:05:13 +00:00
|
|
|
db.message().setMessageUid(message.id, message.uid);
|
2019-11-10 10:25:13 +00:00
|
|
|
message.uid = uid;
|
2019-09-27 11:05:13 +00:00
|
|
|
}
|
2019-06-15 16:10:33 +00:00
|
|
|
|
2021-02-09 15:49:51 +00:00
|
|
|
private static Long findUid(Context context, IMAPFolder ifolder, String msgid, boolean purge) throws MessagingException {
|
2019-09-27 11:05:13 +00:00
|
|
|
String name = ifolder.getFullName();
|
|
|
|
Log.i(name + " searching for msgid=" + msgid);
|
2019-06-15 16:10:33 +00:00
|
|
|
|
2019-09-27 11:05:13 +00:00
|
|
|
Long uid = null;
|
|
|
|
|
|
|
|
Message[] imessages = ifolder.search(new MessageIDTerm(msgid));
|
|
|
|
if (imessages != null) {
|
|
|
|
for (Message iexisting : imessages) {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
2020-06-14 15:18:29 +00:00
|
|
|
if (muid < 0)
|
|
|
|
continue;
|
2019-09-27 11:05:13 +00:00
|
|
|
Log.i(name + " found uid=" + muid + " for msgid=" + msgid);
|
|
|
|
// RFC3501: Unique identifiers are assigned in a strictly ascending fashion
|
|
|
|
if (uid == null || muid > uid)
|
|
|
|
uid = muid;
|
|
|
|
}
|
|
|
|
|
2019-12-08 11:01:27 +00:00
|
|
|
if (uid != null && purge) {
|
2020-05-03 16:37:20 +00:00
|
|
|
boolean purged = false;
|
2019-09-27 11:05:13 +00:00
|
|
|
for (Message iexisting : imessages) {
|
|
|
|
long muid = ifolder.getUID(iexisting);
|
2020-06-14 15:18:29 +00:00
|
|
|
if (muid < 0)
|
|
|
|
continue;
|
2019-09-27 11:05:13 +00:00
|
|
|
if (muid != uid)
|
|
|
|
try {
|
|
|
|
Log.i(name + " deleting uid=" + muid + " for msgid=" + msgid);
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
2020-09-13 11:36:11 +00:00
|
|
|
purged = true;
|
2021-02-05 08:22:26 +00:00
|
|
|
} catch (MessagingException ignored) {
|
2019-09-27 11:05:13 +00:00
|
|
|
Log.w(name + " existing gone uid=" + muid + " for msgid=" + msgid);
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 08:22:26 +00:00
|
|
|
|
2021-02-09 15:49:51 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
if (purged && perform_expunge)
|
2021-02-05 08:22:26 +00:00
|
|
|
try {
|
|
|
|
ifolder.expunge();
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
// NO EXPUNGE failed.
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-12-08 11:01:27 +00:00
|
|
|
}
|
2019-09-27 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Log.i(name + " got uid=" + uid + " for msgid=" + msgid);
|
|
|
|
return 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-09-19 19:42:09 +00:00
|
|
|
private static void onSeen(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, POP3Folder ifolder) throws JSONException {
|
|
|
|
// Mark message (un)seen
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-09-19 19:47:19 +00:00
|
|
|
boolean seen = jargs.getBoolean(0);
|
|
|
|
db.message().setMessageUiSeen(folder.id, seen);
|
2019-09-19 19:42:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-09 16:39:11 +00:00
|
|
|
private static void onFlag(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, JSONException, IOException {
|
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);
|
2019-09-28 18:25:19 +00:00
|
|
|
db.message().setMessageUiFlagged(message.id, false, null);
|
2019-02-27 15:05:15 +00:00
|
|
|
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);
|
2021-02-09 16:39:11 +00:00
|
|
|
if (imessage instanceof GmailMessage)
|
|
|
|
((GmailMessage) imessage).setLabels(new String[]{"\\Starred"}, flagged);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
db.message().setMessageFlagged(message.id, flagged);
|
|
|
|
}
|
|
|
|
|
2019-09-28 08:05:45 +00:00
|
|
|
private static void onFlag(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, POP3Folder ifolder) throws MessagingException, JSONException {
|
|
|
|
// Star/unstar message
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
boolean flagged = jargs.getBoolean(0);
|
|
|
|
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-09-23 11:52:54 +00:00
|
|
|
// This will be fixed when moving the message
|
2019-03-08 15:26:12 +00:00
|
|
|
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
|
2020-06-27 09:02:29 +00:00
|
|
|
// https://tools.ietf.org/html/rfc3501#section-2.3.2
|
|
|
|
String keyword = jargs.getString(0);
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(keyword))
|
|
|
|
throw new IllegalArgumentException("keyword/empty");
|
2019-02-27 17:02:30 +00:00
|
|
|
|
2020-06-27 09:02:29 +00:00
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("keyword/uid");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-11-01 18:38:12 +00:00
|
|
|
if (!ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
|
2021-06-26 15:10:49 +00:00
|
|
|
if (MessageHelper.FLAG_FORWARDED.equals(keyword) && false) {
|
2020-11-01 18:38:12 +00:00
|
|
|
JSONArray janswered = new JSONArray();
|
|
|
|
janswered.put(true);
|
|
|
|
onAnswered(context, janswered, folder, message, ifolder);
|
|
|
|
}
|
2020-09-17 15:42:08 +00:00
|
|
|
return;
|
2020-11-01 18:38:12 +00:00
|
|
|
}
|
2020-09-17 15:42:08 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
|
|
|
|
|
|
|
Flags flags = new Flags(keyword);
|
|
|
|
imessage.setFlags(flags, set);
|
|
|
|
}
|
|
|
|
|
2020-06-25 07:14:05 +00:00
|
|
|
private static void onLabel(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
|
|
|
// Set/clear Gmail label
|
2020-06-27 09:00:13 +00:00
|
|
|
// Gmail does not push label changes
|
2020-06-25 07:14:05 +00:00
|
|
|
String label = jargs.getString(0);
|
|
|
|
boolean set = jargs.getBoolean(1);
|
|
|
|
|
2020-06-27 09:02:29 +00:00
|
|
|
if (TextUtils.isEmpty(label))
|
|
|
|
throw new IllegalArgumentException("label/empty");
|
|
|
|
|
|
|
|
if (message.uid == null)
|
|
|
|
throw new IllegalArgumentException("label/uid");
|
|
|
|
|
2020-06-27 09:00:13 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
if (!set && label.equals(folder.name)) {
|
2020-07-05 08:25:09 +00:00
|
|
|
if (TextUtils.isEmpty(message.msgid)) {
|
|
|
|
Log.w("label/msgid");
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 09:02:29 +00:00
|
|
|
|
2020-06-27 09:00:13 +00:00
|
|
|
// Prevent deleting message
|
|
|
|
EntityFolder archive = db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE);
|
2020-07-05 08:25:09 +00:00
|
|
|
if (archive == null) {
|
|
|
|
Log.w("label/archive");
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 09:00:13 +00:00
|
|
|
|
2021-01-02 07:48:54 +00:00
|
|
|
boolean archived;
|
2020-06-27 09:00:13 +00:00
|
|
|
Folder iarchive = istore.getFolder(archive.name);
|
|
|
|
try {
|
|
|
|
iarchive.open(Folder.READ_ONLY);
|
2021-01-02 07:48:54 +00:00
|
|
|
Message[] imessages = iarchive.search(new MessageIDTerm(message.msgid));
|
|
|
|
archived = (imessages != null && imessages.length > 0);
|
2020-06-27 09:00:13 +00:00
|
|
|
} finally {
|
|
|
|
if (iarchive.isOpen())
|
|
|
|
iarchive.close();
|
|
|
|
}
|
|
|
|
|
2021-01-02 07:48:54 +00:00
|
|
|
if (archived)
|
2020-06-27 09:00:13 +00:00
|
|
|
try {
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
2020-08-16 09:35:43 +00:00
|
|
|
if (imessage == null)
|
|
|
|
throw new MessageRemovedException();
|
2020-06-27 09:00:13 +00:00
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
2021-02-09 15:49:51 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
if (perform_expunge)
|
|
|
|
ifolder.expunge();
|
2020-06-27 09:00:13 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-07-05 08:25:09 +00:00
|
|
|
else {
|
|
|
|
Log.w("label/delete folder=" + folder.name);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-27 09:00:13 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage instanceof GmailMessage)
|
|
|
|
((GmailMessage) imessage).setLabels(new String[]{label}, set);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-06-25 17:53:30 +00:00
|
|
|
}
|
2020-06-25 15:55:59 +00:00
|
|
|
|
2020-06-27 06:16:34 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
List<EntityMessage> messages = db.message().getMessagesByMsgId(message.account, message.msgid);
|
|
|
|
if (messages == null)
|
|
|
|
return;
|
|
|
|
|
2020-06-25 17:53:30 +00:00
|
|
|
for (EntityMessage m : messages) {
|
|
|
|
EntityFolder f = db.folder().getFolder(m.folder);
|
|
|
|
if (!label.equals(f.name) && m.setLabel(label, set)) {
|
|
|
|
Log.i("Set " + label + "=" + set + " id=" + m.id + " folder=" + f.name);
|
|
|
|
db.message().setMessageLabels(m.id, DB.Converters.fromStringArray(m.labels));
|
|
|
|
}
|
|
|
|
}
|
2020-06-27 06:16:34 +00:00
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-06-25 07:14:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void onAdd(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, IMAPStore istore, IMAPFolder ifolder, State state) throws MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
// Add message
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2020-12-17 14:05:44 +00:00
|
|
|
if (folder.local) {
|
|
|
|
Log.i(folder.name + " local add");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-08 07:03:40 +00:00
|
|
|
// Drafts can change accounts
|
|
|
|
if (jargs.length() == 0 && !folder.id.equals(message.folder))
|
|
|
|
throw new IllegalArgumentException("Message folder changed");
|
|
|
|
|
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
|
|
|
// 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-07-29 14:42:17 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties();
|
2019-07-29 09:17:12 +00:00
|
|
|
Session isession = Session.getInstance(props, null);
|
2019-09-23 11:52:54 +00:00
|
|
|
Flags flags = ifolder.getPermanentFlags();
|
2019-07-29 09:17:12 +00:00
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Get raw message
|
|
|
|
MimeMessage imessage;
|
2020-07-03 08:40:10 +00:00
|
|
|
File file = message.getRawFile(context);
|
2019-06-14 06:31:37 +00:00
|
|
|
if (folder.id.equals(message.folder)) {
|
2019-07-27 09:42:19 +00:00
|
|
|
// Pre flight check
|
2019-06-14 06:31:37 +00:00
|
|
|
if (!message.content)
|
|
|
|
throw new IllegalArgumentException("Message body missing");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-04-02 09:33:01 +00:00
|
|
|
imessage = MessageHelper.from(context, message, null, isession, false);
|
2020-07-03 08:40:10 +00:00
|
|
|
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
} else {
|
|
|
|
// Cross account move
|
|
|
|
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);
|
2019-09-28 16:36:07 +00:00
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
2021-03-10 17:12:44 +00:00
|
|
|
imessage = new MimeMessageEx(isession, is, message.msgid);
|
2021-02-19 12:17:33 +00:00
|
|
|
}
|
2021-03-12 06:45:02 +00:00
|
|
|
|
|
|
|
imessage.removeHeader(MessageHelper.HEADER_CORRELATION_ID);
|
2021-03-11 19:37:01 +00:00
|
|
|
imessage.addHeader(MessageHelper.HEADER_CORRELATION_ID, message.msgid);
|
2021-03-12 06:45:02 +00:00
|
|
|
|
2021-03-10 17:12:44 +00:00
|
|
|
imessage.saveChanges();
|
2021-04-19 13:20:40 +00:00
|
|
|
|
2021-06-26 15:10:49 +00:00
|
|
|
if (flags.contains(Flags.Flag.SEEN))
|
|
|
|
imessage.setFlag(Flags.Flag.SEEN, message.ui_seen);
|
|
|
|
if (flags.contains(Flags.Flag.ANSWERED))
|
|
|
|
imessage.setFlag(Flags.Flag.ANSWERED, message.ui_answered);
|
|
|
|
if (flags.contains(Flags.Flag.FLAGGED))
|
|
|
|
imessage.setFlag(Flags.Flag.FLAGGED, message.ui_flagged);
|
|
|
|
if (flags.contains(Flags.Flag.DELETED))
|
|
|
|
imessage.setFlag(Flags.Flag.DELETED, message.ui_deleted);
|
|
|
|
|
|
|
|
if (flags.contains(Flags.Flag.USER)) {
|
|
|
|
if (message.isForwarded()) {
|
|
|
|
Flags fwd = new Flags(MessageHelper.FLAG_FORWARDED);
|
|
|
|
imessage.setFlags(new Flags(fwd), true);
|
|
|
|
}
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
}
|
2019-03-25 15:14:51 +00:00
|
|
|
|
2020-07-03 08:40:10 +00:00
|
|
|
db.message().setMessageRaw(message.id, true);
|
|
|
|
|
|
|
|
// Check size
|
|
|
|
if (account.max_size != null) {
|
|
|
|
long size = file.length();
|
|
|
|
if (size > account.max_size) {
|
|
|
|
String msg = "Too large" +
|
|
|
|
" size=" + Helper.humanReadableByteCount(size) +
|
|
|
|
"/" + Helper.humanReadableByteCount(account.max_size) +
|
|
|
|
" host=" + account.host;
|
2021-03-05 07:28:18 +00:00
|
|
|
Log.w(msg);
|
2020-07-03 08:40:10 +00:00
|
|
|
throw new IllegalArgumentException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 06:31:37 +00:00
|
|
|
// Handle auto read
|
2021-03-11 19:37:01 +00:00
|
|
|
if (flags.contains(Flags.Flag.SEEN))
|
2019-06-14 06:31:37 +00:00
|
|
|
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-09-23 11:52:54 +00:00
|
|
|
if (flags.contains(Flags.Flag.DRAFT))
|
2019-06-24 06:34:56 +00:00
|
|
|
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
|
2020-07-02 15:32:49 +00:00
|
|
|
Long newuid = null;
|
2020-08-28 10:02:08 +00:00
|
|
|
if (MessageHelper.hasCapability(ifolder, "UIDPLUS")) {
|
2020-07-02 15:32:49 +00:00
|
|
|
// https://tools.ietf.org/html/rfc4315
|
|
|
|
AppendUID[] uids = ifolder.appendUIDMessages(new Message[]{imessage});
|
2020-07-10 06:45:04 +00:00
|
|
|
if (uids != null && uids.length > 0 && uids[0] != null && uids[0].uid > 0) {
|
2020-07-02 15:32:49 +00:00
|
|
|
newuid = uids[0].uid;
|
|
|
|
Log.i(folder.name + " appended uid=" + newuid);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
ifolder.appendMessages(new Message[]{imessage});
|
2019-06-14 06:31:37 +00:00
|
|
|
|
2020-12-05 09:45:14 +00:00
|
|
|
if (folder.id.equals(message.folder)) {
|
|
|
|
// Prevent deleting message
|
2019-09-27 11:05:13 +00:00
|
|
|
db.message().setMessageUid(message.id, null);
|
2019-06-14 06:31:37 +00:00
|
|
|
|
2019-10-08 10:25:09 +00:00
|
|
|
// Some providers do not list the new message yet
|
2021-02-06 16:26:52 +00:00
|
|
|
try {
|
2021-02-09 15:49:51 +00:00
|
|
|
Long found = findUid(context, ifolder, message.msgid, true);
|
2021-02-06 16:26:52 +00:00
|
|
|
if (found != null)
|
|
|
|
if (newuid == null)
|
|
|
|
newuid = found;
|
|
|
|
else if (!newuid.equals(found)) {
|
|
|
|
Log.w(folder.name + " Added=" + newuid + " found=" + found);
|
2021-05-31 12:53:29 +00:00
|
|
|
try {
|
|
|
|
Message iprev = ifolder.getMessageByUID(Math.min(newuid, found));
|
|
|
|
if (iprev != null) {
|
|
|
|
iprev.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
if (perform_expunge)
|
|
|
|
ifolder.expunge();
|
|
|
|
}
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-02-06 16:26:52 +00:00
|
|
|
newuid = Math.max(newuid, found);
|
|
|
|
}
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-12-05 09:45:14 +00:00
|
|
|
|
2020-04-01 13:44:05 +00:00
|
|
|
if (newuid != null && (message.uid == null || newuid > message.uid))
|
|
|
|
try {
|
2020-12-03 12:51:31 +00:00
|
|
|
Log.i(folder.name + " Fetching uid=" + newuid);
|
2020-04-01 13:44:05 +00:00
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(newuid);
|
2020-09-21 19:46:39 +00:00
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
2020-12-03 12:51:31 +00:00
|
|
|
} catch (Throwable ex) {
|
2020-04-01 13:44:05 +00:00
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
} else {
|
2021-03-13 11:42:46 +00:00
|
|
|
// Lookup added message
|
2021-03-11 09:21:17 +00:00
|
|
|
int count = 0;
|
2021-03-13 06:59:40 +00:00
|
|
|
Long found = newuid;
|
2021-03-11 09:21:17 +00:00
|
|
|
while (found == null && count++ < FIND_RETRY_COUNT) {
|
|
|
|
found = findUid(context, ifolder, message.msgid, false);
|
|
|
|
if (found == null)
|
|
|
|
try {
|
|
|
|
Thread.sleep(FIND_RETRY_DELAY);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 05:42:06 +00:00
|
|
|
|
2021-03-11 06:32:43 +00:00
|
|
|
try {
|
2021-03-11 07:05:41 +00:00
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
if (found == null) {
|
2021-03-12 07:20:08 +00:00
|
|
|
db.message().setMessageError(message.id,
|
|
|
|
"Message not found in target folder " + account.name + "/" + folder.name);
|
2021-03-11 07:05:41 +00:00
|
|
|
db.message().setMessageUiHide(message.id, false);
|
|
|
|
} else {
|
|
|
|
// Mark source read
|
|
|
|
if (autoread)
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.SEEN, true);
|
|
|
|
|
|
|
|
// Delete source
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.DELETE);
|
|
|
|
}
|
2021-03-11 06:32:43 +00:00
|
|
|
|
2021-03-11 07:05:41 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-06-14 06:31:37 +00:00
|
|
|
|
2021-03-11 07:05:41 +00:00
|
|
|
// Fetch target
|
|
|
|
if (found != null)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " Fetching uid=" + found);
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(found);
|
|
|
|
onFetch(context, fargs, folder, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2019-04-19 11:33:41 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 21:03:55 +00:00
|
|
|
private static void onMove(Context context, JSONArray jargs, boolean copy, EntityAccount account, EntityFolder folder, List<EntityMessage> messages, IMAPStore istore, IMAPFolder ifolder, State state) 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-10-10 15:09:56 +00:00
|
|
|
boolean seen = jargs.optBoolean(1);
|
|
|
|
boolean unflag = jargs.optBoolean(3);
|
|
|
|
|
2019-09-23 11:52:54 +00:00
|
|
|
Flags flags = ifolder.getPermanentFlags();
|
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();
|
2020-10-15 09:30:25 +00:00
|
|
|
if (folder.id.equals(target.id))
|
2020-10-16 06:53:14 +00:00
|
|
|
throw new IllegalArgumentException("self");
|
2021-02-07 14:36:26 +00:00
|
|
|
if (!target.selectable)
|
2021-02-10 19:50:07 +00:00
|
|
|
throw new IllegalArgumentException("not selectable type=" + target.type);
|
2020-10-15 09:30:25 +00:00
|
|
|
|
2021-01-02 19:24:17 +00:00
|
|
|
// De-classify
|
2021-06-23 21:04:21 +00:00
|
|
|
if (!copy)
|
|
|
|
for (EntityMessage message : messages)
|
|
|
|
MessageClassifier.classify(message, folder, target, context);
|
2021-01-02 19:24:17 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
IMAPFolder itarget = (IMAPFolder) istore.getFolder(target.name);
|
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
// Get source messages
|
|
|
|
Map<Message, EntityMessage> map = new HashMap<>();
|
2021-06-23 21:03:55 +00:00
|
|
|
Map<EntityMessage, String> msgids = new HashMap<>();
|
2019-09-28 11:49:56 +00:00
|
|
|
for (EntityMessage message : messages)
|
2019-11-24 11:33:50 +00:00
|
|
|
try {
|
2019-12-01 11:52:40 +00:00
|
|
|
if (message.uid == null)
|
2019-12-03 06:34:00 +00:00
|
|
|
throw new IllegalArgumentException("move without uid");
|
2019-11-24 11:33:50 +00:00
|
|
|
Message imessage = ifolder.getMessageByUID(message.uid);
|
|
|
|
if (imessage == null)
|
2019-12-01 11:52:40 +00:00
|
|
|
throw new MessageRemovedException("move without message");
|
2019-11-24 11:33:50 +00:00
|
|
|
map.put(imessage, message);
|
|
|
|
} catch (MessageRemovedException ex) {
|
2019-12-01 11:52:40 +00:00
|
|
|
Log.e(ex);
|
2019-11-24 11:33:50 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
}
|
2019-09-28 11:49:56 +00:00
|
|
|
|
2020-08-26 08:38:15 +00:00
|
|
|
// Some servers return different capabilities for different sessions
|
2020-08-28 10:02:08 +00:00
|
|
|
boolean canMove = MessageHelper.hasCapability(ifolder, "MOVE");
|
2020-08-07 18:49:27 +00:00
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
// Some providers do not support the COPY operation for drafts
|
2019-10-02 08:25:00 +00:00
|
|
|
boolean draft = (EntityFolder.DRAFTS.equals(folder.type) || EntityFolder.DRAFTS.equals(target.type));
|
2021-06-23 21:03:55 +00:00
|
|
|
boolean duplicate = (copy && !account.isGmail());
|
|
|
|
if (draft || duplicate) {
|
2019-06-24 07:07:43 +00:00
|
|
|
Log.i(folder.name + " move from " + folder.type + " to " + target.type);
|
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
List<Message> icopies = new ArrayList<>();
|
|
|
|
for (Message imessage : map.keySet()) {
|
|
|
|
EntityMessage message = map.get(imessage);
|
|
|
|
|
|
|
|
File file = File.createTempFile("draft", "." + message.id, context.getCacheDir());
|
2019-09-28 16:36:07 +00:00
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
2019-09-28 11:49:56 +00:00
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
2019-06-24 07:07:43 +00:00
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
Properties props = MessageHelper.getSessionProperties();
|
|
|
|
Session isession = Session.getInstance(props, null);
|
2019-07-29 09:17:12 +00:00
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
Message icopy;
|
|
|
|
try (InputStream is = new BufferedInputStream(new FileInputStream(file))) {
|
2021-06-23 21:03:55 +00:00
|
|
|
if (duplicate) {
|
|
|
|
String msgid = EntityMessage.generateMessageId();
|
|
|
|
msgids.put(message, msgid);
|
|
|
|
icopy = new MimeMessageEx(isession, is, msgid);
|
|
|
|
icopy.saveChanges();
|
|
|
|
} else
|
|
|
|
icopy = new MimeMessage(isession, is);
|
2019-09-28 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
file.delete();
|
2019-06-24 07:07:43 +00:00
|
|
|
|
2020-07-27 12:53:54 +00:00
|
|
|
for (Flags.Flag flag : imessage.getFlags().getSystemFlags())
|
|
|
|
icopy.setFlag(flag, true);
|
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
icopies.add(icopy);
|
|
|
|
}
|
2019-06-24 11:16:32 +00:00
|
|
|
|
2019-09-28 11:49:56 +00:00
|
|
|
itarget.appendMessages(icopies.toArray(new Message[0]));
|
2019-06-24 11:16:32 +00:00
|
|
|
} else {
|
2019-09-28 11:49:56 +00:00
|
|
|
for (Message imessage : map.keySet()) {
|
2020-11-07 13:26:00 +00:00
|
|
|
Log.i((copy ? "Copy" : "Move") + " seen=" + seen + " unflag=" + unflag + " flags=" + imessage.getFlags() + " can=" + canMove);
|
2019-06-24 11:16:32 +00:00
|
|
|
|
2019-10-10 15:09:56 +00:00
|
|
|
// Mark read
|
2020-08-07 18:52:20 +00:00
|
|
|
if (seen && !imessage.isSet(Flags.Flag.SEEN) && flags.contains(Flags.Flag.SEEN))
|
2019-10-10 15:09:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.SEEN, true);
|
2019-09-23 11:52:54 +00:00
|
|
|
|
2019-10-10 15:09:56 +00:00
|
|
|
// Remove star
|
2020-08-07 18:52:20 +00:00
|
|
|
if (unflag && imessage.isSet(Flags.Flag.FLAGGED) && flags.contains(Flags.Flag.FLAGGED))
|
2019-10-10 15:09:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.FLAGGED, false);
|
2021-06-30 19:35:00 +00:00
|
|
|
|
|
|
|
// Mark not spam
|
|
|
|
if (EntityFolder.JUNK.equals(folder.type)
|
|
|
|
&& ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
|
2021-07-01 06:00:04 +00:00
|
|
|
Flags notJunk = new Flags(MessageHelper.FLAG_NOT_JUNK);
|
2021-06-30 19:35:00 +00:00
|
|
|
imessage.setFlags(notJunk, true);
|
|
|
|
}
|
2019-09-28 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 18:49:27 +00:00
|
|
|
// https://tools.ietf.org/html/rfc6851
|
|
|
|
if (!copy && canMove)
|
|
|
|
ifolder.moveMessages(map.keySet().toArray(new Message[0]), itarget);
|
|
|
|
else
|
|
|
|
ifolder.copyMessages(map.keySet().toArray(new Message[0]), 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
|
2020-08-07 18:49:27 +00:00
|
|
|
if (!copy && (draft || !canMove)) {
|
2019-03-25 15:14:51 +00:00
|
|
|
try {
|
2020-07-16 17:42:17 +00:00
|
|
|
for (Message imessage : map.keySet())
|
2019-09-28 11:49:56 +00:00
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
2021-02-09 15:49:51 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
if (perform_expunge)
|
|
|
|
ifolder.expunge();
|
2020-09-13 13:01:57 +00:00
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(ex);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-08-07 18:58:42 +00:00
|
|
|
} else {
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2020-08-07 18:58:42 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2019-09-12 15:44:36 +00:00
|
|
|
|
2019-09-27 13:32:29 +00:00
|
|
|
// Fetch appended/copied when needed
|
2020-12-28 11:21:00 +00:00
|
|
|
boolean fetch = (copy ||
|
|
|
|
!"connected".equals(target.state) ||
|
|
|
|
!MessageHelper.hasCapability(ifolder, "IDLE"));
|
2020-01-17 09:22:36 +00:00
|
|
|
if (draft || fetch)
|
2019-09-27 12:14:04 +00:00
|
|
|
try {
|
2020-01-17 09:22:36 +00:00
|
|
|
Log.i(target.name + " moved message fetch=" + fetch);
|
2019-09-27 13:32:29 +00:00
|
|
|
itarget.open(READ_WRITE);
|
2019-09-28 11:49:56 +00:00
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
boolean sync = false;
|
2019-09-28 11:49:56 +00:00
|
|
|
for (EntityMessage message : map.values())
|
2021-02-06 17:30:26 +00:00
|
|
|
try {
|
2021-06-23 21:03:55 +00:00
|
|
|
String msgid = msgids.get(message);
|
|
|
|
if (msgid == null)
|
|
|
|
msgid = message.msgid;
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(msgid))
|
2021-02-06 17:30:26 +00:00
|
|
|
throw new IllegalArgumentException("move: msgid missing");
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-06-23 21:03:55 +00:00
|
|
|
Long uid = findUid(context, itarget, msgid, false);
|
2021-02-06 17:30:26 +00:00
|
|
|
if (uid == null)
|
|
|
|
throw new IllegalArgumentException("move: uid not found");
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-06-30 11:25:31 +00:00
|
|
|
if (draft || duplicate) {
|
2021-02-06 17:30:26 +00:00
|
|
|
Message icopy = itarget.getMessageByUID(uid);
|
2021-06-30 11:25:31 +00:00
|
|
|
if (icopy == null)
|
2021-02-06 17:30:26 +00:00
|
|
|
throw new IllegalArgumentException("move: gone uid=" + uid);
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
// Mark read
|
|
|
|
if (seen && !icopy.isSet(Flags.Flag.SEEN) && flags.contains(Flags.Flag.SEEN))
|
|
|
|
icopy.setFlag(Flags.Flag.SEEN, true);
|
2019-10-02 08:25:00 +00:00
|
|
|
|
2021-02-06 17:30:26 +00:00
|
|
|
// Remove star
|
|
|
|
if (unflag && icopy.isSet(Flags.Flag.FLAGGED) && flags.contains(Flags.Flag.FLAGGED))
|
|
|
|
icopy.setFlag(Flags.Flag.FLAGGED, false);
|
|
|
|
|
|
|
|
// Set drafts flag
|
|
|
|
if (flags.contains(Flags.Flag.DRAFT))
|
|
|
|
icopy.setFlag(Flags.Flag.DRAFT, EntityFolder.DRAFTS.equals(target.type));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetch) {
|
|
|
|
Log.i(target.name + " Fetching uid=" + uid);
|
|
|
|
JSONArray fargs = new JSONArray();
|
|
|
|
fargs.put(uid);
|
|
|
|
onFetch(context, fargs, target, istore, itarget, state);
|
2019-09-28 11:49:56 +00:00
|
|
|
}
|
2021-02-06 17:30:26 +00:00
|
|
|
} catch (Throwable ex) {
|
2021-02-07 17:15:26 +00:00
|
|
|
if (ex instanceof IllegalArgumentException)
|
|
|
|
Log.i(ex);
|
|
|
|
else
|
|
|
|
Log.e(ex);
|
2021-02-06 17:30:26 +00:00
|
|
|
if (fetch)
|
|
|
|
sync = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sync)
|
|
|
|
EntityOperation.sync(context, target.id, false);
|
2019-09-27 13:32:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
2019-09-28 11:49:56 +00:00
|
|
|
} finally {
|
|
|
|
if (itarget.isOpen())
|
|
|
|
itarget.close();
|
2019-09-27 12:14:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-12 15:44:36 +00:00
|
|
|
// Delete junk contacts
|
2019-09-28 11:49:56 +00:00
|
|
|
if (EntityFolder.JUNK.equals(target.type))
|
|
|
|
for (EntityMessage message : map.values()) {
|
|
|
|
Address[] recipients = (message.reply != null ? message.reply : message.from);
|
|
|
|
if (recipients != null)
|
|
|
|
for (Address recipient : recipients) {
|
|
|
|
String email = ((InternetAddress) recipient).getAddress();
|
|
|
|
int count = db.contact().deleteContact(target.account, EntityContact.TYPE_FROM, email);
|
|
|
|
Log.i("Deleted contact email=" + email + " count=" + count);
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 12:11:50 +00:00
|
|
|
private static void onMove(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message) throws JSONException, FolderNotFoundException {
|
|
|
|
// Move message
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
// Get arguments
|
|
|
|
long id = jargs.getLong(0);
|
|
|
|
boolean seen = jargs.optBoolean(1);
|
|
|
|
boolean unflag = jargs.optBoolean(3);
|
|
|
|
|
|
|
|
// Get target folder
|
|
|
|
EntityFolder target = db.folder().getFolder(id);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
2020-10-15 09:30:25 +00:00
|
|
|
if (folder.id.equals(target.id))
|
2020-10-16 06:53:14 +00:00
|
|
|
throw new IllegalArgumentException("self");
|
2020-04-06 12:11:50 +00:00
|
|
|
|
2020-04-12 13:30:33 +00:00
|
|
|
// Move from trash/drafts only
|
|
|
|
if (!EntityFolder.TRASH.equals(folder.type) &&
|
|
|
|
!EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
throw new IllegalArgumentException("Invalid POP3 folder" +
|
|
|
|
" source=" + folder.type + " target=" + target.type);
|
|
|
|
|
2020-04-06 12:11:50 +00:00
|
|
|
message.folder = target.id;
|
|
|
|
if (seen)
|
|
|
|
message.ui_seen = seen;
|
|
|
|
if (unflag)
|
|
|
|
message.ui_flagged = false;
|
|
|
|
message.ui_hide = false;
|
|
|
|
|
|
|
|
db.message().updateMessage(message);
|
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void onFetch(Context context, JSONArray jargs, EntityFolder folder, IMAPStore istore, IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
2019-09-17 11:38:23 +00:00
|
|
|
long uid = jargs.getLong(0);
|
2020-08-09 05:35:53 +00:00
|
|
|
boolean removed = jargs.optBoolean(1);
|
|
|
|
|
2020-06-14 15:18:29 +00:00
|
|
|
if (uid < 0)
|
2020-07-16 18:11:07 +00:00
|
|
|
throw new MessageRemovedException(folder.name + " fetch uid=" + uid);
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2019-02-27 17:02:30 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2019-09-18 16:30:12 +00:00
|
|
|
EntityAccount account = db.account().getAccount(folder.account);
|
2020-11-05 20:44:13 +00:00
|
|
|
if (account == null)
|
|
|
|
throw new IllegalArgumentException("account missing");
|
2019-02-27 17:02:30 +00:00
|
|
|
|
2019-09-18 16:30:12 +00:00
|
|
|
try {
|
2021-01-02 07:50:28 +00:00
|
|
|
if (removed) {
|
|
|
|
db.message().deleteMessage(folder.id, uid);
|
2020-08-09 05:35:53 +00:00
|
|
|
throw new MessageRemovedException("removed uid=" + uid);
|
2021-01-02 07:50:28 +00:00
|
|
|
}
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2019-09-26 08:53:27 +00:00
|
|
|
MimeMessage imessage = (MimeMessage) ifolder.getMessageByUID(uid);
|
2019-09-18 16:30:12 +00:00
|
|
|
if (imessage == null)
|
2020-07-16 18:11:07 +00:00
|
|
|
throw new MessageRemovedException(folder.name + " fetch not found uid=" + uid);
|
2021-06-18 08:17:10 +00:00
|
|
|
// synchronizeMessage will check expunged/deleted
|
2019-09-18 16:30:12 +00:00
|
|
|
|
2020-08-09 05:35:53 +00:00
|
|
|
SyncStats stats = new SyncStats();
|
|
|
|
boolean download = db.folder().getFolderDownload(folder.id);
|
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id);
|
|
|
|
|
2021-06-18 09:42:32 +00:00
|
|
|
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);
|
|
|
|
if (account.isGmail()) {
|
|
|
|
fp.add(GmailFolder.FetchProfileItem.THRID);
|
|
|
|
fp.add(GmailFolder.FetchProfileItem.LABELS);
|
|
|
|
}
|
|
|
|
ifolder.fetch(new Message[]{imessage}, fp);
|
2020-06-25 17:53:30 +00:00
|
|
|
|
2021-06-18 09:42:32 +00:00
|
|
|
EntityMessage message = synchronizeMessage(context, account, folder, istore, ifolder, imessage, false, download, rules, state, stats);
|
|
|
|
if (message != null) {
|
|
|
|
if (account.isGmail() && EntityFolder.USER.equals(folder.type))
|
|
|
|
try {
|
|
|
|
JSONArray jlabel = new JSONArray();
|
|
|
|
jlabel.put(0, folder.name);
|
|
|
|
jlabel.put(1, true);
|
|
|
|
onLabel(context, jlabel, folder, message, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex1) {
|
|
|
|
Log.e(ex1);
|
|
|
|
}
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2021-06-18 09:42:32 +00:00
|
|
|
if (download)
|
|
|
|
downloadMessage(context, account, folder, istore, ifolder, imessage, message.id, state, stats);
|
2019-09-18 16:30:12 +00:00
|
|
|
}
|
2021-06-18 09:42:32 +00:00
|
|
|
|
|
|
|
if (!stats.isEmpty())
|
|
|
|
EntityLog.log(context, account.name + "/" + folder.name + " fetch stats " + stats);
|
2021-05-03 16:30:49 +00:00
|
|
|
} catch (MessageRemovedException | MessageRemovedIOException ex) {
|
2019-09-18 16:30:12 +00:00
|
|
|
Log.i(ex);
|
2020-06-25 17:53:30 +00:00
|
|
|
|
|
|
|
if (account.isGmail() && EntityFolder.USER.equals(folder.type)) {
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
if (message != null)
|
|
|
|
try {
|
|
|
|
JSONArray jlabel = new JSONArray();
|
|
|
|
jlabel.put(0, folder.name);
|
|
|
|
jlabel.put(1, false);
|
|
|
|
onLabel(context, jlabel, folder, message, istore, ifolder, state);
|
|
|
|
} catch (Throwable ex1) {
|
|
|
|
Log.e(ex1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 16:30:12 +00:00
|
|
|
db.message().deleteMessage(folder.id, uid);
|
2019-09-17 11:38:23 +00:00
|
|
|
} finally {
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2019-09-17 11:38:23 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2019-06-14 08:12:08 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
private static void onDelete(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException {
|
|
|
|
// Delete message
|
|
|
|
DB db = DB.getInstance(context);
|
2021-02-11 08:51:48 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
2020-12-17 14:05:44 +00:00
|
|
|
|
|
|
|
if (folder.local) {
|
|
|
|
Log.i(folder.name + " local delete");
|
|
|
|
db.message().deleteMessage(message.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
try {
|
2021-02-23 14:59:15 +00:00
|
|
|
boolean deleted = false;
|
2019-09-17 11:38:23 +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
|
2019-06-14 08:12:08 +00:00
|
|
|
try {
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " deleting uid=" + message.uid);
|
2021-02-11 09:41:07 +00:00
|
|
|
if (perform_expunge)
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
else
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, message.ui_deleted);
|
2021-02-23 14:59:15 +00:00
|
|
|
deleted = true;
|
2019-06-14 08:12:08 +00:00
|
|
|
} catch (MessageRemovedException ignored) {
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.w(folder.name + " existing gone uid=" + message.uid);
|
2019-06-14 08:12:08 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-04-11 11:02:56 +00:00
|
|
|
|
2021-02-23 14:59:15 +00:00
|
|
|
if (!deleted && !TextUtils.isEmpty(message.msgid))
|
2020-10-04 17:57:19 +00:00
|
|
|
try {
|
|
|
|
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 {
|
2021-02-11 09:41:07 +00:00
|
|
|
if (perform_expunge)
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
else
|
|
|
|
iexisting.setFlag(Flags.Flag.DELETED, message.ui_deleted);
|
2021-02-23 14:59:15 +00:00
|
|
|
deleted = true;
|
2020-10-04 17:57:19 +00:00
|
|
|
} catch (MessageRemovedException ignored) {
|
|
|
|
Log.w(folder.name + " existing gone uid=" + muid);
|
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2020-10-04 17:57:19 +00:00
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2021-02-11 08:51:48 +00:00
|
|
|
if (perform_expunge) {
|
2021-02-23 14:59:15 +00:00
|
|
|
if (deleted)
|
2021-02-09 15:49:51 +00:00
|
|
|
ifolder.expunge(); // NO EXPUNGE failed.
|
2021-02-11 08:51:48 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
2021-02-11 09:41:07 +00:00
|
|
|
} else {
|
2021-02-23 14:59:15 +00:00
|
|
|
if (deleted)
|
2021-02-11 09:41:07 +00:00
|
|
|
db.message().setMessageDeleted(message.id, message.ui_deleted);
|
2021-02-09 15:49:51 +00:00
|
|
|
}
|
2021-02-11 09:41:07 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
} finally {
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2019-09-17 11:38:23 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void onDelete(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, POP3Folder ifolder, POP3Store istore, State state) throws MessagingException, IOException {
|
2019-09-19 19:42:09 +00:00
|
|
|
// Delete message
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2020-05-03 08:03:23 +00:00
|
|
|
if (EntityFolder.INBOX.equals(folder.type)) {
|
2020-10-24 15:55:46 +00:00
|
|
|
if (account.leave_deleted) {
|
|
|
|
// Remove message/attachments files on cleanup
|
|
|
|
db.message().resetMessageContent(message.id);
|
|
|
|
db.attachment().resetAvailable(message.id);
|
|
|
|
} else {
|
2020-05-03 08:03:23 +00:00
|
|
|
Map<String, String> caps = istore.capabilities();
|
|
|
|
|
|
|
|
Message[] imessages = ifolder.getMessages();
|
|
|
|
Log.i(folder.name + " POP messages=" + imessages.length);
|
|
|
|
|
2020-08-20 16:27:05 +00:00
|
|
|
boolean hasUidl = caps.containsKey("UIDL");
|
|
|
|
if (hasUidl) {
|
|
|
|
FetchProfile ifetch = new FetchProfile();
|
|
|
|
ifetch.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(imessages, ifetch);
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:03:23 +00:00
|
|
|
boolean found = false;
|
|
|
|
for (Message imessage : imessages) {
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
2020-05-03 08:03:23 +00:00
|
|
|
|
2020-08-20 16:27:05 +00:00
|
|
|
String uidl = (hasUidl ? ifolder.getUID(imessage) : null);
|
|
|
|
String msgid = helper.getMessageID();
|
2020-05-03 08:03:23 +00:00
|
|
|
|
2020-08-20 16:27:05 +00:00
|
|
|
Log.i(folder.name + " POP searching=" + message.uidl + "/" + message.msgid +
|
|
|
|
" iterate=" + uidl + "/" + msgid);
|
|
|
|
if ((uidl != null && uidl.equals(message.uidl)) ||
|
|
|
|
(msgid != null && msgid.equals(message.msgid))) {
|
2020-05-03 08:03:23 +00:00
|
|
|
found = true;
|
2020-08-20 16:27:05 +00:00
|
|
|
Log.i(folder.name + " POP delete=" + uidl + "/" + msgid);
|
2020-05-03 08:03:23 +00:00
|
|
|
imessage.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
break;
|
|
|
|
}
|
2019-09-20 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 16:03:14 +00:00
|
|
|
if (found) {
|
2020-05-03 08:03:23 +00:00
|
|
|
try {
|
2020-05-05 16:03:14 +00:00
|
|
|
Log.i(folder.name + " POP expunge=" + found);
|
2020-05-03 08:03:23 +00:00
|
|
|
ifolder.close(true);
|
|
|
|
ifolder.open(Folder.READ_WRITE);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2020-10-30 14:42:44 +00:00
|
|
|
state.error(new FolderClosedException(ifolder, "POP", new Exception(ex)));
|
2020-05-03 08:03:23 +00:00
|
|
|
}
|
2020-05-05 16:03:14 +00:00
|
|
|
}
|
2020-05-03 08:03:23 +00:00
|
|
|
}
|
2020-05-05 16:03:14 +00:00
|
|
|
|
|
|
|
// Synchronize will delete messages when needed
|
|
|
|
db.message().setMessageUiHide(message.id, true);
|
2020-05-03 08:03:23 +00:00
|
|
|
} else
|
2021-01-02 14:57:24 +00:00
|
|
|
db.message().deleteMessage(message.id);
|
2020-04-05 19:35:56 +00:00
|
|
|
|
2020-04-29 07:43:56 +00:00
|
|
|
if (!EntityFolder.DRAFTS.equals(folder.type) &&
|
|
|
|
!EntityFolder.TRASH.equals(folder.type)) {
|
2020-04-05 19:35:56 +00:00
|
|
|
EntityFolder trash = db.folder().getFolderByType(message.account, EntityFolder.TRASH);
|
|
|
|
if (trash == null) {
|
|
|
|
trash = new EntityFolder();
|
|
|
|
trash.account = account.id;
|
|
|
|
trash.name = context.getString(R.string.title_folder_trash);
|
|
|
|
trash.type = EntityFolder.TRASH;
|
|
|
|
trash.synchronize = false;
|
|
|
|
trash.unified = false;
|
|
|
|
trash.notify = false;
|
|
|
|
trash.sync_days = Integer.MAX_VALUE;
|
|
|
|
trash.keep_days = Integer.MAX_VALUE;
|
|
|
|
trash.initialize = 0;
|
|
|
|
trash.id = db.folder().insertFolder(trash);
|
|
|
|
}
|
|
|
|
|
|
|
|
long id = message.id;
|
2019-09-20 08:21:38 +00:00
|
|
|
|
2020-04-05 19:35:56 +00:00
|
|
|
message.id = null;
|
|
|
|
message.folder = trash.id;
|
2020-04-06 12:11:50 +00:00
|
|
|
message.msgid = null; // virtual message
|
2020-04-05 19:35:56 +00:00
|
|
|
message.ui_hide = false;
|
|
|
|
message.ui_seen = true;
|
|
|
|
message.id = db.message().insertMessage(message);
|
|
|
|
|
|
|
|
try {
|
|
|
|
File source = EntityMessage.getFile(context, id);
|
|
|
|
File target = message.getFile(context);
|
|
|
|
Helper.copy(source, target);
|
|
|
|
} catch (IOException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityAttachment.copy(context, id, message.id);
|
|
|
|
}
|
2019-09-19 19:42:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:43:13 +00:00
|
|
|
private static void onHeaders(Context context, JSONArray jargs, EntityFolder folder, EntityMessage message, IMAPFolder ifolder) throws MessagingException, IOException {
|
2019-02-27 17:02:30 +00:00
|
|
|
// 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();
|
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage, context);
|
2019-02-27 15:05:15 +00:00
|
|
|
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-09-28 16:36:07 +00:00
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
2019-02-27 15:05:15 +00:00
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
2019-09-28 16:36:07 +00:00
|
|
|
|
|
|
|
db.message().setMessageRaw(message.id, true);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (jargs.length() > 0) {
|
2019-04-28 06:28:41 +00:00
|
|
|
// Cross account move
|
2020-02-06 19:08:34 +00:00
|
|
|
long tid = jargs.getLong(0);
|
|
|
|
EntityFolder target = db.folder().getFolder(tid);
|
|
|
|
if (target == null)
|
|
|
|
throw new FolderNotFoundException();
|
|
|
|
|
|
|
|
Log.i(folder.name + " queuing ADD id=" + message.id + ":" + target.id);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
EntityOperation operation = new EntityOperation();
|
2020-02-06 19:08:34 +00:00
|
|
|
operation.account = target.account;
|
|
|
|
operation.folder = target.id;
|
2019-02-27 15:05:15 +00:00
|
|
|
operation.message = message.id;
|
|
|
|
operation.name = EntityOperation.ADD;
|
|
|
|
operation.args = jargs.toString();
|
|
|
|
operation.created = new Date().getTime();
|
|
|
|
operation.id = db.operation().insertOperation(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 12:22:18 +00:00
|
|
|
private static void onBody(Context context, JSONArray jargs, 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();
|
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-02-27 15:05:15 +00:00
|
|
|
String body = parts.getHtml(context);
|
2020-02-20 09:35:01 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
2021-01-19 10:57:59 +00:00
|
|
|
String text = HtmlHelper.getFullText(body);
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
2021-01-25 13:49:56 +00:00
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
2019-05-04 18:52:21 +00:00
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
2021-01-19 11:18:03 +00:00
|
|
|
message.language,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.isPlainOnly(),
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.getWarnings(message.warning));
|
2021-01-05 18:51:22 +00:00
|
|
|
MessageClassifier.classify(message, folder, null, context);
|
2020-07-15 16:58:03 +00:00
|
|
|
|
|
|
|
if (body != null)
|
|
|
|
EntityLog.log(context, "Operation body size=" + body.length());
|
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");
|
2020-10-18 09:03:29 +00:00
|
|
|
if (attachment.subsequence != null)
|
|
|
|
throw new IllegalArgumentException("Download of sub attachment");
|
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
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-05-12 13:29:18 +00:00
|
|
|
|
2019-06-26 19:21:09 +00:00
|
|
|
// Download attachment
|
2021-03-18 16:46:20 +00:00
|
|
|
parts.downloadAttachment(context, attachment);
|
2020-07-15 16:58:03 +00:00
|
|
|
|
|
|
|
if (attachment.size != null)
|
|
|
|
EntityLog.log(context, "Operation attachment size=" + attachment.size);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
private static void onExists(Context context, JSONArray jargs, EntityAccount account, EntityFolder folder, EntityMessage message, EntityOperation op, IMAPFolder ifolder) throws MessagingException, IOException {
|
|
|
|
boolean retry = jargs.optBoolean(0);
|
|
|
|
|
2019-09-17 12:22:18 +00:00
|
|
|
if (message.uid != null)
|
|
|
|
return;
|
|
|
|
|
2019-12-01 13:43:17 +00:00
|
|
|
if (message.msgid == null)
|
|
|
|
throw new IllegalArgumentException("exists without msgid");
|
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
// Search for message
|
2019-09-17 12:22:18 +00:00
|
|
|
Message[] imessages = ifolder.search(new MessageIDTerm(message.msgid));
|
2020-06-08 17:36:45 +00:00
|
|
|
if (imessages == null || imessages.length == 0)
|
|
|
|
try {
|
|
|
|
// Needed for Outlook
|
|
|
|
imessages = ifolder.search(
|
|
|
|
new AndTerm(
|
|
|
|
new SentDateTerm(ComparisonTerm.GE, new Date()),
|
2020-06-22 10:08:12 +00:00
|
|
|
new HeaderTerm(MessageHelper.HEADER_CORRELATION_ID, message.msgid)));
|
2021-01-29 11:24:44 +00:00
|
|
|
} catch (Throwable ex) {
|
2020-06-08 17:36:45 +00:00
|
|
|
Log.e(ex);
|
2021-01-29 11:24:44 +00:00
|
|
|
// Seznam: Jakarta Mail Exception: java.io.IOException: Connection dropped by server?
|
2020-06-08 17:36:45 +00:00
|
|
|
}
|
2020-11-26 17:07:36 +00:00
|
|
|
|
2021-02-22 19:11:34 +00:00
|
|
|
// Some email servers are slow with adding sent messages
|
2021-02-24 13:05:11 +00:00
|
|
|
if (retry)
|
|
|
|
Log.w(folder.name + " EXISTS retry" +
|
2021-02-22 19:11:34 +00:00
|
|
|
" found=" + (imessages == null ? null : imessages.length) +
|
2021-02-24 13:05:11 +00:00
|
|
|
" host=" + account.host);
|
|
|
|
else if (imessages == null || imessages.length == 0) {
|
2021-02-22 19:11:34 +00:00
|
|
|
long next = new Date().getTime() + EXISTS_RETRY_DELAY;
|
|
|
|
|
2021-05-10 12:52:39 +00:00
|
|
|
Intent intent = new Intent(context, ServiceSynchronize.class);
|
2021-02-22 19:11:34 +00:00
|
|
|
intent.setAction("exists:" + message.id);
|
2021-03-30 07:36:51 +00:00
|
|
|
PendingIntent piExists = PendingIntentCompat.getForegroundService(
|
2021-05-10 12:52:39 +00:00
|
|
|
context, ServiceSynchronize.PI_EXISTS, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2021-02-22 19:11:34 +00:00
|
|
|
|
|
|
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
2021-04-27 05:53:09 +00:00
|
|
|
AlarmManagerCompatEx.setAndAllowWhileIdle(context, am, AlarmManager.RTC_WAKEUP, next, piExists);
|
2021-02-22 19:11:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-26 17:07:36 +00:00
|
|
|
if (imessages != null && imessages.length == 1) {
|
2020-12-23 17:09:38 +00:00
|
|
|
String msgid;
|
|
|
|
try {
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessages[0], context);
|
|
|
|
msgid = helper.getMessageID();
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
msgid = message.msgid;
|
|
|
|
}
|
|
|
|
if (Objects.equals(message.msgid, msgid)) {
|
|
|
|
long uid = ifolder.getUID(imessages[0]);
|
|
|
|
EntityOperation.queue(context, folder, EntityOperation.FETCH, uid);
|
|
|
|
} else {
|
|
|
|
EntityOperation.queue(context, message, EntityOperation.ADD);
|
|
|
|
}
|
2020-11-26 17:07:36 +00:00
|
|
|
} else {
|
|
|
|
if (imessages != null && imessages.length > 1)
|
2021-02-22 19:11:34 +00:00
|
|
|
Log.e(folder.name + " EXISTS messages=" + imessages.length + " retry=" + retry);
|
2021-01-29 10:02:00 +00:00
|
|
|
EntityLog.log(context, folder.name + " EXISTS messages=" + imessages.length);
|
2020-11-26 17:07:36 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.ADD);
|
2019-09-17 12:22:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 06:44:50 +00:00
|
|
|
static void onSynchronizeFolders(
|
|
|
|
Context context, EntityAccount account, Store istore,
|
|
|
|
State state, boolean force) 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);
|
2021-02-25 11:31:34 +00:00
|
|
|
boolean sync_folders = prefs.getBoolean("sync_folders", true);
|
2020-06-09 14:46:37 +00:00
|
|
|
boolean sync_shared_folders = prefs.getBoolean("sync_shared_folders", false);
|
2021-02-25 11:31:34 +00:00
|
|
|
Log.i(account.name + " sync folders=" + sync_folders + " shared=" + sync_shared_folders + " force=" + force);
|
|
|
|
|
|
|
|
if (force)
|
|
|
|
sync_folders = true;
|
2019-04-04 16:18:13 +00:00
|
|
|
|
2019-05-01 07:23:31 +00:00
|
|
|
// Get folder names
|
2020-12-17 14:05:44 +00:00
|
|
|
boolean drafts = false;
|
2019-06-25 14:34:30 +00:00
|
|
|
Map<String, EntityFolder> local = new HashMap<>();
|
2020-12-17 14:05:44 +00:00
|
|
|
List<EntityFolder> folders = db.folder().getFolders(account.id, false, false);
|
|
|
|
for (EntityFolder folder : folders)
|
2019-05-01 07:23:31 +00:00
|
|
|
if (folder.tbc != null) {
|
2021-03-17 06:51:31 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " creating");
|
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (!ifolder.exists()) {
|
|
|
|
ifolder.create(Folder.HOLDS_MESSAGES);
|
|
|
|
ifolder.setSubscribed(true);
|
|
|
|
}
|
|
|
|
local.put(folder.name, folder);
|
|
|
|
} finally {
|
|
|
|
db.folder().resetFolderTbc(folder.id);
|
|
|
|
sync_folders = true;
|
2019-09-09 06:35:29 +00:00
|
|
|
}
|
2019-07-29 19:53:32 +00:00
|
|
|
|
|
|
|
} else if (folder.rename != null) {
|
2021-03-17 06:51:31 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " rename into " + folder.rename);
|
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (ifolder.exists()) {
|
|
|
|
// https://tools.ietf.org/html/rfc3501#section-6.3.9
|
|
|
|
boolean subscribed = ifolder.isSubscribed();
|
|
|
|
if (subscribed)
|
|
|
|
ifolder.setSubscribed(false);
|
|
|
|
|
|
|
|
Folder itarget = istore.getFolder(folder.rename);
|
|
|
|
ifolder.renameTo(itarget);
|
|
|
|
|
|
|
|
if (subscribed && folder.selectable)
|
|
|
|
try {
|
|
|
|
itarget.open(READ_WRITE);
|
|
|
|
itarget.setSubscribed(subscribed);
|
|
|
|
itarget.close();
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-10-04 13:59:02 +00:00
|
|
|
|
2021-03-17 06:51:31 +00:00
|
|
|
db.folder().renameFolder(folder.account, folder.name, folder.rename);
|
|
|
|
folder.name = folder.rename;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.folder().resetFolderRename(folder.id);
|
|
|
|
sync_folders = true;
|
2019-07-29 19:53:32 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 07:23:31 +00:00
|
|
|
} else if (folder.tbd != null && folder.tbd) {
|
2021-03-17 06:51:31 +00:00
|
|
|
try {
|
|
|
|
Log.i(folder.name + " deleting");
|
|
|
|
Folder ifolder = istore.getFolder(folder.name);
|
|
|
|
if (ifolder.exists()) {
|
|
|
|
ifolder.setSubscribed(false);
|
|
|
|
ifolder.delete(false);
|
|
|
|
}
|
|
|
|
db.folder().deleteFolder(folder.id);
|
2021-03-17 12:08:54 +00:00
|
|
|
} finally {
|
|
|
|
db.folder().resetFolderTbd(folder.id);
|
2021-03-17 06:51:31 +00:00
|
|
|
sync_folders = true;
|
2019-09-09 06:35:29 +00:00
|
|
|
}
|
2019-07-29 19:53:32 +00:00
|
|
|
|
2019-06-08 14:33:25 +00:00
|
|
|
} else {
|
2020-12-17 14:05:44 +00:00
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
drafts = true;
|
2020-12-18 07:02:07 +00:00
|
|
|
|
|
|
|
if (folder.local) {
|
|
|
|
if (!EntityFolder.DRAFTS.equals(folder.type)) {
|
|
|
|
List<Long> ids = db.message().getMessageByFolder(folder.id);
|
|
|
|
if (ids == null || ids.size() == 0)
|
|
|
|
db.folder().deleteFolder(folder.id);
|
|
|
|
}
|
|
|
|
} else {
|
2020-12-17 14:05:44 +00:00
|
|
|
local.put(folder.name, folder);
|
2021-02-25 11:31:34 +00:00
|
|
|
if (folder.selectable && folder.synchronize && folder.initialize != 0)
|
2020-12-17 14:05:44 +00:00
|
|
|
sync_folders = true;
|
|
|
|
}
|
2019-06-08 14:33:25 +00:00
|
|
|
}
|
2020-12-17 14:05:44 +00:00
|
|
|
Log.i("Local folder count=" + local.size() + " drafts=" + drafts);
|
|
|
|
|
|
|
|
if (!drafts) {
|
|
|
|
EntityFolder d = new EntityFolder();
|
|
|
|
d.account = account.id;
|
|
|
|
d.name = context.getString(R.string.title_folder_local_drafts);
|
|
|
|
d.type = EntityFolder.DRAFTS;
|
|
|
|
d.local = true;
|
|
|
|
d.setProperties();
|
|
|
|
d.synchronize = false;
|
|
|
|
d.download = false;
|
|
|
|
d.sync_days = Integer.MAX_VALUE;
|
|
|
|
d.keep_days = Integer.MAX_VALUE;
|
|
|
|
db.folder().insertFolder(d);
|
|
|
|
}
|
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();
|
2020-06-09 14:46:37 +00:00
|
|
|
List<Folder> ifolders = new ArrayList<>();
|
|
|
|
ifolders.addAll(Arrays.asList(defaultFolder.list("*")));
|
|
|
|
|
2020-06-10 13:22:29 +00:00
|
|
|
List<String> subscription = new ArrayList<>();
|
|
|
|
try {
|
|
|
|
Folder[] isubscribed = defaultFolder.listSubscribed("*");
|
2020-06-10 18:12:23 +00:00
|
|
|
for (Folder ifolder : isubscribed) {
|
2020-06-21 07:52:26 +00:00
|
|
|
String fullName = ifolder.getFullName();
|
2020-06-21 11:30:00 +00:00
|
|
|
if (TextUtils.isEmpty(fullName)) {
|
2020-07-08 12:58:36 +00:00
|
|
|
Log.w("Subscribed folder name empty namespace=" + defaultFolder.getFullName());
|
2020-06-21 07:52:26 +00:00
|
|
|
continue;
|
2020-06-21 11:30:00 +00:00
|
|
|
}
|
2020-06-21 07:52:26 +00:00
|
|
|
subscription.add(fullName);
|
|
|
|
Log.i("Subscribed " + defaultFolder.getFullName() + ":" + fullName);
|
2020-06-10 18:12:23 +00:00
|
|
|
}
|
2020-06-21 11:14:33 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
/*
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: java.lang.NullPointerException: Folder name is null
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.IMAPFolder.<init>(SourceFile:372)
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.IMAPFolder.<init>(SourceFile:411)
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.IMAPStore.newIMAPFolder(SourceFile:1809)
|
|
|
|
06-21 10:02:38.035 9927 10024 E fairemail: at com.sun.mail.imap.DefaultFolder.listSubscribed(SourceFile:89)
|
|
|
|
*/
|
2020-06-10 13:22:29 +00:00
|
|
|
Log.e(account.name, ex);
|
|
|
|
}
|
|
|
|
|
2020-06-09 14:46:37 +00:00
|
|
|
if (sync_shared_folders) {
|
2020-06-11 19:11:11 +00:00
|
|
|
// https://tools.ietf.org/html/rfc2342
|
2020-06-09 14:46:37 +00:00
|
|
|
Folder[] namespaces = istore.getSharedNamespaces();
|
2021-02-05 17:31:38 +00:00
|
|
|
EntityLog.log(context, "Namespaces=" + namespaces.length);
|
2020-06-09 14:46:37 +00:00
|
|
|
for (Folder namespace : namespaces) {
|
2021-02-05 17:31:38 +00:00
|
|
|
EntityLog.log(context, "Namespace=" + namespace.getFullName());
|
2020-06-10 13:22:29 +00:00
|
|
|
if (namespace.getSeparator() == separator) {
|
2020-06-09 16:43:17 +00:00
|
|
|
try {
|
|
|
|
ifolders.addAll(Arrays.asList(namespace.list("*")));
|
|
|
|
} catch (FolderNotFoundException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-06-10 13:22:29 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
Folder[] isubscribed = namespace.listSubscribed("*");
|
2020-06-10 18:12:23 +00:00
|
|
|
for (Folder ifolder : isubscribed) {
|
2020-06-21 07:52:26 +00:00
|
|
|
String fullName = ifolder.getFullName();
|
2020-06-21 11:30:00 +00:00
|
|
|
if (TextUtils.isEmpty(fullName)) {
|
|
|
|
Log.e("Subscribed folder name empty namespace=" + namespace.getFullName());
|
2020-06-21 07:52:26 +00:00
|
|
|
continue;
|
2020-06-21 11:30:00 +00:00
|
|
|
}
|
2020-06-21 07:52:26 +00:00
|
|
|
subscription.add(fullName);
|
|
|
|
Log.i("Subscribed " + namespace.getFullName() + ":" + fullName);
|
2020-06-10 18:12:23 +00:00
|
|
|
}
|
2020-06-21 11:14:33 +00:00
|
|
|
} catch (Throwable ex) {
|
2020-06-10 13:22:29 +00:00
|
|
|
Log.e(account.name, ex);
|
|
|
|
}
|
|
|
|
} else
|
2020-06-09 14:46:37 +00:00
|
|
|
Log.e("Namespace separator=" + namespace.getSeparator() + " default=" + separator);
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 12:07:10 +00:00
|
|
|
|
|
|
|
long duration = new Date().getTime() - start;
|
2019-05-25 14:21:25 +00:00
|
|
|
|
2020-06-09 14:46:37 +00:00
|
|
|
Log.i("Remote folder count=" + ifolders.size() +
|
2020-06-10 18:12:23 +00:00
|
|
|
" subscriptions=" + subscription.size() +
|
2019-05-26 12:07:10 +00:00
|
|
|
" 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();
|
2020-06-21 07:52:26 +00:00
|
|
|
if (TextUtils.isEmpty(fullName)) {
|
|
|
|
Log.e("Folder name empty");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-24 08:32:03 +00:00
|
|
|
String[] attrs = ((IMAPFolder) ifolder).getAttributes();
|
|
|
|
String type = EntityFolder.getType(attrs, fullName, false);
|
2020-03-31 16:58:29 +00:00
|
|
|
boolean subscribed = subscription.contains(fullName);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-02-24 08:32:03 +00:00
|
|
|
boolean selectable = true;
|
|
|
|
boolean inferiors = true;
|
|
|
|
for (String attr : attrs) {
|
|
|
|
if (attr.equalsIgnoreCase("\\NoSelect"))
|
|
|
|
selectable = false;
|
|
|
|
if (attr.equalsIgnoreCase("\\NoInferiors"))
|
|
|
|
inferiors = false;
|
|
|
|
}
|
|
|
|
selectable = selectable && ((ifolder.getType() & IMAPFolder.HOLDS_MESSAGES) != 0);
|
|
|
|
inferiors = inferiors && ((ifolder.getType() & IMAPFolder.HOLDS_FOLDERS) != 0);
|
|
|
|
|
2021-03-05 17:13:16 +00:00
|
|
|
if (EntityFolder.INBOX.equals(type))
|
|
|
|
selectable = true;
|
|
|
|
|
2020-02-24 08:32:03 +00:00
|
|
|
Log.i(account.name + ":" + fullName + " type=" + type +
|
|
|
|
" subscribed=" + subscribed +
|
|
|
|
" selectable=" + selectable +
|
|
|
|
" inferiors=" + inferiors +
|
|
|
|
" attrs=" + TextUtils.join(" ", attrs));
|
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
|
|
|
|
2020-03-31 16:57:06 +00:00
|
|
|
EntityFolder folder;
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-05-22 12:59:21 +00:00
|
|
|
|
2020-03-31 16:57:06 +00:00
|
|
|
folder = db.folder().getFolderByName(account.id, fullName);
|
|
|
|
if (folder == null) {
|
2021-02-20 07:17:32 +00:00
|
|
|
EntityFolder parent = null;
|
2021-02-22 14:51:02 +00:00
|
|
|
int sep = fullName.lastIndexOf(separator);
|
2021-02-20 13:29:24 +00:00
|
|
|
if (sep > 0)
|
2021-02-20 07:17:32 +00:00
|
|
|
parent = db.folder().getFolderByName(account.id, fullName.substring(0, sep));
|
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
folder = new EntityFolder();
|
|
|
|
folder.account = account.id;
|
|
|
|
folder.name = fullName;
|
|
|
|
folder.type = (EntityFolder.SYSTEM.equals(type) ? type : EntityFolder.USER);
|
|
|
|
folder.subscribed = subscribed;
|
2019-07-07 06:17:27 +00:00
|
|
|
folder.selectable = selectable;
|
2020-02-24 08:32:03 +00:00
|
|
|
folder.inferiors = inferiors;
|
2021-02-20 07:17:32 +00:00
|
|
|
folder.setProperties();
|
2020-12-01 20:07:13 +00:00
|
|
|
folder.setSpecials(account);
|
2021-02-20 07:17:32 +00:00
|
|
|
|
2021-02-25 11:31:34 +00:00
|
|
|
if (selectable && parent != null && EntityFolder.USER.equals(parent.type)) {
|
2021-02-20 07:17:32 +00:00
|
|
|
folder.synchronize = parent.synchronize;
|
|
|
|
folder.poll = parent.poll;
|
|
|
|
folder.poll_factor = parent.poll_factor;
|
|
|
|
folder.download = parent.download;
|
|
|
|
folder.auto_classify_source = parent.auto_classify_source;
|
|
|
|
folder.auto_classify_target = parent.auto_classify_target;
|
|
|
|
folder.unified = parent.unified;
|
|
|
|
folder.navigation = parent.navigation;
|
|
|
|
folder.notify = parent.notify;
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:13:03 +00:00
|
|
|
folder.id = db.folder().insertFolder(folder);
|
2021-02-19 18:07:42 +00:00
|
|
|
Log.i(folder.name + " added type=" + folder.type + " sync=" + folder.synchronize);
|
|
|
|
if (folder.synchronize)
|
|
|
|
EntityOperation.sync(context, folder.id, false);
|
2020-03-31 16:57:06 +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-09-18 06:56:33 +00:00
|
|
|
if (folder.selectable != selectable)
|
|
|
|
db.folder().setFolderSelectable(folder.id, selectable);
|
|
|
|
|
2020-02-24 08:32:03 +00:00
|
|
|
if (folder.inferiors != inferiors)
|
|
|
|
db.folder().setFolderInferiors(folder.id, inferiors);
|
|
|
|
|
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
|
|
|
}
|
2020-08-17 06:48:14 +00:00
|
|
|
|
2020-03-31 16:57:06 +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-10-03 08:03:01 +00:00
|
|
|
Log.i("Creating folders parents=" + parentFolders.size());
|
2019-05-22 12:59:21 +00:00
|
|
|
for (String parentName : parentFolders.keySet()) {
|
|
|
|
EntityFolder parent = nameFolder.get(parentName);
|
2019-10-02 20:50:25 +00:00
|
|
|
if (parent == null && parentName != null) {
|
2019-10-03 08:03:01 +00:00
|
|
|
parent = db.folder().getFolderByName(account.id, parentName);
|
|
|
|
if (parent == null) {
|
2020-03-26 07:39:49 +00:00
|
|
|
Log.i("Creating parent name=" + parentName);
|
2019-10-03 08:03:01 +00:00
|
|
|
parent = new EntityFolder();
|
|
|
|
parent.account = account.id;
|
|
|
|
parent.name = parentName;
|
|
|
|
parent.type = EntityFolder.SYSTEM;
|
|
|
|
parent.subscribed = false;
|
|
|
|
parent.selectable = false;
|
2020-03-26 07:39:49 +00:00
|
|
|
parent.inferiors = false;
|
|
|
|
parent.setProperties();
|
|
|
|
parent.display = parentName + "*";
|
2019-10-03 08:03:01 +00:00
|
|
|
parent.id = db.folder().insertFolder(parent);
|
|
|
|
}
|
2019-10-02 20:50:25 +00:00
|
|
|
nameFolder.put(parentName, parent);
|
|
|
|
}
|
2019-10-03 08:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("Updating folders parents=" + parentFolders.size());
|
|
|
|
for (String parentName : parentFolders.keySet()) {
|
|
|
|
EntityFolder parent = nameFolder.get(parentName);
|
2019-05-22 12:59:21 +00:00
|
|
|
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);
|
2020-10-20 18:27:35 +00:00
|
|
|
List<EntityFolder> childs = parentFolders.get(name);
|
|
|
|
if (EntityFolder.USER.equals(folder.type) ||
|
|
|
|
childs == null || childs.size() == 0) {
|
2019-06-25 14:34:30 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-26 17:58:13 +00:00
|
|
|
private static void onPurgeFolder(Context context, JSONArray jargs, EntityFolder folder, IMAPFolder ifolder) throws MessagingException {
|
|
|
|
// Delete all messages from folder
|
|
|
|
try {
|
2021-05-14 17:03:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
List<Long> busy = db.message().getBusyUids(folder.id, new Date().getTime());
|
|
|
|
|
|
|
|
Message[] imessages = ifolder.getMessages();
|
|
|
|
Log.i(folder.name + " purge=" + imessages.length + " busy=" + busy.size());
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
List<Message> idelete = new ArrayList<>();
|
|
|
|
for (Message imessage : imessages) {
|
|
|
|
long uid = ifolder.getUID(imessage);
|
|
|
|
if (!busy.contains(uid))
|
|
|
|
idelete.add(imessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityLog.log(context, folder.name + " purging=" + idelete.size() + "/" + imessages.length);
|
|
|
|
ifolder.setFlags(idelete.toArray(new Message[0]), new Flags(Flags.Flag.DELETED), true);
|
2020-07-26 17:58:13 +00:00
|
|
|
Log.i(folder.name + " purge deleted");
|
|
|
|
|
2021-02-09 15:49:51 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
if (perform_expunge)
|
|
|
|
ifolder.expunge();
|
2020-07-26 17:58:13 +00:00
|
|
|
Log.i(folder.name + " purge expunged");
|
2020-07-26 19:35:29 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
throw ex;
|
2020-07-26 17:58:13 +00:00
|
|
|
} finally {
|
2020-10-22 09:09:10 +00:00
|
|
|
EntityOperation.sync(context, folder.id, false);
|
2020-07-26 17:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:51:34 +00:00
|
|
|
private static void onExpungeFolder(Context context, JSONArray jargs, EntityFolder folder, IMAPFolder ifolder) throws MessagingException {
|
|
|
|
Log.i(folder.name + " expunge");
|
|
|
|
ifolder.expunge();
|
|
|
|
}
|
|
|
|
|
2020-07-27 14:54:03 +00:00
|
|
|
private static void onPurgeFolder(Context context, EntityFolder folder) {
|
2020-10-22 09:09:10 +00:00
|
|
|
// POP3
|
2020-07-26 17:58:13 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
|
|
|
int purged = db.message().deleteHiddenMessages(folder.id);
|
|
|
|
Log.i(folder.name + " purge count=" + purged);
|
|
|
|
|
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 09:53:48 +00:00
|
|
|
private static void onRule(Context context, JSONArray jargs, EntityMessage message) throws JSONException, IOException, AddressException {
|
2020-07-13 14:34:55 +00:00
|
|
|
// Download message body
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
|
|
|
long id = jargs.getLong(0);
|
|
|
|
EntityRule rule = db.rule().getRule(id);
|
|
|
|
if (rule == null)
|
|
|
|
throw new IllegalArgumentException("Rule not found id=" + id);
|
|
|
|
|
|
|
|
if (!message.content)
|
|
|
|
throw new IllegalArgumentException("Message without content id=" + rule.id + ":" + rule.name);
|
|
|
|
|
|
|
|
rule.execute(context, message);
|
|
|
|
}
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
private static void onSynchronizeMessages(
|
|
|
|
Context context, JSONArray jargs,
|
|
|
|
EntityAccount account, final EntityFolder folder,
|
2021-07-27 19:04:48 +00:00
|
|
|
POP3Folder ifolder, POP3Store istore, State state) throws MessagingException, IOException {
|
2019-09-19 15:41:26 +00:00
|
|
|
DB db = DB.getInstance(context);
|
2020-06-07 07:59:25 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean notify_known = prefs.getBoolean("notify_known", false);
|
2020-06-07 14:32:14 +00:00
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-09-19 19:42:09 +00:00
|
|
|
|
2021-07-29 06:43:33 +00:00
|
|
|
boolean force = jargs.optBoolean(5, false);
|
|
|
|
|
|
|
|
EntityLog.log(context, account.name + " POP sync type=" + folder.type +
|
|
|
|
" force=" + force +
|
|
|
|
" connected=" + (ifolder != null));
|
2019-09-20 07:45:36 +00:00
|
|
|
|
2019-09-19 19:42:09 +00:00
|
|
|
if (!EntityFolder.INBOX.equals(folder.type)) {
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-01 06:57:17 +00:00
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id);
|
|
|
|
|
2019-09-19 15:41:26 +00:00
|
|
|
try {
|
|
|
|
db.folder().setFolderSyncState(folder.id, "syncing");
|
|
|
|
|
2021-06-25 14:53:50 +00:00
|
|
|
// Get capabilities
|
2019-09-24 17:33:20 +00:00
|
|
|
Map<String, String> caps = istore.capabilities();
|
2021-06-25 13:55:16 +00:00
|
|
|
boolean hasUidl = caps.containsKey("UIDL");
|
2021-07-18 08:06:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP capabilities= " + caps.keySet());
|
2019-09-24 17:33:20 +00:00
|
|
|
|
2021-06-25 14:53:50 +00:00
|
|
|
// Get messages
|
2019-09-19 15:41:26 +00:00
|
|
|
Message[] imessages = ifolder.getMessages();
|
2021-06-25 13:55:16 +00:00
|
|
|
List<TupleUidl> ids = db.message().getUidls(folder.id);
|
2021-06-25 18:02:13 +00:00
|
|
|
int max = (account.max_messages == null
|
|
|
|
? imessages.length
|
|
|
|
: Math.min(imessages.length, account.max_messages));
|
2021-06-25 13:55:16 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
boolean sync = true;
|
2021-07-29 06:43:33 +00:00
|
|
|
if (!force &&
|
|
|
|
imessages.length > 0 && folder.last_sync_count != null &&
|
2021-07-23 20:31:23 +00:00
|
|
|
imessages.length == folder.last_sync_count) {
|
|
|
|
// Check if last message known as new messages indicator
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessages[imessages.length - 1], context);
|
|
|
|
String msgid = helper.getMessageID();
|
|
|
|
if (msgid != null) {
|
|
|
|
int count = db.message().countMessageByMsgId(folder.id, msgid);
|
|
|
|
if (count == 1) {
|
|
|
|
Log.i(account.name + " POP having last msgid=" + msgid);
|
|
|
|
sync = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 08:06:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP" +
|
2021-06-25 13:55:16 +00:00
|
|
|
" device=" + ids.size() +
|
|
|
|
" server=" + imessages.length +
|
|
|
|
" max=" + max + "/" + account.max_messages +
|
2021-07-23 20:31:23 +00:00
|
|
|
" last=" + folder.last_sync_count +
|
|
|
|
" sync=" + sync +
|
2021-06-25 13:55:16 +00:00
|
|
|
" uidl=" + hasUidl);
|
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
folder.last_sync_count = imessages.length;
|
|
|
|
db.folder().setFolderLastSyncCount(folder.id, folder.last_sync_count);
|
2021-06-25 14:53:50 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
if (sync) {
|
|
|
|
// Index IDs
|
|
|
|
Map<String, String> uidlMsgId = new HashMap<>();
|
|
|
|
for (TupleUidl id : ids) {
|
|
|
|
if (id.uidl != null && id.msgid != null)
|
|
|
|
uidlMsgId.put(id.uidl, id.msgid);
|
|
|
|
}
|
2019-09-24 17:33:20 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
// Fetch UIDLs
|
2020-08-10 13:45:55 +00:00
|
|
|
if (hasUidl) {
|
2021-07-23 20:31:23 +00:00
|
|
|
FetchProfile ifetch = new FetchProfile();
|
|
|
|
ifetch.add(UIDFolder.FetchProfileItem.UID); // This will fetch all UIDs
|
|
|
|
ifolder.fetch(imessages, ifetch);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
if (!account.leave_on_device) {
|
|
|
|
if (hasUidl) {
|
|
|
|
Map<String, TupleUidl> known = new HashMap<>();
|
|
|
|
for (TupleUidl id : ids)
|
|
|
|
if (id.uidl != null)
|
|
|
|
known.put(id.uidl, id);
|
|
|
|
|
|
|
|
for (Message imessage : imessages) {
|
|
|
|
String uidl = ifolder.getUID(imessage);
|
|
|
|
if (TextUtils.isEmpty(uidl))
|
|
|
|
known.clear(); // better safe than sorry
|
|
|
|
else
|
|
|
|
known.remove(uidl);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
for (TupleUidl uidl : known.values()) {
|
|
|
|
EntityLog.log(context, account.name + " POP purging uidl=" + uidl.uidl);
|
|
|
|
db.message().deleteMessage(uidl.id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Map<String, TupleUidl> known = new HashMap<>();
|
|
|
|
for (TupleUidl id : ids)
|
|
|
|
if (id.msgid != null)
|
|
|
|
known.put(id.msgid, id);
|
|
|
|
|
|
|
|
for (int i = imessages.length - max; i < imessages.length; i++) {
|
|
|
|
Message imessage = imessages[i];
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
|
|
|
String msgid = helper.getMessageID(); // expensive!
|
|
|
|
if (!TextUtils.isEmpty(msgid))
|
|
|
|
known.remove(msgid);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
for (TupleUidl uidl : known.values()) {
|
|
|
|
EntityLog.log(context, account.name + " POP purging msgid=" + uidl.msgid);
|
|
|
|
db.message().deleteMessage(uidl.id);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-20 07:45:36 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
boolean _new = true;
|
|
|
|
for (int i = imessages.length - 1; i >= imessages.length - max; i--) {
|
2021-07-27 16:32:10 +00:00
|
|
|
state.ensureRunning("Sync/POP3");
|
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
Message imessage = imessages[i];
|
|
|
|
try {
|
|
|
|
MessageHelper helper = new MessageHelper((MimeMessage) imessage, context);
|
2019-09-24 17:33:20 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
String uidl;
|
|
|
|
String msgid;
|
|
|
|
if (hasUidl) {
|
|
|
|
uidl = ifolder.getUID(imessage);
|
|
|
|
if (TextUtils.isEmpty(uidl)) {
|
|
|
|
EntityLog.log(context, account.name + " POP no uidl");
|
|
|
|
continue;
|
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
msgid = uidlMsgId.get(uidl);
|
|
|
|
if (msgid == null) {
|
|
|
|
msgid = helper.getMessageID();
|
|
|
|
if (TextUtils.isEmpty(msgid))
|
|
|
|
msgid = uidl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uidl = null;
|
2020-08-10 13:45:55 +00:00
|
|
|
msgid = helper.getMessageID();
|
2021-07-23 20:31:23 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(msgid)) {
|
|
|
|
Long time = helper.getReceived();
|
|
|
|
if (time == null)
|
|
|
|
time = helper.getSent();
|
|
|
|
if (time != null)
|
|
|
|
msgid = Long.toString(time);
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
|
2020-08-31 05:55:54 +00:00
|
|
|
if (TextUtils.isEmpty(msgid)) {
|
2021-07-23 20:31:23 +00:00
|
|
|
EntityLog.log(context, account.name + " POP no msgid uidl=" + uidl);
|
|
|
|
continue;
|
2020-08-31 05:55:54 +00:00
|
|
|
}
|
2020-05-13 12:33:47 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
if (db.message().countMessageByMsgId(folder.id, msgid) > 0) {
|
|
|
|
_new = false;
|
|
|
|
Log.i(account.name + " POP having " + msgid + "/" + uidl);
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-10 13:45:55 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
try {
|
|
|
|
Long sent = helper.getSent();
|
|
|
|
Long received = helper.getReceivedHeader();
|
|
|
|
if (received == null)
|
|
|
|
received = sent;
|
|
|
|
if (received == null)
|
|
|
|
received = 0L;
|
|
|
|
|
|
|
|
boolean seen = (received <= account.created);
|
|
|
|
EntityLog.log(context, account.name + " POP sync=" + uidl + "/" + msgid +
|
|
|
|
" new=" + _new + " seen=" + seen);
|
|
|
|
|
|
|
|
String[] authentication = helper.getAuthentication();
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
|
|
|
|
|
|
|
EntityMessage message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.uid = null;
|
|
|
|
message.uidl = uidl;
|
|
|
|
message.msgid = msgid;
|
|
|
|
message.hash = helper.getHash();
|
|
|
|
message.references = TextUtils.join(" ", helper.getReferences());
|
|
|
|
message.inreplyto = helper.getInReplyTo();
|
|
|
|
message.deliveredto = helper.getDeliveredTo();
|
|
|
|
message.thread = helper.getThreadId(context, account.id, 0);
|
|
|
|
message.priority = helper.getPriority();
|
|
|
|
message.auto_submitted = helper.getAutoSubmitted();
|
|
|
|
message.receipt_request = helper.getReceiptRequested();
|
|
|
|
message.receipt_to = helper.getReceiptTo();
|
|
|
|
message.bimi_selector = helper.getBimiSelector();
|
|
|
|
message.dkim = MessageHelper.getAuthentication("dkim", authentication);
|
|
|
|
message.spf = MessageHelper.getAuthentication("spf", authentication);
|
|
|
|
message.dmarc = MessageHelper.getAuthentication("dmarc", authentication);
|
|
|
|
message.return_path = helper.getReturnPath();
|
|
|
|
message.submitter = helper.getSender();
|
|
|
|
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.unsubscribe = helper.getListUnsubscribe();
|
|
|
|
message.headers = helper.getHeaders();
|
|
|
|
message.subject = helper.getSubject();
|
|
|
|
message.size = parts.getBodySize();
|
|
|
|
message.total = helper.getSize();
|
|
|
|
message.content = false;
|
|
|
|
message.encrypt = parts.getEncryption();
|
|
|
|
message.ui_encrypt = message.encrypt;
|
|
|
|
message.received = received;
|
|
|
|
message.sent = sent;
|
|
|
|
message.seen = seen;
|
|
|
|
message.answered = false;
|
|
|
|
message.flagged = false;
|
|
|
|
message.flags = null;
|
|
|
|
message.keywords = new String[0];
|
|
|
|
message.ui_seen = seen;
|
|
|
|
message.ui_answered = false;
|
|
|
|
message.ui_flagged = false;
|
|
|
|
message.ui_hide = false;
|
|
|
|
message.ui_found = false;
|
|
|
|
message.ui_ignored = !_new;
|
|
|
|
message.ui_browsed = false;
|
|
|
|
|
|
|
|
if (message.deliveredto != null)
|
|
|
|
try {
|
|
|
|
Address deliveredto = new InternetAddress(message.deliveredto);
|
|
|
|
if (MessageHelper.equalEmail(new Address[]{deliveredto}, message.to))
|
|
|
|
message.deliveredto = null;
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-07-15 07:40:12 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
if (MessageHelper.equalEmail(message.submitter, message.from))
|
|
|
|
message.submitter = null;
|
2021-04-25 10:52:19 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
if (message.size == null && message.total != null)
|
|
|
|
message.size = message.total;
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
message.identity = (identity == null ? null : identity.id);
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
message.sender = MessageHelper.getSortKey(message.from);
|
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(message.from);
|
|
|
|
message.avatar = (lookupUri == null ? null : lookupUri.toString());
|
|
|
|
if (message.avatar == null && notify_known && pro)
|
|
|
|
message.ui_ignored = true;
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
// No MX check
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2020-05-06 18:32:03 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
|
|
|
EntityLog.log(context, account.name + " POP added id=" + message.id +
|
|
|
|
" uidl/msgid=" + message.uidl + "/" + message.msgid);
|
|
|
|
|
|
|
|
int sequence = 1;
|
|
|
|
for (EntityAttachment attachment : parts.getAttachments()) {
|
|
|
|
Log.i(account.name + " POP attachment seq=" + sequence +
|
|
|
|
" name=" + attachment.name + " type=" + attachment.type +
|
|
|
|
" cid=" + attachment.cid + " pgp=" + attachment.encryption +
|
|
|
|
" size=" + attachment.size);
|
|
|
|
attachment.message = message.id;
|
|
|
|
attachment.sequence = sequence++;
|
|
|
|
attachment.id = db.attachment().insertAttachment(attachment);
|
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
runRules(context, imessage, account, folder, message, rules);
|
|
|
|
reportNewMessage(context, account, folder, message);
|
2019-09-19 19:47:19 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2020-06-22 08:22:25 +00:00
|
|
|
|
2021-07-23 20:31:23 +00:00
|
|
|
String body = parts.getHtml(context);
|
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
|
|
|
String text = HtmlHelper.getFullText(body);
|
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
message.language,
|
|
|
|
parts.isPlainOnly(),
|
|
|
|
message.preview,
|
|
|
|
parts.getWarnings(message.warning));
|
|
|
|
|
|
|
|
for (EntityAttachment attachment : parts.getAttachments())
|
|
|
|
if (attachment.subsequence == null)
|
|
|
|
parts.downloadAttachment(context, attachment);
|
|
|
|
|
|
|
|
|
|
|
|
ContactInfo.update(context, account, folder, message);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
2020-05-06 18:32:03 +00:00
|
|
|
}
|
2021-07-23 20:31:23 +00:00
|
|
|
} finally {
|
|
|
|
((POP3Message) imessage).invalidate(true);
|
2020-05-06 18:32:03 +00:00
|
|
|
}
|
2019-09-19 15:41:26 +00:00
|
|
|
}
|
2021-06-22 11:18:49 +00:00
|
|
|
}
|
2019-09-20 07:45:36 +00:00
|
|
|
|
2020-10-26 12:21:43 +00:00
|
|
|
db.folder().setFolderLastSync(folder.id, new Date().getTime());
|
2021-07-18 08:06:11 +00:00
|
|
|
EntityLog.log(context, account.name + " POP done");
|
2019-09-19 15:41:26 +00:00
|
|
|
} finally {
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 09:02:42 +00:00
|
|
|
private static void onSynchronizeMessages(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context, JSONArray jargs,
|
2019-02-27 19:04:26 +00:00
|
|
|
EntityAccount account, final EntityFolder folder,
|
2021-07-27 19:04:48 +00:00
|
|
|
IMAPStore istore, final IMAPFolder ifolder, State state) throws JSONException, MessagingException, IOException {
|
2019-02-27 15:05:15 +00:00
|
|
|
final DB db = DB.getInstance(context);
|
|
|
|
try {
|
2020-07-15 16:58:03 +00:00
|
|
|
SyncStats stats = new SyncStats();
|
|
|
|
|
2019-03-25 15:02:27 +00:00
|
|
|
// Legacy
|
|
|
|
if (jargs.length() == 0)
|
2020-12-02 15:58:32 +00:00
|
|
|
jargs = folder.getSyncArgs(false);
|
2019-03-25 15:02:27 +00:00
|
|
|
|
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);
|
2020-12-02 15:58:32 +00:00
|
|
|
boolean force = jargs.optBoolean(5, false);
|
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);
|
2020-03-31 19:07:40 +00:00
|
|
|
boolean sync_nodate = prefs.getBoolean("sync_nodate", false);
|
2019-05-06 13:25:11 +00:00
|
|
|
boolean sync_unseen = prefs.getBoolean("sync_unseen", false);
|
2019-07-24 14:33:00 +00:00
|
|
|
boolean sync_flagged = prefs.getBoolean("sync_flagged", false);
|
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);
|
2021-04-08 05:57:28 +00:00
|
|
|
boolean use_modseq = prefs.getBoolean("use_modseq", true);
|
2021-02-09 15:49:51 +00:00
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
2019-05-06 13:25:11 +00:00
|
|
|
|
|
|
|
Log.i(folder.name + " start sync after=" + sync_days + "/" + keep_days +
|
2020-12-02 15:58:32 +00:00
|
|
|
" force=" + force +
|
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");
|
|
|
|
|
2019-07-24 20:01:27 +00:00
|
|
|
// Check uid validity
|
|
|
|
try {
|
|
|
|
long uidv = ifolder.getUIDValidity();
|
2019-07-24 20:04:58 +00:00
|
|
|
if (folder.uidv != null && !folder.uidv.equals(uidv)) {
|
2019-07-24 20:01:27 +00:00
|
|
|
Log.w(folder.name + " uid validity changed from " + folder.uidv + " to " + uidv);
|
|
|
|
db.message().deleteLocalMessages(folder.id);
|
|
|
|
}
|
|
|
|
folder.uidv = uidv;
|
|
|
|
db.folder().setFolderUidValidity(folder.id, uidv);
|
|
|
|
} catch (MessagingException ex) {
|
2019-09-17 19:25:41 +00:00
|
|
|
Log.w(folder.name, ex);
|
2019-07-24 20:01:27 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
// https://tools.ietf.org/html/rfc4551
|
|
|
|
// https://wiki.mozilla.org/Thunderbird:IMAP_RFC_4551_Implementation
|
2021-04-08 05:57:28 +00:00
|
|
|
Long modseq = null;
|
2021-04-07 06:42:47 +00:00
|
|
|
boolean modified = true;
|
2021-04-08 05:57:28 +00:00
|
|
|
if (use_modseq)
|
|
|
|
try {
|
|
|
|
if (MessageHelper.hasCapability(ifolder, "CONDSTORE")) {
|
|
|
|
modseq = ifolder.getHighestModSeq();
|
|
|
|
modified = (force || initialize != 0 ||
|
|
|
|
folder.modseq == null || !folder.modseq.equals(modseq));
|
2021-04-12 10:19:50 +00:00
|
|
|
EntityLog.log(context, folder.name + " modseq=" + modseq + "/" + folder.modseq + " modified=" + modified);
|
2021-04-08 05:57:28 +00:00
|
|
|
}
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
// 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 {
|
2021-05-26 17:39:08 +00:00
|
|
|
int old = db.message().deleteMessagesBefore(folder.id, sync_time, 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
|
2020-12-02 15:58:32 +00:00
|
|
|
final List<Long> uids = db.message().getUids(folder.id, sync_kept || force ? null : sync_time);
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " local count=" + uids.size());
|
|
|
|
|
|
|
|
// Reduce list of local uids
|
2021-04-05 10:13:54 +00:00
|
|
|
SearchTerm dateTerm = account.use_date
|
2021-04-05 10:05:00 +00:00
|
|
|
? new SentDateTerm(ComparisonTerm.GE, new Date(sync_time))
|
|
|
|
: new ReceivedDateTerm(ComparisonTerm.GE, new Date(sync_time));
|
2021-04-05 10:13:54 +00:00
|
|
|
|
|
|
|
SearchTerm searchTerm = dateTerm;
|
|
|
|
Flags flags = ifolder.getPermanentFlags();
|
2020-07-21 18:48:08 +00:00
|
|
|
if (sync_nodate)
|
2020-03-31 19:07:40 +00:00
|
|
|
searchTerm = new OrTerm(searchTerm, new ReceivedDateTerm(ComparisonTerm.LT, new Date(365 * 24 * 3600 * 1000L)));
|
2019-09-23 11:52:54 +00:00
|
|
|
if (sync_unseen && flags.contains(Flags.Flag.SEEN))
|
2019-05-06 13:25:11 +00:00
|
|
|
searchTerm = new OrTerm(searchTerm, new FlagTerm(new Flags(Flags.Flag.SEEN), false));
|
2019-09-23 11:52:54 +00:00
|
|
|
if (sync_flagged && flags.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;
|
2021-04-05 10:13:54 +00:00
|
|
|
if (sync_time == 0)
|
|
|
|
imessages = ifolder.getMessages();
|
|
|
|
else
|
|
|
|
try {
|
|
|
|
imessages = ifolder.search(searchTerm);
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
// Fallback to date only search
|
|
|
|
// BAD Could not parse command
|
|
|
|
imessages = ifolder.search(dateTerm);
|
|
|
|
}
|
2019-12-01 11:42:02 +00:00
|
|
|
if (imessages == null)
|
|
|
|
imessages = new Message[0];
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
stats.search_ms = (SystemClock.elapsedRealtime() - search);
|
|
|
|
Log.i(folder.name + " remote count=" + imessages.length + " search=" + stats.search_ms + " ms");
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
Long[] ids = new Long[imessages.length];
|
|
|
|
if (!modified) {
|
|
|
|
Log.i(folder.name + " quick check");
|
|
|
|
long fetch = SystemClock.elapsedRealtime();
|
2019-03-30 10:57:29 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID);
|
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
stats.flags = imessages.length;
|
|
|
|
stats.flags_ms = (SystemClock.elapsedRealtime() - fetch);
|
|
|
|
Log.i(folder.name + " remote fetched=" + stats.flags_ms + " ms");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-10-05 14:04:18 +00:00
|
|
|
try {
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int i = 0; i < imessages.length; i++) {
|
|
|
|
state.ensureRunning("Sync/IMAP");
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
long uid = ifolder.getUID(imessages[i]);
|
2021-04-12 10:49:46 +00:00
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid);
|
|
|
|
ids[i] = (message == null ? null : message.id);
|
|
|
|
if (message == null || message.ui_hide) {
|
2021-04-07 06:42:47 +00:00
|
|
|
Log.i(folder.name + " missing uid=" + uid);
|
|
|
|
modified = true;
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
uids.remove(uid);
|
|
|
|
}
|
2020-10-05 14:04:18 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
2021-04-07 06:42:47 +00:00
|
|
|
modified = true;
|
|
|
|
db.folder().setFolderModSeq(folder.id, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uids.size() > 0) {
|
|
|
|
Log.i(folder.name + " remaining=" + uids.size());
|
|
|
|
modified = true;
|
2020-10-05 14:04:18 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
EntityLog.log(context, folder.name + " modified=" + modified);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (modified) {
|
|
|
|
long fetch = SystemClock.elapsedRealtime();
|
|
|
|
|
|
|
|
FetchProfile fp = new FetchProfile();
|
|
|
|
fp.add(UIDFolder.FetchProfileItem.UID); // To check if message exists
|
|
|
|
fp.add(FetchProfile.Item.FLAGS); // To update existing messages
|
|
|
|
if (account.isGmail())
|
|
|
|
fp.add(GmailFolder.FetchProfileItem.LABELS);
|
|
|
|
ifolder.fetch(imessages, fp);
|
|
|
|
|
|
|
|
stats.flags = imessages.length;
|
|
|
|
stats.flags_ms = (SystemClock.elapsedRealtime() - fetch);
|
|
|
|
Log.i(folder.name + " remote fetched=" + stats.flags_ms + " ms");
|
2019-10-21 07:54:20 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
// Sort for finding referenced/replied-to messages
|
|
|
|
// Sorting on date/time would be better, but requires fetching the headers
|
|
|
|
Arrays.sort(imessages, new Comparator<Message>() {
|
2019-02-27 15:05:15 +00:00
|
|
|
@Override
|
2021-04-07 06:42:47 +00:00
|
|
|
public int compare(Message m1, Message m2) {
|
2019-10-21 07:54:20 +00:00
|
|
|
try {
|
2021-04-07 06:42:47 +00:00
|
|
|
return Long.compare(ifolder.getUID(m1), ifolder.getUID(m2));
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
return 0;
|
2019-10-21 07:54:20 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
|
|
|
});
|
2019-10-21 07:54:20 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
int expunge = 0;
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int i = 0; i < imessages.length; i++) {
|
|
|
|
state.ensureRunning("Sync/IMAP");
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
try {
|
|
|
|
if (perform_expunge && imessages[i].isSet(Flags.Flag.DELETED))
|
|
|
|
expunge++;
|
|
|
|
else
|
|
|
|
uids.remove(ifolder.getUID(imessages[i]));
|
|
|
|
} catch (MessageRemovedException ex) {
|
|
|
|
Log.w(folder.name, ex);
|
2021-05-01 13:26:02 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
2021-04-07 06:42:47 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
2021-05-01 13:26:02 +00:00
|
|
|
EntityLog.log(context, folder.name + " expunge " + Log.formatThrowable(ex, false));
|
2021-04-07 06:42:47 +00:00
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
}
|
2021-07-27 16:32:10 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
|
|
|
|
if (expunge > 0)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " expunging=" + expunge);
|
|
|
|
ifolder.expunge();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uids.size() > 0) {
|
|
|
|
// This is done outside of JavaMail to prevent changed notifications
|
|
|
|
if (!ifolder.isOpen())
|
|
|
|
throw new FolderClosedException(ifolder, "UID FETCH");
|
|
|
|
|
|
|
|
long getuid = SystemClock.elapsedRealtime();
|
|
|
|
MessagingException ex = (MessagingException) ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
|
|
|
|
@Override
|
|
|
|
public Object doCommand(IMAPProtocol protocol) {
|
|
|
|
try {
|
|
|
|
protocol.select(folder.name);
|
|
|
|
} catch (ProtocolException ex) {
|
|
|
|
return new MessagingException("UID FETCH", ex);
|
2019-07-09 19:07:56 +00:00
|
|
|
}
|
2021-02-19 17:07:06 +00:00
|
|
|
|
2021-04-07 06:42:47 +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);
|
|
|
|
}
|
|
|
|
String command = "UID FETCH " + sb + " (UID FLAGS)";
|
|
|
|
Response[] responses = protocol.command(command, null);
|
|
|
|
|
|
|
|
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);
|
|
|
|
FLAGS flags = fr.getItem(FLAGS.class);
|
|
|
|
if (uid == null || flags == null)
|
|
|
|
continue;
|
|
|
|
if (perform_expunge && flags.contains(Flags.Flag.DELETED))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uids.remove(uid.uid);
|
|
|
|
|
|
|
|
if (force) {
|
|
|
|
EntityMessage message = db.message().getMessageByUid(folder.id, uid.uid);
|
|
|
|
if (message != null) {
|
|
|
|
boolean update = false;
|
|
|
|
boolean seen = flags.contains(Flags.Flag.SEEN);
|
|
|
|
boolean answered = flags.contains(Flags.Flag.ANSWERED);
|
|
|
|
boolean flagged = flags.contains(Flags.Flag.FLAGGED);
|
|
|
|
boolean deleted = flags.contains(Flags.Flag.DELETED);
|
|
|
|
if (message.seen != seen) {
|
|
|
|
update = true;
|
|
|
|
message.seen = seen;
|
|
|
|
message.ui_seen = seen;
|
|
|
|
Log.i("UID fetch seen=" + seen);
|
|
|
|
}
|
|
|
|
if (message.answered != answered) {
|
|
|
|
update = true;
|
|
|
|
message.answered = answered;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
Log.i("UID fetch answered=" + answered);
|
|
|
|
}
|
|
|
|
if (message.flagged != flagged) {
|
|
|
|
update = true;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.ui_flagged = flagged;
|
|
|
|
Log.i("UID fetch flagged=" + flagged);
|
|
|
|
}
|
|
|
|
if (message.deleted != deleted) {
|
|
|
|
update = true;
|
|
|
|
message.deleted = deleted;
|
|
|
|
message.ui_deleted = deleted;
|
|
|
|
Log.i("UID fetch deleted=" + deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update)
|
|
|
|
db.message().updateMessage(message);
|
|
|
|
}
|
2021-02-19 17:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
} else {
|
|
|
|
for (Response response : responses)
|
|
|
|
if (response.isBYE())
|
|
|
|
return new MessagingException("UID FETCH", new IOException(response.toString()));
|
|
|
|
else if (response.isNO() || response.isBAD())
|
|
|
|
return new MessagingException(response.toString());
|
|
|
|
return new MessagingException("UID FETCH failed");
|
|
|
|
}
|
2019-05-22 11:17:42 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (ex != null)
|
|
|
|
throw ex;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
stats.uids = uids.size();
|
|
|
|
stats.uids_ms = (SystemClock.elapsedRealtime() - getuid);
|
|
|
|
Log.i(folder.name + " remote uids=" + stats.uids_ms + " ms");
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
List<EntityRule> rules = db.rule().getEnabledRules(folder.id);
|
2019-07-27 05:24:59 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
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);
|
|
|
|
if (account.isGmail())
|
|
|
|
fp.add(GmailFolder.FetchProfileItem.THRID);
|
|
|
|
|
|
|
|
// Add/update local messages
|
|
|
|
int synced = 0;
|
|
|
|
Log.i(folder.name + " add=" + imessages.length);
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0; i -= SYNC_BATCH_SIZE) {
|
|
|
|
state.ensureRunning("Sync/IMAP");
|
|
|
|
|
2021-04-07 06:42:47 +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) {
|
|
|
|
long uid = ifolder.getUID(imessage); // already fetched
|
|
|
|
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);
|
|
|
|
stats.headers += full.size();
|
|
|
|
stats.headers_ms += (SystemClock.elapsedRealtime() - headers);
|
|
|
|
Log.i(folder.name + " fetched headers=" + full.size() + " " + stats.headers_ms + " ms");
|
|
|
|
}
|
2020-09-02 08:12:43 +00:00
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
int free = Log.getFreeMemMb();
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("account", account.id + ":" + account.protocol);
|
|
|
|
crumb.put("folder", folder.id + ":" + folder.type);
|
|
|
|
crumb.put("start", Integer.toString(from));
|
|
|
|
crumb.put("end", Integer.toString(i));
|
|
|
|
crumb.put("free", Integer.toString(free));
|
|
|
|
crumb.put("partial", Boolean.toString(account.partial_fetch));
|
|
|
|
Log.breadcrumb("sync", crumb);
|
|
|
|
Log.i("Sync " + from + ".." + i + " free=" + free);
|
|
|
|
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int j = isub.length - 1; j >= 0; j--) {
|
|
|
|
state.ensureRunning("Sync/IMAP");
|
|
|
|
|
2021-04-07 06:42:47 +00:00
|
|
|
try {
|
|
|
|
// Some providers erroneously return old messages
|
|
|
|
if (full.contains(isub[j]))
|
2020-09-02 08:12:43 +00:00
|
|
|
try {
|
2021-04-07 06:42:47 +00:00
|
|
|
Date received = isub[j].getReceivedDate();
|
|
|
|
boolean unseen = (sync_unseen && !isub[j].isSet(Flags.Flag.SEEN));
|
|
|
|
boolean flagged = (sync_flagged && isub[j].isSet(Flags.Flag.FLAGGED));
|
|
|
|
if (received != null && received.getTime() < keep_time && !unseen && !flagged) {
|
|
|
|
long uid = ifolder.getUID(isub[j]);
|
|
|
|
Log.i(folder.name + " Skipping old uid=" + uid + " date=" + received);
|
|
|
|
ids[from + j] = null;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
2020-09-02 08:12:43 +00:00
|
|
|
Log.w(ex);
|
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
|
|
|
|
EntityMessage message = synchronizeMessage(
|
|
|
|
context,
|
|
|
|
account, folder,
|
|
|
|
istore, ifolder, (MimeMessage) isub[j],
|
|
|
|
false, download && initialize == 0,
|
|
|
|
rules, state, stats);
|
|
|
|
ids[from + j] = (message == null || message.ui_hide ? null : message.id);
|
|
|
|
|
|
|
|
if (message != null && full.contains(isub[j]))
|
|
|
|
if ((++synced % SYNC_YIELD_COUNT) == 0)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " yield synced=" + synced);
|
|
|
|
Thread.sleep(SYNC_YIELD_DURATION);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
} catch (MessageRemovedException ex) {
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.w(folder.name, ex);
|
2021-04-07 06:42:47 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
2019-02-27 15:05:15 +00:00
|
|
|
throw ex;
|
2021-04-07 06:42:47 +00:00
|
|
|
} catch (IOException ex) {
|
|
|
|
if (ex.getCause() instanceof MessagingException) {
|
|
|
|
Log.w(folder.name, ex);
|
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
} else
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
db.folder().setFolderError(folder.id, Log.formatThrowable(ex));
|
|
|
|
} finally {
|
|
|
|
// Free memory
|
2021-06-18 09:42:32 +00:00
|
|
|
isub[j] = null;
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
2021-07-27 16:32:10 +00:00
|
|
|
}
|
2021-04-07 06:42:47 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 10:20:54 +00:00
|
|
|
// Delete not synchronized messages without uid
|
2021-01-06 13:29:47 +00:00
|
|
|
if (!EntityFolder.isOutgoing(folder.type)) {
|
2021-05-10 15:51:54 +00:00
|
|
|
int orphans = db.message().deleteOrphans(folder.id, new Date().getTime());
|
2021-01-06 13:29:47 +00:00
|
|
|
Log.i(folder.name + " deleted orphans=" + orphans);
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-04-08 05:57:28 +00:00
|
|
|
// Update modseq
|
|
|
|
folder.modseq = modseq;
|
|
|
|
db.folder().setFolderModSeq(folder.id, folder.modseq);
|
|
|
|
|
|
|
|
// Update stats
|
2020-08-08 09:51:41 +00:00
|
|
|
int count = MessageHelper.getMessageCount(ifolder);
|
2019-03-09 08:20:46 +00:00
|
|
|
db.folder().setFolderTotal(folder.id, count < 0 ? null : count);
|
2020-01-19 07:33:37 +00:00
|
|
|
account.last_connected = new Date().getTime();
|
|
|
|
db.account().setAccountConnected(account.id, account.last_connected);
|
2019-03-09 08:20:46 +00:00
|
|
|
|
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
|
2020-09-02 08:12:43 +00:00
|
|
|
int downloaded = 0;
|
2019-02-27 15:05:15 +00:00
|
|
|
Log.i(folder.name + " download=" + imessages.length);
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int i = imessages.length - 1; i >= 0; i -= DOWNLOAD_BATCH_SIZE) {
|
|
|
|
state.ensureRunning("Sync/IMAP");
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-07-27 16:32:10 +00:00
|
|
|
int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1);
|
2019-02-27 15:05:15 +00:00
|
|
|
Message[] isub = Arrays.copyOfRange(imessages, from, i + 1);
|
2021-03-01 19:03:23 +00:00
|
|
|
Arrays.fill(imessages, from, i + 1, null);
|
2019-02-27 15:05:15 +00:00
|
|
|
// 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<>();
|
2020-11-05 09:33:37 +00:00
|
|
|
crumb.put("account", account.id + ":" + account.protocol);
|
|
|
|
crumb.put("folder", folder.id + ":" + folder.type);
|
2019-05-16 20:06:13 +00:00
|
|
|
crumb.put("start", Integer.toString(from));
|
|
|
|
crumb.put("end", Integer.toString(i));
|
|
|
|
crumb.put("free", Integer.toString(free));
|
2019-10-19 10:20:37 +00:00
|
|
|
crumb.put("partial", Boolean.toString(account.partial_fetch));
|
2019-08-12 11:07:14 +00:00
|
|
|
Log.breadcrumb("download", crumb);
|
2019-05-16 20:06:13 +00:00
|
|
|
Log.i("Download " + from + ".." + i + " free=" + free);
|
|
|
|
|
2021-07-27 16:32:10 +00:00
|
|
|
for (int j = isub.length - 1; j >= 0; j--) {
|
|
|
|
state.ensureRunning("Sync/IMAP");
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
try {
|
2020-09-03 13:50:33 +00:00
|
|
|
if (ids[from + j] != null) {
|
|
|
|
boolean fetched = downloadMessage(
|
2020-09-21 19:46:39 +00:00
|
|
|
context,
|
2019-10-10 07:35:07 +00:00
|
|
|
account, folder,
|
|
|
|
istore, ifolder,
|
2020-07-15 16:58:03 +00:00
|
|
|
(MimeMessage) isub[j], ids[from + j],
|
2020-09-03 13:50:33 +00:00
|
|
|
state, stats);
|
|
|
|
if (fetched)
|
2020-09-02 08:12:43 +00:00
|
|
|
if ((++downloaded % DOWNLOAD_YIELD_COUNT) == 0)
|
|
|
|
try {
|
|
|
|
Log.i(folder.name + " yield downloaded=" + downloaded);
|
|
|
|
Thread.sleep(DOWNLOAD_YIELD_DURATION);
|
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-09-03 13:50:33 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (FolderClosedException ex) {
|
|
|
|
throw ex;
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(folder.name, ex);
|
|
|
|
} finally {
|
|
|
|
// Free memory
|
2021-03-01 19:03:23 +00:00
|
|
|
isub[j] = null;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2021-07-27 16:32:10 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-07-24 20:01:55 +00:00
|
|
|
db.folder().setFolderLastSync(folder.id, new Date().getTime());
|
2021-02-28 08:42:02 +00:00
|
|
|
//db.folder().setFolderError(folder.id, null);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
stats.total = (SystemClock.elapsedRealtime() - search);
|
2020-07-25 05:33:58 +00:00
|
|
|
|
2020-08-10 07:53:06 +00:00
|
|
|
EntityLog.log(context, account.name + "/" + folder.name + " sync stats " + stats);
|
2019-02-27 15:05:15 +00:00
|
|
|
} finally {
|
|
|
|
Log.i(folder.name + " end sync state=" + state);
|
|
|
|
db.folder().setFolderSyncState(folder.id, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static EntityMessage synchronizeMessage(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context,
|
2019-03-18 09:52:20 +00:00
|
|
|
EntityAccount account, EntityFolder folder,
|
2019-10-05 18:44:45 +00:00
|
|
|
IMAPStore istore, IMAPFolder ifolder, MimeMessage imessage,
|
2019-07-15 10:54:53 +00:00
|
|
|
boolean browsed, boolean download,
|
2020-07-15 16:58:03 +00:00
|
|
|
List<EntityRule> rules, State state, SyncStats stats) throws MessagingException, IOException {
|
2021-02-11 08:51:48 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-04-26 06:15:53 +00:00
|
|
|
boolean download_headers = prefs.getBoolean("download_headers", false);
|
2021-02-11 08:51:48 +00:00
|
|
|
boolean notify_known = prefs.getBoolean("notify_known", false);
|
|
|
|
boolean perform_expunge = prefs.getBoolean("perform_expunge", true);
|
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-09-26 08:53:27 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
long uid = ifolder.getUID(imessage);
|
2020-06-14 15:18:29 +00:00
|
|
|
if (uid < 0) {
|
2021-02-27 17:21:22 +00:00
|
|
|
Log.w(folder.name + " invalid uid=" + uid);
|
2020-06-14 15:18:29 +00:00
|
|
|
throw new MessageRemovedException("uid");
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (imessage.isExpunged()) {
|
2021-02-27 17:21:22 +00:00
|
|
|
Log.w(folder.name + " expunged uid=" + uid);
|
2019-09-17 11:38:23 +00:00
|
|
|
throw new MessageRemovedException("Expunged");
|
|
|
|
}
|
2021-02-11 08:51:48 +00:00
|
|
|
if (perform_expunge && imessage.isSet(Flags.Flag.DELETED)) {
|
2021-02-27 17:21:22 +00:00
|
|
|
Log.w(folder.name + " deleted uid=" + uid);
|
2021-02-11 08:51:48 +00:00
|
|
|
try {
|
|
|
|
ifolder.expunge();
|
|
|
|
} catch (MessagingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
throw new MessageRemovedException("Deleted");
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage, context);
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean seen = helper.getSeen();
|
2020-05-23 14:06:32 +00:00
|
|
|
boolean answered = helper.getAnswered();
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean flagged = helper.getFlagged();
|
2021-02-11 08:51:48 +00:00
|
|
|
boolean deleted = helper.getDeleted();
|
2019-09-17 11:38:23 +00:00
|
|
|
String flags = helper.getFlags();
|
|
|
|
String[] keywords = helper.getKeywords();
|
2020-06-25 07:14:05 +00:00
|
|
|
String[] labels = helper.getLabels();
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean update = false;
|
|
|
|
boolean process = false;
|
2020-12-28 08:23:42 +00:00
|
|
|
boolean syncSimilar = false;
|
2019-07-15 11:48:27 +00:00
|
|
|
|
2019-09-17 11:38:23 +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
|
2021-06-26 10:19:23 +00:00
|
|
|
boolean have = false;
|
2020-02-10 10:40:53 +00:00
|
|
|
Integer color = null;
|
2021-03-01 14:42:05 +00:00
|
|
|
String notes = null;
|
2021-04-08 07:40:21 +00:00
|
|
|
Integer notes_color = null;
|
2019-09-17 11:38:23 +00:00
|
|
|
if (message == null) {
|
|
|
|
String msgid = helper.getMessageID();
|
|
|
|
Log.i(folder.name + " searching for " + msgid);
|
2020-04-04 09:21:08 +00:00
|
|
|
List<EntityMessage> dups = db.message().getMessagesByMsgId(folder.account, msgid);
|
|
|
|
for (EntityMessage dup : dups) {
|
2019-09-17 11:38:23 +00:00
|
|
|
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);
|
|
|
|
|
2021-06-26 10:19:23 +00:00
|
|
|
if (!EntityFolder.JUNK.equals(dfolder.type))
|
|
|
|
have = true;
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (dup.folder.equals(folder.id)) {
|
|
|
|
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);
|
|
|
|
|
2020-03-25 07:14:32 +00:00
|
|
|
if (dup.uid == null) {
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " set uid=" + uid);
|
|
|
|
dup.uid = uid;
|
|
|
|
dup.thread = thread;
|
|
|
|
|
|
|
|
if (EntityFolder.SENT.equals(folder.type)) {
|
2020-07-22 16:46:57 +00:00
|
|
|
Long sent = helper.getSent();
|
|
|
|
Long received = helper.getReceived();
|
|
|
|
if (sent != null)
|
|
|
|
dup.sent = sent;
|
|
|
|
if (received != null)
|
|
|
|
dup.received = received;
|
2019-08-29 19:41:52 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
|
|
|
|
dup.error = null;
|
2020-03-25 07:14:15 +00:00
|
|
|
|
|
|
|
message = dup;
|
2020-03-25 07:14:32 +00:00
|
|
|
process = true;
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-02-10 10:40:53 +00:00
|
|
|
|
2020-12-28 08:23:42 +00:00
|
|
|
if (dup.seen != seen || dup.answered != answered || dup.flagged != flagged)
|
|
|
|
syncSimilar = true;
|
|
|
|
|
2020-02-10 10:40:53 +00:00
|
|
|
if (dup.flagged && dup.color != null)
|
|
|
|
color = dup.color;
|
2021-04-08 07:40:21 +00:00
|
|
|
if (dup.notes != null) {
|
2021-03-01 14:42:05 +00:00
|
|
|
notes = dup.notes;
|
2021-04-08 07:40:21 +00:00
|
|
|
notes_color = dup.notes_color;
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (message == null) {
|
2020-02-06 21:35:36 +00:00
|
|
|
Long sent = helper.getSent();
|
2020-07-22 16:46:57 +00:00
|
|
|
|
|
|
|
Long received;
|
2020-07-24 11:26:18 +00:00
|
|
|
long future = new Date().getTime() + FUTURE_RECEIVED;
|
2020-07-22 16:46:57 +00:00
|
|
|
if (account.use_date) {
|
|
|
|
received = sent;
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceived();
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceivedHeader();
|
|
|
|
} else if (account.use_received) {
|
|
|
|
received = helper.getReceivedHeader();
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceived();
|
2020-07-22 12:57:47 +00:00
|
|
|
} else {
|
2020-02-06 21:35:36 +00:00
|
|
|
received = helper.getReceived();
|
2020-07-24 11:26:18 +00:00
|
|
|
if (received == null || received == 0 || received > future)
|
2020-07-22 16:46:57 +00:00
|
|
|
received = helper.getReceivedHeader();
|
2020-02-06 21:35:36 +00:00
|
|
|
}
|
2020-07-22 16:46:57 +00:00
|
|
|
if (received == null)
|
|
|
|
received = 0L;
|
2020-02-06 21:35:36 +00:00
|
|
|
|
2020-06-12 06:21:16 +00:00
|
|
|
String[] authentication = helper.getAuthentication();
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-09-17 11:38:23 +00:00
|
|
|
|
|
|
|
message = new EntityMessage();
|
|
|
|
message.account = folder.account;
|
|
|
|
message.folder = folder.id;
|
|
|
|
message.uid = uid;
|
|
|
|
|
|
|
|
message.msgid = helper.getMessageID();
|
|
|
|
if (TextUtils.isEmpty(message.msgid))
|
|
|
|
Log.w("No Message-ID id=" + message.id + " uid=" + message.uid);
|
|
|
|
|
2020-04-01 10:50:15 +00:00
|
|
|
message.hash = helper.getHash();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.references = TextUtils.join(" ", helper.getReferences());
|
|
|
|
message.inreplyto = helper.getInReplyTo();
|
|
|
|
// Local address contains control or whitespace in string ``mailing list someone@example.org''
|
|
|
|
message.deliveredto = helper.getDeliveredTo();
|
|
|
|
message.thread = helper.getThreadId(context, account.id, uid);
|
2019-09-30 14:55:58 +00:00
|
|
|
message.priority = helper.getPriority();
|
2020-10-02 13:54:45 +00:00
|
|
|
message.auto_submitted = helper.getAutoSubmitted();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.receipt_request = helper.getReceiptRequested();
|
|
|
|
message.receipt_to = helper.getReceiptTo();
|
2021-07-16 18:03:27 +00:00
|
|
|
message.bimi_selector = helper.getBimiSelector();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.dkim = MessageHelper.getAuthentication("dkim", authentication);
|
|
|
|
message.spf = MessageHelper.getAuthentication("spf", authentication);
|
|
|
|
message.dmarc = MessageHelper.getAuthentication("dmarc", authentication);
|
2021-02-01 10:54:43 +00:00
|
|
|
message.return_path = helper.getReturnPath();
|
2020-01-30 17:24:46 +00:00
|
|
|
message.submitter = helper.getSender();
|
2019-09-17 11:38:23 +00:00
|
|
|
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.unsubscribe = helper.getListUnsubscribe();
|
2019-12-24 16:18:48 +00:00
|
|
|
message.autocrypt = helper.getAutocrypt();
|
2021-04-26 06:15:53 +00:00
|
|
|
if (download_headers)
|
|
|
|
message.headers = helper.getHeaders();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.subject = helper.getSubject();
|
2019-09-30 19:00:28 +00:00
|
|
|
message.size = parts.getBodySize();
|
|
|
|
message.total = helper.getSize();
|
2019-09-17 11:38:23 +00:00
|
|
|
message.content = false;
|
2019-12-02 09:49:27 +00:00
|
|
|
message.encrypt = parts.getEncryption();
|
2020-01-18 10:28:37 +00:00
|
|
|
message.ui_encrypt = message.encrypt;
|
2020-02-06 21:35:36 +00:00
|
|
|
message.received = received;
|
2021-03-01 14:42:05 +00:00
|
|
|
message.notes = notes;
|
2021-04-08 07:40:21 +00:00
|
|
|
message.notes_color = notes_color;
|
2020-02-06 21:35:36 +00:00
|
|
|
message.sent = sent;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.seen = seen;
|
|
|
|
message.answered = answered;
|
|
|
|
message.flagged = flagged;
|
2021-02-11 08:51:48 +00:00
|
|
|
message.deleted = deleted;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.flags = flags;
|
|
|
|
message.keywords = keywords;
|
2020-06-25 07:14:05 +00:00
|
|
|
message.labels = labels;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.ui_seen = seen;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
message.ui_flagged = flagged;
|
2021-02-11 08:51:48 +00:00
|
|
|
message.ui_deleted = deleted;
|
2019-09-27 16:25:55 +00:00
|
|
|
message.ui_hide = false;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.ui_found = false;
|
|
|
|
message.ui_ignored = seen;
|
|
|
|
message.ui_browsed = browsed;
|
|
|
|
|
2020-02-10 10:40:53 +00:00
|
|
|
if (message.flagged)
|
|
|
|
message.color = color;
|
|
|
|
|
2021-04-25 10:52:19 +00:00
|
|
|
if (message.deliveredto != null)
|
|
|
|
try {
|
|
|
|
Address deliveredto = new InternetAddress(message.deliveredto);
|
|
|
|
if (MessageHelper.equalEmail(new Address[]{deliveredto}, message.to))
|
|
|
|
message.deliveredto = null;
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2021-01-11 08:13:34 +00:00
|
|
|
if (MessageHelper.equalEmail(message.submitter, message.from))
|
2020-01-30 17:24:46 +00:00
|
|
|
message.submitter = null;
|
|
|
|
|
2019-09-26 12:03:35 +00:00
|
|
|
// Borrow reply name from sender name
|
|
|
|
if (message.from != null && message.from.length == 1 &&
|
|
|
|
message.reply != null && message.reply.length == 1) {
|
|
|
|
InternetAddress reply = (InternetAddress) message.reply[0];
|
|
|
|
if (TextUtils.isEmpty(reply.getPersonal())) {
|
|
|
|
InternetAddress from = (InternetAddress) message.from[0];
|
|
|
|
reply.setPersonal(from.getPersonal());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
message.identity = (identity == null ? null : identity.id);
|
|
|
|
|
2020-09-15 07:37:23 +00:00
|
|
|
message.sender = MessageHelper.getSortKey(EntityFolder.isOutgoing(folder.type) ? message.to : message.from);
|
2020-05-06 20:30:47 +00:00
|
|
|
Uri lookupUri = ContactInfo.getLookupUri(message.from);
|
2019-09-17 11:38:23 +00:00
|
|
|
message.avatar = (lookupUri == null ? null : lookupUri.toString());
|
2020-06-07 14:32:14 +00:00
|
|
|
if (message.avatar == null && notify_known && pro)
|
2020-06-07 07:59:25 +00:00
|
|
|
message.ui_ignored = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
|
2021-06-10 14:19:58 +00:00
|
|
|
// For contact forms
|
|
|
|
boolean self = false;
|
|
|
|
if (identity != null && message.from != null)
|
|
|
|
for (Address from : message.from)
|
|
|
|
if (identity.sameAddress(from) || identity.similarAddress(from)) {
|
|
|
|
self = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-28 06:27:40 +00:00
|
|
|
|
2021-06-10 14:19:58 +00:00
|
|
|
if (!self) {
|
|
|
|
String warning = message.checkReplyDomain(context);
|
|
|
|
message.reply_domain = (warning == null);
|
2020-12-24 07:38:15 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
boolean check_mx = prefs.getBoolean("check_mx", false);
|
|
|
|
if (check_mx)
|
|
|
|
try {
|
2021-06-28 06:27:40 +00:00
|
|
|
Address[] addresses =
|
|
|
|
(message.reply == null || message.reply.length == 0
|
|
|
|
? message.from : message.reply);
|
2020-04-16 16:28:12 +00:00
|
|
|
DnsHelper.checkMx(context, addresses);
|
|
|
|
message.mx = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
} catch (UnknownHostException ex) {
|
|
|
|
message.mx = false;
|
|
|
|
message.warning = ex.getMessage();
|
|
|
|
} catch (Throwable ex) {
|
2019-09-17 19:25:41 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-12-06 07:50:46 +00:00
|
|
|
message.warning = Log.formatThrowable(ex, false);
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-07-16 12:44:21 +00:00
|
|
|
|
2021-06-19 08:00:38 +00:00
|
|
|
boolean check_blocklist = prefs.getBoolean("check_blocklist", false);
|
2021-07-06 12:42:03 +00:00
|
|
|
if (check_blocklist &&
|
|
|
|
!have &&
|
|
|
|
!EntityFolder.isOutgoing(folder.type) &&
|
|
|
|
!EntityFolder.ARCHIVE.equals(folder.type) &&
|
|
|
|
!EntityFolder.TRASH.equals(folder.type) &&
|
|
|
|
!EntityFolder.JUNK.equals(folder.type) &&
|
|
|
|
!Arrays.asList(message.keywords).contains(MessageHelper.FLAG_NOT_JUNK))
|
2021-06-29 12:06:30 +00:00
|
|
|
try {
|
2021-07-18 19:05:00 +00:00
|
|
|
message.blocklist = DnsBlockList.isJunk(context,
|
|
|
|
imessage.getHeader("Received"));
|
|
|
|
|
|
|
|
if (message.blocklist == null || !message.blocklist) {
|
|
|
|
List<Address> senders = new ArrayList<>();
|
|
|
|
if (message.reply != null)
|
|
|
|
senders.addAll(Arrays.asList(message.reply));
|
|
|
|
if (message.from != null)
|
|
|
|
senders.addAll(Arrays.asList(message.from));
|
|
|
|
message.blocklist = DnsBlockList.isJunk(context, senders);
|
|
|
|
}
|
2021-06-29 12:06:30 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(folder.name, ex);
|
2021-06-19 08:00:38 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
|
|
|
|
2021-01-27 14:48:49 +00:00
|
|
|
message.notifying = EntityMessage.NOTIFYING_IGNORE;
|
2019-09-17 11:38:23 +00:00
|
|
|
message.id = db.message().insertMessage(message);
|
|
|
|
Log.i(folder.name + " added id=" + message.id + " uid=" + message.uid);
|
|
|
|
|
|
|
|
int sequence = 1;
|
2019-10-13 14:26:37 +00:00
|
|
|
List<EntityAttachment> attachments = parts.getAttachments();
|
|
|
|
for (EntityAttachment attachment : attachments) {
|
2019-09-21 11:18:48 +00:00
|
|
|
Log.i(folder.name + " attachment seq=" + sequence + " " + attachment);
|
2019-09-17 11:38:23 +00:00
|
|
|
attachment.message = message.id;
|
|
|
|
attachment.sequence = sequence++;
|
|
|
|
attachment.id = db.attachment().insertAttachment(attachment);
|
|
|
|
}
|
2019-06-13 18:45:35 +00:00
|
|
|
|
2020-05-05 19:13:05 +00:00
|
|
|
runRules(context, imessage, account, folder, message, rules);
|
2021-06-20 07:04:12 +00:00
|
|
|
|
2021-07-06 12:42:03 +00:00
|
|
|
if (message.blocklist != null && message.blocklist) {
|
2021-06-20 07:04:12 +00:00
|
|
|
boolean use_blocklist = prefs.getBoolean("use_blocklist", false);
|
|
|
|
if (use_blocklist) {
|
|
|
|
EntityLog.log(context, "Block list" +
|
|
|
|
" folder=" + folder.name +
|
|
|
|
" message=" + message.id +
|
|
|
|
"@" + new Date(message.received) +
|
|
|
|
":" + message.subject);
|
|
|
|
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
|
2021-06-20 07:22:20 +00:00
|
|
|
if (junk != null) {
|
2021-06-20 07:04:12 +00:00
|
|
|
EntityOperation.queue(context, message, EntityOperation.MOVE, junk.id, false);
|
2021-06-20 07:22:20 +00:00
|
|
|
message.ui_hide = true;
|
|
|
|
}
|
2021-06-20 07:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-03 09:07:11 +00:00
|
|
|
if (download && !message.ui_hide &&
|
2021-01-13 09:22:52 +00:00
|
|
|
MessageClassifier.isEnabled(context) && folder.auto_classify_source)
|
2021-01-27 14:48:49 +00:00
|
|
|
db.message().setMessageUiHide(message.id, true); // keep local value
|
2019-11-22 12:50:16 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
db.setTransactionSuccessful();
|
2019-09-21 08:05:24 +00:00
|
|
|
} catch (SQLiteConstraintException ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
|
|
|
|
Map<String, String> crumb = new HashMap<>();
|
|
|
|
crumb.put("folder", message.account + ":" + message.folder + ":" + folder.type);
|
|
|
|
crumb.put("message", uid + ":" + message.uid);
|
|
|
|
crumb.put("what", ex.getMessage());
|
|
|
|
Log.breadcrumb("insert", crumb);
|
2019-10-09 13:43:14 +00:00
|
|
|
|
|
|
|
return null;
|
2019-09-17 11:38:23 +00:00
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-01-27 14:48:49 +00:00
|
|
|
try {
|
2021-04-25 09:27:34 +00:00
|
|
|
ContactInfo.update(context, account, folder, message);
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-01-27 14:48:49 +00:00
|
|
|
// Download small messages inline
|
|
|
|
if (download && !message.ui_hide) {
|
|
|
|
long maxSize;
|
|
|
|
if (state == null || state.networkState.isUnmetered())
|
2020-09-13 12:38:36 +00:00
|
|
|
maxSize = MessageHelper.SMALL_MESSAGE_SIZE;
|
2021-01-27 14:48:49 +00:00
|
|
|
else {
|
|
|
|
maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE);
|
|
|
|
if (maxSize == 0 || maxSize > MessageHelper.SMALL_MESSAGE_SIZE)
|
|
|
|
maxSize = MessageHelper.SMALL_MESSAGE_SIZE;
|
2021-01-15 17:22:18 +00:00
|
|
|
}
|
2021-01-27 14:48:49 +00:00
|
|
|
|
|
|
|
if ((message.size != null && message.size < maxSize) ||
|
|
|
|
(MessageClassifier.isEnabled(context)) && folder.auto_classify_source)
|
|
|
|
try {
|
|
|
|
String body = parts.getHtml(context);
|
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
|
|
|
String text = HtmlHelper.getFullText(body);
|
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
|
|
|
message.language,
|
|
|
|
parts.isPlainOnly(),
|
|
|
|
message.preview,
|
|
|
|
parts.getWarnings(message.warning));
|
|
|
|
MessageClassifier.classify(message, folder, null, context);
|
|
|
|
|
|
|
|
if (stats != null && body != null)
|
|
|
|
stats.content += body.length();
|
|
|
|
Log.i(folder.name + " inline downloaded message id=" + message.id +
|
|
|
|
" size=" + message.size + "/" + (body == null ? null : body.length()));
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(body) && parts.hasBody())
|
|
|
|
reportEmptyMessage(context, state, account, istore);
|
|
|
|
} finally {
|
|
|
|
if (!message.ui_hide)
|
|
|
|
db.message().setMessageUiHide(message.id, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
db.message().setMessageNotifying(message.id, 0);
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2021-01-04 08:32:22 +00:00
|
|
|
|
|
|
|
reportNewMessage(context, account, folder, message);
|
2019-09-17 11:38:23 +00:00
|
|
|
} else {
|
|
|
|
if (process) {
|
|
|
|
EntityIdentity identity = matchIdentity(context, folder, message);
|
|
|
|
if (identity != null &&
|
|
|
|
(message.identity == null || !message.identity.equals(identity.id))) {
|
|
|
|
message.identity = identity.id;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " identity=" + identity.id);
|
2019-03-31 18:54:01 +00:00
|
|
|
}
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if ((!message.seen.equals(seen) || !message.ui_seen.equals(seen)) &&
|
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.SEEN) == 0) {
|
|
|
|
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);
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-09-25 19:23:33 +00:00
|
|
|
if ((!message.answered.equals(answered) || !message.ui_answered.equals(message.answered)) &&
|
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.ANSWERED) == 0) {
|
2019-09-17 11:38:23 +00:00
|
|
|
update = true;
|
|
|
|
message.answered = answered;
|
|
|
|
message.ui_answered = answered;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " answered=" + answered);
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if ((!message.flagged.equals(flagged) || !message.ui_flagged.equals(flagged)) &&
|
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.FLAG) == 0) {
|
|
|
|
update = true;
|
|
|
|
message.flagged = flagged;
|
|
|
|
message.ui_flagged = flagged;
|
2019-09-28 18:25:19 +00:00
|
|
|
if (!flagged)
|
|
|
|
message.color = null;
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " flagged=" + flagged);
|
2020-12-28 08:23:42 +00:00
|
|
|
syncSimilar = true;
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-02-11 08:51:48 +00:00
|
|
|
if ((!message.deleted.equals(deleted) || !message.ui_deleted.equals(deleted)) &&
|
|
|
|
db.operation().getOperationCount(folder.id, message.id, EntityOperation.DELETE) == 0) {
|
|
|
|
update = true;
|
|
|
|
message.deleted = deleted;
|
|
|
|
message.ui_deleted = deleted;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " deleted=" + deleted);
|
|
|
|
syncSimilar = true;
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +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
|
|
|
|
2020-09-17 15:42:08 +00:00
|
|
|
if (!Helper.equal(message.keywords, keywords) &&
|
|
|
|
ifolder.getPermanentFlags().contains(Flags.Flag.USER)) {
|
2019-09-17 11:38:23 +00:00
|
|
|
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
|
|
|
|
2020-06-25 07:14:05 +00:00
|
|
|
if (!Helper.equal(message.labels, labels)) {
|
|
|
|
update = true;
|
|
|
|
message.labels = labels;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid +
|
|
|
|
" labels=" + (labels == null ? null : TextUtils.join(" ", labels)));
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:12:29 +00:00
|
|
|
if (download_headers && message.headers == null) {
|
|
|
|
update = true;
|
|
|
|
message.headers = helper.getHeaders();
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " headers");
|
|
|
|
}
|
|
|
|
|
2020-04-01 14:05:45 +00:00
|
|
|
if (message.hash == null || process) {
|
2020-04-01 10:50:15 +00:00
|
|
|
update = true;
|
|
|
|
message.hash = helper.getHash();
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " hash=" + message.hash);
|
2020-04-01 14:17:54 +00:00
|
|
|
|
|
|
|
// Update archive to prevent visible > 1
|
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
for (EntityMessage dup : db.message().getMessagesByMsgId(message.account, message.msgid))
|
|
|
|
db.message().setMessageHash(dup.id, message.hash);
|
2020-04-01 10:50:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 09:29:46 +00:00
|
|
|
if (message.ui_hide &&
|
|
|
|
(message.ui_busy == null || message.ui_busy < new Date().getTime()) &&
|
2020-11-10 18:14:21 +00:00
|
|
|
db.operation().getOperationCount(folder.id, message.id) == 0 &&
|
|
|
|
db.operation().getOperationCount(folder.id, EntityOperation.PURGE) == 0) {
|
2019-09-17 11:38:23 +00:00
|
|
|
update = true;
|
2019-09-27 16:25:55 +00:00
|
|
|
message.ui_hide = false;
|
2019-09-17 11:38:23 +00:00
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " unhide");
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (message.ui_browsed != browsed) {
|
|
|
|
update = true;
|
|
|
|
message.ui_browsed = browsed;
|
|
|
|
Log.i(folder.name + " updated id=" + message.id + " uid=" + message.uid + " browsed=" + browsed);
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2020-05-06 20:30:47 +00:00
|
|
|
Uri uri = ContactInfo.getLookupUri(message.from);
|
2020-04-15 18:26:04 +00:00
|
|
|
if (uri != null) {
|
|
|
|
String avatar = 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-09-17 11:38:23 +00:00
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
if (update || process)
|
|
|
|
try {
|
|
|
|
db.beginTransaction();
|
2019-06-13 18:45:35 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
db.message().updateMessage(message);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2021-01-04 08:32:22 +00:00
|
|
|
if (process)
|
2020-05-05 19:13:05 +00:00
|
|
|
runRules(context, imessage, account, folder, message, rules);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
db.setTransactionSuccessful();
|
|
|
|
} finally {
|
|
|
|
db.endTransaction();
|
|
|
|
}
|
2019-03-31 18:54:01 +00:00
|
|
|
|
2021-01-02 14:57:24 +00:00
|
|
|
if (process) {
|
2021-04-25 09:27:34 +00:00
|
|
|
ContactInfo.update(context, account, folder, message);
|
2021-01-05 18:51:22 +00:00
|
|
|
MessageClassifier.classify(message, folder, null, context);
|
2021-01-02 14:57:24 +00:00
|
|
|
} else
|
2019-12-07 16:02:42 +00:00
|
|
|
Log.d(folder.name + " unchanged uid=" + uid);
|
2021-01-04 08:32:22 +00:00
|
|
|
|
|
|
|
if (process)
|
|
|
|
reportNewMessage(context, account, folder, message);
|
2019-09-17 11:38:23 +00:00
|
|
|
}
|
2019-06-14 09:25:56 +00:00
|
|
|
|
2020-12-28 08:23:42 +00:00
|
|
|
if (syncSimilar && account.isGmail())
|
|
|
|
for (EntityMessage similar : db.message().getMessagesBySimilarity(message.account, message.id, message.msgid)) {
|
|
|
|
if (similar.seen != message.seen) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " seen=" + message.seen);
|
|
|
|
db.message().setMessageSeen(similar.id, message.seen);
|
|
|
|
db.message().setMessageUiSeen(similar.id, message.seen);
|
|
|
|
}
|
|
|
|
if (similar.answered != message.answered) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " answered=" + message.answered);
|
|
|
|
db.message().setMessageAnswered(similar.id, message.answered);
|
|
|
|
}
|
|
|
|
if (similar.flagged != flagged) {
|
|
|
|
Log.i(folder.name + " Synchronize similar id=" + similar.id + " flagged=" + message.flagged);
|
|
|
|
db.message().setMessageFlagged(similar.id, message.flagged);
|
|
|
|
db.message().setMessageUiFlagged(similar.id, message.flagged, flagged ? similar.color : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
List<String> fkeywords = new ArrayList<>(Arrays.asList(folder.keywords));
|
2019-06-14 09:25:56 +00:00
|
|
|
|
2019-09-17 11:38:23 +00:00
|
|
|
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-09-17 11:38:23 +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-09-17 11:38:23 +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);
|
|
|
|
|
2020-08-01 06:05:06 +00:00
|
|
|
if (EntityFolder.DRAFTS.equals(folder.type))
|
|
|
|
return null;
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
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));
|
2019-10-13 14:37:38 +00:00
|
|
|
if (message.bcc != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.bcc));
|
2020-02-12 06:48:40 +00:00
|
|
|
if (message.from != null)
|
|
|
|
addresses.addAll(Arrays.asList(message.from));
|
2019-06-13 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 14:57:12 +00:00
|
|
|
InternetAddress deliveredto = null;
|
|
|
|
if (message.deliveredto != null)
|
|
|
|
try {
|
|
|
|
deliveredto = new InternetAddress(message.deliveredto);
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
|
2019-06-13 18:45:35 +00:00
|
|
|
// Search for matching identity
|
2021-04-25 10:59:11 +00:00
|
|
|
List<EntityIdentity> identities = getIdentities(folder.account, context);
|
2019-09-22 18:03:31 +00:00
|
|
|
if (identities != null) {
|
|
|
|
for (Address address : addresses)
|
|
|
|
for (EntityIdentity identity : identities)
|
2019-09-23 08:47:05 +00:00
|
|
|
if (identity.sameAddress(address))
|
2019-09-22 18:03:31 +00:00
|
|
|
return identity;
|
|
|
|
|
|
|
|
for (Address address : addresses)
|
|
|
|
for (EntityIdentity identity : identities)
|
2019-09-23 08:47:05 +00:00
|
|
|
if (identity.similarAddress(address))
|
2019-09-22 18:03:31 +00:00
|
|
|
return identity;
|
2020-12-13 14:57:12 +00:00
|
|
|
|
|
|
|
if (deliveredto != null)
|
|
|
|
for (EntityIdentity identity : identities)
|
|
|
|
if (identity.sameAddress(deliveredto) || identity.similarAddress(deliveredto))
|
|
|
|
return identity;
|
2019-06-13 18:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-05 19:13:05 +00:00
|
|
|
private static void runRules(
|
|
|
|
Context context, Message imessage,
|
|
|
|
EntityAccount account, EntityFolder folder, EntityMessage message,
|
|
|
|
List<EntityRule> rules) {
|
2019-10-13 14:26:37 +00:00
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
if (!ActivityBilling.isPro(context))
|
2019-03-31 06:48:42 +00:00
|
|
|
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);
|
2019-12-06 07:50:46 +00:00
|
|
|
db.message().setMessageError(message.id, Log.formatThrowable(ex));
|
2019-03-31 06:48:42 +00:00
|
|
|
}
|
2020-06-22 08:22:25 +00:00
|
|
|
}
|
2020-05-05 19:13:05 +00:00
|
|
|
|
2020-06-22 08:22:25 +00:00
|
|
|
private static void reportNewMessage(Context context, EntityAccount account, EntityFolder folder, EntityMessage message) {
|
2020-05-05 19:13:05 +00:00
|
|
|
// Prepare scroll to top
|
|
|
|
if (!message.ui_seen && !message.ui_hide &&
|
|
|
|
message.received > account.created) {
|
|
|
|
Intent report = new Intent(ActivityView.ACTION_NEW_MESSAGE);
|
|
|
|
report.putExtra("folder", folder.id);
|
|
|
|
report.putExtra("unified", folder.unified);
|
|
|
|
Log.i("Report new id=" + message.id + " folder=" + folder.name + " unified=" + folder.unified);
|
|
|
|
|
|
|
|
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
|
|
|
|
lbm.sendBroadcast(report);
|
|
|
|
}
|
2019-03-31 06:48:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 08:12:43 +00:00
|
|
|
private static boolean downloadMessage(
|
2020-09-21 19:46:39 +00:00
|
|
|
Context context,
|
2019-10-10 07:35:07 +00:00
|
|
|
EntityAccount account, EntityFolder folder,
|
|
|
|
IMAPStore istore, IMAPFolder ifolder,
|
2020-07-15 16:58:03 +00:00
|
|
|
MimeMessage imessage, long id, State state, SyncStats stats) throws MessagingException, IOException {
|
2019-04-19 16:54:40 +00:00
|
|
|
if (state.getNetworkState().isRoaming())
|
2020-09-02 08:12:43 +00:00
|
|
|
return false;
|
2019-04-19 16:54:40 +00:00
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
EntityMessage message = db.message().getMessage(id);
|
2020-05-25 12:31:29 +00:00
|
|
|
if (message == null || message.ui_hide)
|
2020-09-02 08:12:43 +00:00
|
|
|
return false;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2020-07-13 13:34:31 +00:00
|
|
|
long maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE);
|
2019-02-27 15:05:15 +00:00
|
|
|
if (maxSize == 0)
|
|
|
|
maxSize = Long.MAX_VALUE;
|
2021-04-26 06:15:53 +00:00
|
|
|
boolean download_eml = prefs.getBoolean("download_eml", false);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-09-22 07:53:55 +00:00
|
|
|
// Fetch on demand to prevent OOM
|
|
|
|
|
|
|
|
//FetchProfile fp = new FetchProfile();
|
2019-05-17 15:41:23 +00:00
|
|
|
//fp.add(FetchProfile.Item.ENVELOPE);
|
|
|
|
//fp.add(FetchProfile.Item.FLAGS);
|
2019-09-22 07:53:55 +00:00
|
|
|
//fp.add(FetchProfile.Item.CONTENT_INFO); // body structure
|
2019-05-17 15:41:23 +00:00
|
|
|
//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);
|
2020-06-25 07:14:05 +00:00
|
|
|
//if (account.isGmail()) {
|
2020-05-01 19:47:59 +00:00
|
|
|
// fp.add(GmailFolder.FetchProfileItem.THRID);
|
2020-06-25 07:14:05 +00:00
|
|
|
// fp.add(GmailFolder.FetchProfileItem.LABELS);
|
|
|
|
//}
|
2019-09-22 07:53:55 +00:00
|
|
|
//ifolder.fetch(new Message[]{imessage}, fp);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-05-23 14:05:29 +00:00
|
|
|
MessageHelper helper = new MessageHelper(imessage, context);
|
|
|
|
MessageHelper.MessageParts parts = helper.getMessageParts();
|
2019-02-27 15:05:15 +00:00
|
|
|
|
|
|
|
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);
|
2020-02-20 09:35:01 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
Helper.writeText(file, body);
|
2021-01-19 10:57:59 +00:00
|
|
|
String text = HtmlHelper.getFullText(body);
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview = HtmlHelper.getPreview(text);
|
2021-01-25 13:49:56 +00:00
|
|
|
message.language = HtmlHelper.getLanguage(context, message.subject, text);
|
2019-05-04 18:52:21 +00:00
|
|
|
db.message().setMessageContent(message.id,
|
|
|
|
true,
|
2021-01-19 11:18:03 +00:00
|
|
|
message.language,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.isPlainOnly(),
|
2021-01-19 11:18:03 +00:00
|
|
|
message.preview,
|
2019-05-04 18:52:21 +00:00
|
|
|
parts.getWarnings(message.warning));
|
2021-01-05 18:51:22 +00:00
|
|
|
MessageClassifier.classify(message, folder, null, context);
|
2021-01-02 14:57:24 +00:00
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
if (stats != null && body != null)
|
|
|
|
stats.content += body.length();
|
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
|
|
|
|
2020-04-18 07:44:16 +00:00
|
|
|
if (TextUtils.isEmpty(body) && parts.hasBody())
|
2020-09-21 19:46:39 +00:00
|
|
|
reportEmptyMessage(context, state, account, istore);
|
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 {
|
2021-03-18 16:46:20 +00:00
|
|
|
parts.downloadAttachment(context, attachment);
|
2020-07-15 16:58:03 +00:00
|
|
|
if (stats != null && attachment.size != null)
|
|
|
|
stats.attachments += attachment.size;
|
2019-03-14 08:05:28 +00:00
|
|
|
} catch (Throwable ex) {
|
2019-09-17 19:25:41 +00:00
|
|
|
Log.e(folder.name, ex);
|
2019-12-06 07:50:46 +00:00
|
|
|
db.attachment().setError(attachment.id, Log.formatThrowable(ex, false));
|
2019-03-14 08:05:28 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-09-02 08:12:43 +00:00
|
|
|
|
2021-04-26 06:15:53 +00:00
|
|
|
if (download_eml &&
|
2021-04-26 08:12:29 +00:00
|
|
|
(message.raw == null || !message.raw) &&
|
2021-04-26 06:15:53 +00:00
|
|
|
(state.getNetworkState().isUnmetered() || (message.total != null && message.total < maxSize))) {
|
|
|
|
File file = message.getRawFile(context);
|
|
|
|
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
|
|
|
|
imessage.writeTo(os);
|
|
|
|
}
|
|
|
|
|
2021-04-26 08:12:29 +00:00
|
|
|
message.raw = true;
|
|
|
|
db.message().setMessageRaw(message.id, message.raw);
|
2021-04-26 06:15:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 08:12:43 +00:00
|
|
|
return fetch;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
private static void reportEmptyMessage(Context context, State state, EntityAccount account, IMAPStore istore) {
|
2019-10-10 07:35:07 +00:00
|
|
|
try {
|
|
|
|
if (istore.hasCapability("ID")) {
|
|
|
|
Map<String, String> id = new LinkedHashMap<>();
|
|
|
|
id.put("name", context.getString(R.string.app_name));
|
|
|
|
id.put("version", BuildConfig.VERSION_NAME);
|
|
|
|
Map<String, String> sid = istore.id(id);
|
|
|
|
if (sid != null) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (String key : sid.keySet())
|
|
|
|
sb.append(" ").append(key).append("=").append(sid.get(key));
|
2020-03-17 08:09:29 +00:00
|
|
|
if (!account.partial_fetch)
|
2020-07-06 10:29:20 +00:00
|
|
|
Log.w("Empty message" + sb.toString());
|
2019-10-10 07:35:07 +00:00
|
|
|
}
|
2020-03-17 08:09:29 +00:00
|
|
|
} else {
|
|
|
|
if (!account.partial_fetch)
|
2020-07-06 10:29:20 +00:00
|
|
|
Log.w("Empty message " + account.host);
|
2020-03-17 08:09:29 +00:00
|
|
|
}
|
2019-10-10 07:35:07 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2020-03-06 14:29:33 +00:00
|
|
|
|
|
|
|
// Auto disable partial fetch
|
|
|
|
if (account.partial_fetch) {
|
|
|
|
account.partial_fetch = false;
|
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
db.account().setAccountPartialFetch(account.id, account.partial_fetch);
|
2020-09-21 19:46:39 +00:00
|
|
|
state.error(new StoreClosedException(istore));
|
2020-03-06 14:29:33 +00:00
|
|
|
}
|
2019-10-10 07:35:07 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 09:47:03 +00:00
|
|
|
static void notifyMessages(Context context, List<TupleMessageEx> messages, NotificationData data, boolean foreground) {
|
2019-08-01 07:19:22 +00:00
|
|
|
if (messages == null)
|
|
|
|
messages = new ArrayList<>();
|
2019-02-27 18:29:58 +00:00
|
|
|
|
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
|
|
|
|
2020-07-25 17:54:28 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-09-30 09:49:42 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2021-05-29 13:36:22 +00:00
|
|
|
boolean badge = prefs.getBoolean("badge", true);
|
2020-07-25 17:54:28 +00:00
|
|
|
boolean notify_background_only = prefs.getBoolean("notify_background_only", false);
|
2019-09-30 09:49:42 +00:00
|
|
|
boolean notify_summary = prefs.getBoolean("notify_summary", false);
|
2020-07-05 10:22:11 +00:00
|
|
|
boolean notify_preview = prefs.getBoolean("notify_preview", true);
|
|
|
|
boolean notify_preview_only = prefs.getBoolean("notify_preview_only", false);
|
2021-06-22 12:51:40 +00:00
|
|
|
boolean notify_screen_on = prefs.getBoolean("notify_screen_on", false);
|
2020-10-17 08:34:44 +00:00
|
|
|
boolean wearable_preview = prefs.getBoolean("wearable_preview", false);
|
2019-09-30 09:49:42 +00:00
|
|
|
boolean biometrics = prefs.getBoolean("biometrics", false);
|
2020-07-28 19:26:48 +00:00
|
|
|
String pin = prefs.getString("pin", null);
|
2021-03-18 10:33:59 +00:00
|
|
|
boolean biometric_notify = prefs.getBoolean("biometrics_notify", true);
|
2019-08-13 08:27:17 +00:00
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-03-30 07:43:36 +00:00
|
|
|
|
2020-07-28 19:26:48 +00:00
|
|
|
boolean redacted = ((biometrics || !TextUtils.isEmpty(pin)) && !biometric_notify);
|
|
|
|
if (redacted)
|
2019-09-30 09:49:42 +00:00
|
|
|
notify_summary = true;
|
|
|
|
|
|
|
|
Log.i("Notify messages=" + messages.size() +
|
|
|
|
" biometrics=" + biometrics + "/" + biometric_notify +
|
|
|
|
" summary=" + notify_summary);
|
|
|
|
|
2021-02-15 09:47:03 +00:00
|
|
|
Map<Long, Integer> newMessages = new HashMap<>();
|
|
|
|
|
2019-09-01 09:32:18 +00:00
|
|
|
Map<Long, List<TupleMessageEx>> groupMessages = new HashMap<>();
|
2021-02-15 09:47:03 +00:00
|
|
|
for (long group : data.groupNotifying.keySet())
|
2019-08-23 16:43:18 +00:00
|
|
|
groupMessages.put(group, new ArrayList<>());
|
|
|
|
|
|
|
|
// Current
|
2019-02-27 18:29:58 +00:00
|
|
|
for (TupleMessageEx message : messages) {
|
2021-01-27 14:48:49 +00:00
|
|
|
if (message.notifying == EntityMessage.NOTIFYING_IGNORE) {
|
|
|
|
Log.e("Notify ignore");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-17 08:23:51 +00:00
|
|
|
// Check if notification channel enabled
|
2019-11-12 09:57:26 +00:00
|
|
|
if (message.notifying == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && pro) {
|
|
|
|
String channelId = message.getNotificationChannelId();
|
|
|
|
if (channelId != null) {
|
|
|
|
NotificationChannel channel = nm.getNotificationChannel(channelId);
|
2020-07-25 17:48:31 +00:00
|
|
|
if (channel != null && channel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
|
2021-06-08 14:21:04 +00:00
|
|
|
db.message().setMessageUiIgnored(message.id, true);
|
2020-07-25 17:48:31 +00:00
|
|
|
Log.i("Notify disabled=" + message.id + " channel=" + channelId);
|
2019-11-12 09:57:26 +00:00
|
|
|
continue;
|
2020-07-25 17:48:31 +00:00
|
|
|
}
|
2019-11-12 09:57:26 +00:00
|
|
|
}
|
2019-04-17 08:23:51 +00:00
|
|
|
}
|
2019-03-07 12:29:03 +00:00
|
|
|
|
2020-07-05 10:22:11 +00:00
|
|
|
if (notify_preview && notify_preview_only && !message.content)
|
|
|
|
continue;
|
|
|
|
|
2020-08-16 17:10:10 +00:00
|
|
|
if (foreground && notify_background_only && message.notifying == 0) {
|
|
|
|
Log.i("Notify foreground=" + message.id);
|
|
|
|
if (!message.ui_ignored)
|
|
|
|
db.message().setMessageUiIgnored(message.id, true);
|
2020-07-25 17:54:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-09-01 09:32:18 +00:00
|
|
|
long group = (pro && message.accountNotify ? message.account : 0);
|
2020-06-15 10:34:25 +00:00
|
|
|
if (!message.folderUnified)
|
|
|
|
group = -message.folder;
|
2021-02-15 09:47:03 +00:00
|
|
|
if (!data.groupNotifying.containsKey(group))
|
2021-02-15 10:56:37 +00:00
|
|
|
data.groupNotifying.put(group, new ArrayList<>());
|
2019-09-18 09:11:49 +00:00
|
|
|
if (!groupMessages.containsKey(group))
|
2021-02-15 10:56:37 +00:00
|
|
|
groupMessages.put(group, new ArrayList<>());
|
2019-03-07 12:29:03 +00:00
|
|
|
|
2021-06-19 16:39:42 +00:00
|
|
|
if (message.notifying == 0) {
|
|
|
|
// Handle clear notifying on boot/update
|
|
|
|
data.groupNotifying.get(group).remove(message.id);
|
|
|
|
data.groupNotifying.get(group).remove(-message.id);
|
|
|
|
} else {
|
2019-08-24 07:06:57 +00:00
|
|
|
long id = message.id * message.notifying;
|
2021-02-15 09:47:03 +00:00
|
|
|
if (!data.groupNotifying.get(group).contains(id) &&
|
|
|
|
!data.groupNotifying.get(group).contains(-id)) {
|
2019-09-18 19:36:10 +00:00
|
|
|
Log.i("Notify database=" + id);
|
2021-02-15 09:47:03 +00:00
|
|
|
data.groupNotifying.get(group).add(id);
|
2019-09-18 19:36:10 +00:00
|
|
|
}
|
2019-08-24 07:06:57 +00:00
|
|
|
}
|
2019-07-06 08:16:42 +00:00
|
|
|
|
2021-02-15 10:56:37 +00:00
|
|
|
if (!(message.ui_seen || message.ui_ignored || message.ui_hide)) {
|
2021-02-15 09:47:03 +00:00
|
|
|
Integer current = newMessages.get(group);
|
|
|
|
newMessages.put(group, current == null ? 1 : current + 1);
|
|
|
|
|
2021-02-15 10:56:37 +00:00
|
|
|
// This assumes the messages are properly ordered
|
2021-02-14 19:41:17 +00:00
|
|
|
if (groupMessages.get(group).size() < MAX_NOTIFICATION_COUNT)
|
|
|
|
groupMessages.get(group).add(message);
|
2021-02-15 09:47:03 +00:00
|
|
|
else
|
|
|
|
db.message().setMessageUiIgnored(message.id, true);
|
2019-07-06 08:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 18:29:58 +00:00
|
|
|
// Difference
|
2021-06-22 18:06:06 +00:00
|
|
|
boolean flash = false;
|
2019-09-01 09:32:18 +00:00
|
|
|
for (long group : groupMessages.keySet()) {
|
2019-09-30 09:49:42 +00:00
|
|
|
List<Long> add = new ArrayList<>();
|
2019-10-17 15:14:46 +00:00
|
|
|
List<Long> update = new ArrayList<>();
|
2021-02-15 09:47:03 +00:00
|
|
|
List<Long> remove = new ArrayList<>(data.groupNotifying.get(group));
|
2021-02-15 07:58:05 +00:00
|
|
|
for (int m = 0; m < groupMessages.get(group).size(); m++) {
|
2021-02-14 20:14:59 +00:00
|
|
|
TupleMessageEx message = groupMessages.get(group).get(m);
|
2021-02-15 07:58:05 +00:00
|
|
|
if (m >= MAX_NOTIFICATION_DISPLAY) {
|
2021-02-15 10:56:37 +00:00
|
|
|
// This is to prevent notification sounds when shifting messages up
|
2021-02-24 11:41:10 +00:00
|
|
|
if (!message.ui_silent) {
|
|
|
|
Log.i("Notify silence=" + message.id);
|
2021-02-15 10:56:37 +00:00
|
|
|
db.message().setMessageUiSilent(message.id, true);
|
2021-02-24 11:41:10 +00:00
|
|
|
}
|
2021-02-15 07:58:05 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-08-05 14:38:17 +00:00
|
|
|
long id = (message.content ? message.id : -message.id);
|
|
|
|
if (remove.contains(id)) {
|
|
|
|
remove.remove(id);
|
|
|
|
Log.i("Notify existing=" + id);
|
|
|
|
} else {
|
2019-09-30 09:49:42 +00:00
|
|
|
boolean existing = remove.contains(-id);
|
2019-10-17 15:14:46 +00:00
|
|
|
if (existing) {
|
2020-07-05 10:47:10 +00:00
|
|
|
if (message.content && notify_preview) {
|
|
|
|
Log.i("Notify preview=" + id);
|
|
|
|
add.add(id);
|
|
|
|
update.add(id);
|
|
|
|
}
|
2019-09-30 09:49:42 +00:00
|
|
|
remove.remove(-id);
|
2021-06-22 18:06:06 +00:00
|
|
|
} else {
|
|
|
|
flash = true;
|
2020-07-05 10:47:10 +00:00
|
|
|
add.add(id);
|
2021-06-22 18:06:06 +00:00
|
|
|
}
|
2019-09-30 09:49:42 +00:00
|
|
|
Log.i("Notify adding=" + id + " existing=" + existing);
|
2019-08-05 14:38:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-06 08:16:42 +00:00
|
|
|
|
2021-03-01 18:24:39 +00:00
|
|
|
Integer prev = prefs.getInt("new_messages." + group, 0);
|
2021-02-15 09:47:03 +00:00
|
|
|
Integer current = newMessages.get(group);
|
|
|
|
if (current == null)
|
|
|
|
current = 0;
|
2021-03-01 18:24:39 +00:00
|
|
|
prefs.edit().putInt("new_messages." + group, current).apply();
|
2019-10-17 15:14:46 +00:00
|
|
|
|
2021-02-15 09:47:03 +00:00
|
|
|
if (prev.equals(current) &&
|
|
|
|
remove.size() + add.size() == 0) {
|
2019-08-05 14:38:17 +00:00
|
|
|
Log.i("Notify unchanged");
|
|
|
|
continue;
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 14:38:17 +00:00
|
|
|
// Build notifications
|
2019-10-17 15:14:46 +00:00
|
|
|
List<NotificationCompat.Builder> notifications = getNotificationUnseen(context,
|
2019-09-30 09:49:42 +00:00
|
|
|
group, groupMessages.get(group),
|
2021-04-08 06:03:06 +00:00
|
|
|
notify_summary, current - prev, current,
|
2020-07-28 19:26:48 +00:00
|
|
|
redacted);
|
2019-08-05 14:38:17 +00:00
|
|
|
|
2021-02-15 10:56:37 +00:00
|
|
|
Log.i("Notify group=" + group +
|
|
|
|
" new=" + prev + "/" + current +
|
|
|
|
" count=" + notifications.size() +
|
|
|
|
" add=" + add.size() +
|
|
|
|
" update=" + update.size() +
|
|
|
|
" remove=" + remove.size());
|
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);
|
2021-05-05 16:57:33 +00:00
|
|
|
EntityLog.log(context, "Notify cancel tag=" + tag + " id=" + id);
|
2021-07-15 16:36:39 +00:00
|
|
|
nm.cancel(tag, NotificationHelper.NOTIFICATION_TAGGED);
|
2019-09-18 16:14:39 +00:00
|
|
|
|
2021-02-15 09:47:03 +00:00
|
|
|
data.groupNotifying.get(group).remove(id);
|
2019-09-18 16:14:39 +00:00
|
|
|
db.message().setMessageNotifying(Math.abs(id), 0);
|
2019-05-07 18:47:40 +00:00
|
|
|
}
|
2019-02-27 18:29:58 +00:00
|
|
|
|
2021-07-15 13:03:49 +00:00
|
|
|
if (notifications.size() == 0) {
|
|
|
|
String tag = "unseen." + group + "." + 0;
|
|
|
|
EntityLog.log(context, "Notify cancel tag=" + tag);
|
2021-07-15 16:36:39 +00:00
|
|
|
nm.cancel(tag, NotificationHelper.NOTIFICATION_TAGGED);
|
2021-07-15 13:03:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 09:49:42 +00:00
|
|
|
for (Long id : add) {
|
2021-02-15 09:47:03 +00:00
|
|
|
data.groupNotifying.get(group).add(id);
|
|
|
|
data.groupNotifying.get(group).remove(-id);
|
2019-09-30 09:49:42 +00:00
|
|
|
db.message().setMessageNotifying(Math.abs(id), (int) Math.signum(id));
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:14:46 +00:00
|
|
|
for (NotificationCompat.Builder builder : notifications) {
|
|
|
|
long id = builder.getExtras().getLong("id", 0);
|
2021-02-15 10:56:37 +00:00
|
|
|
if ((id == 0 && !prev.equals(current)) || add.contains(id)) {
|
2020-04-03 18:55:55 +00:00
|
|
|
// https://developer.android.com/training/wearables/notifications/creating
|
2019-10-21 14:30:42 +00:00
|
|
|
if (id == 0) {
|
|
|
|
if (!notify_summary)
|
|
|
|
builder.setLocalOnly(true);
|
|
|
|
} else {
|
|
|
|
if (wearable_preview ? id < 0 : update.contains(id))
|
|
|
|
builder.setLocalOnly(true);
|
|
|
|
}
|
2019-10-21 08:34:16 +00:00
|
|
|
|
2019-07-10 16:33:25 +00:00
|
|
|
String tag = "unseen." + group + "." + Math.abs(id);
|
2019-10-17 15:14:46 +00:00
|
|
|
Notification notification = builder.build();
|
2020-10-23 14:11:22 +00:00
|
|
|
EntityLog.log(context, "Notifying tag=" + tag +
|
|
|
|
" id=" + id + " group=" + notification.getGroup() +
|
|
|
|
(Build.VERSION.SDK_INT < Build.VERSION_CODES.O
|
|
|
|
? " sdk=" + Build.VERSION.SDK_INT
|
2020-10-23 15:17:48 +00:00
|
|
|
: " channel=" + notification.getChannelId()) +
|
|
|
|
" sort=" + notification.getSortKey());
|
2020-02-22 21:09:46 +00:00
|
|
|
try {
|
2021-07-15 16:36:39 +00:00
|
|
|
nm.notify(tag, NotificationHelper.NOTIFICATION_TAGGED, notification);
|
2021-05-28 18:22:47 +00:00
|
|
|
// https://github.com/leolin310148/ShortcutBadger/wiki/Xiaomi-Device-Support
|
2021-05-29 13:36:22 +00:00
|
|
|
if (id == 0 && badge && Helper.isXiaomi())
|
2021-05-28 18:22:47 +00:00
|
|
|
ShortcutBadger.applyNotification(context, notification, current);
|
2020-02-22 21:09:46 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
2019-08-04 13:21:21 +00:00
|
|
|
}
|
2019-07-06 08:16:42 +00:00
|
|
|
}
|
2021-06-22 18:06:06 +00:00
|
|
|
}
|
2021-06-22 12:51:40 +00:00
|
|
|
|
2021-06-22 18:06:06 +00:00
|
|
|
if (notify_screen_on && flash) {
|
|
|
|
Log.i("Notify screen on");
|
|
|
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
PowerManager.WakeLock wakeLock = pm.newWakeLock(
|
|
|
|
PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP,
|
|
|
|
BuildConfig.APPLICATION_ID + ":notification");
|
|
|
|
wakeLock.acquire(SCREEN_ON_DURATION);
|
2019-02-27 18:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 15:14:46 +00:00
|
|
|
private static List<NotificationCompat.Builder> getNotificationUnseen(
|
2019-09-30 09:49:42 +00:00
|
|
|
Context context,
|
|
|
|
long group, List<TupleMessageEx> messages,
|
2021-04-08 06:03:06 +00:00
|
|
|
boolean notify_summary, int new_messages, int total_messages, boolean redacted) {
|
2019-10-17 15:14:46 +00:00
|
|
|
List<NotificationCompat.Builder> 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-10-02 09:16:35 +00:00
|
|
|
// Android 7+ N https://android-developers.googleblog.com/2016/06/notifications-in-android-n.html
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2020-06-15 15:08:01 +00:00
|
|
|
// Group
|
|
|
|
// < 0: folder
|
|
|
|
// = 0: unified
|
|
|
|
// > 0: account
|
|
|
|
|
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;
|
|
|
|
|
2019-08-13 08:27:17 +00:00
|
|
|
boolean pro = ActivityBilling.isPro(context);
|
2019-05-07 17:28:46 +00:00
|
|
|
|
2019-02-27 17:33:46 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
2020-10-23 15:17:48 +00:00
|
|
|
boolean notify_newest_first = prefs.getBoolean("notify_newest_first", false);
|
2021-06-06 10:10:43 +00:00
|
|
|
MessageHelper.AddressFormat email_format = MessageHelper.getAddressFormat(context);
|
2020-02-24 09:11:05 +00:00
|
|
|
boolean prefer_contact = prefs.getBoolean("prefer_contact", false);
|
2019-05-06 08:33:29 +00:00
|
|
|
boolean flags = prefs.getBoolean("flags", true);
|
2020-10-24 10:47:09 +00:00
|
|
|
boolean notify_messaging = prefs.getBoolean("notify_messaging", false);
|
2021-06-05 14:43:11 +00:00
|
|
|
boolean notify_subtext = prefs.getBoolean("notify_subtext", true);
|
2019-05-07 17:28:46 +00:00
|
|
|
boolean notify_preview = prefs.getBoolean("notify_preview", true);
|
2020-02-11 18:55:03 +00:00
|
|
|
boolean notify_preview_all = prefs.getBoolean("notify_preview_all", false);
|
2020-10-17 08:34:44 +00:00
|
|
|
boolean wearable_preview = prefs.getBoolean("wearable_preview", false);
|
2019-06-28 11:08:04 +00:00
|
|
|
boolean notify_trash = (prefs.getBoolean("notify_trash", true) || !pro);
|
2019-09-27 09:39:35 +00:00
|
|
|
boolean notify_junk = (prefs.getBoolean("notify_junk", false) && pro);
|
2019-06-28 11:08:04 +00:00
|
|
|
boolean notify_archive = (prefs.getBoolean("notify_archive", true) || !pro);
|
2019-10-23 10:51:20 +00:00
|
|
|
boolean notify_move = (prefs.getBoolean("notify_move", false) && pro);
|
2019-06-28 11:08:04 +00:00
|
|
|
boolean notify_reply = (prefs.getBoolean("notify_reply", false) && pro);
|
2019-08-24 13:00:14 +00:00
|
|
|
boolean notify_reply_direct = (prefs.getBoolean("notify_reply_direct", 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);
|
2021-04-12 12:59:38 +00:00
|
|
|
boolean notify_hide = (prefs.getBoolean("notify_hide", false) && pro);
|
2020-01-06 08:21:38 +00:00
|
|
|
boolean notify_snooze = (prefs.getBoolean("notify_snooze", false) && pro);
|
2019-10-02 09:32:00 +00:00
|
|
|
boolean notify_remove = prefs.getBoolean("notify_remove", true);
|
2019-07-09 20:44:02 +00:00
|
|
|
boolean light = prefs.getBoolean("light", false);
|
|
|
|
String sound = prefs.getString("sound", null);
|
2019-09-14 08:50:47 +00:00
|
|
|
boolean alert_once = prefs.getBoolean("alert_once", true);
|
2019-05-05 20:28:07 +00:00
|
|
|
|
2019-02-27 17:33:46 +00:00
|
|
|
// Get contact info
|
2020-02-24 09:11:05 +00:00
|
|
|
Map<Long, Address[]> messageFrom = new HashMap<>();
|
|
|
|
Map<Long, ContactInfo[]> messageInfo = new HashMap<>();
|
2021-02-14 20:14:59 +00:00
|
|
|
for (int m = 0; m < messages.size() && m < MAX_NOTIFICATION_DISPLAY; m++) {
|
|
|
|
TupleMessageEx message = messages.get(m);
|
2021-07-16 18:03:27 +00:00
|
|
|
ContactInfo[] info = ContactInfo.get(context,
|
|
|
|
message.account, message.folderType,
|
|
|
|
message.bimi_selector, message.from);
|
2020-02-24 09:11:05 +00:00
|
|
|
|
|
|
|
Address[] modified = (message.from == null
|
|
|
|
? new InternetAddress[0]
|
|
|
|
: Arrays.copyOf(message.from, message.from.length));
|
|
|
|
for (int i = 0; i < modified.length; i++) {
|
|
|
|
String displayName = info[i].getDisplayName();
|
|
|
|
if (!TextUtils.isEmpty(displayName)) {
|
|
|
|
String email = ((InternetAddress) modified[i]).getAddress();
|
|
|
|
String personal = ((InternetAddress) modified[i]).getPersonal();
|
|
|
|
if (TextUtils.isEmpty(personal) || prefer_contact)
|
|
|
|
try {
|
|
|
|
modified[i] = new InternetAddress(email, displayName, StandardCharsets.UTF_8.name());
|
|
|
|
} catch (UnsupportedEncodingException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
messageInfo.put(message.id, info);
|
|
|
|
messageFrom.put(message.id, modified);
|
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
// Summary notification
|
2019-09-30 09:49:42 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || notify_summary) {
|
2019-07-15 18:39:24 +00:00
|
|
|
// Build pending intents
|
2020-06-15 10:34:25 +00:00
|
|
|
Intent content;
|
2020-06-15 14:43:29 +00:00
|
|
|
if (group < 0) {
|
2020-06-15 10:34:25 +00:00
|
|
|
content = new Intent(context, ActivityView.class)
|
|
|
|
.setAction("folder:" + (-group) + (notify_remove ? ":" + group : ""));
|
2020-06-15 14:43:29 +00:00
|
|
|
if (messages.size() > 0)
|
|
|
|
content.putExtra("type", messages.get(0).folderType);
|
|
|
|
} else
|
2020-06-15 10:34:25 +00:00
|
|
|
content = new Intent(context, ActivityView.class)
|
|
|
|
.setAction("unified" + (notify_remove ? ":" + group : ""));
|
|
|
|
content.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piContent = PendingIntentCompat.getActivity(
|
2021-05-22 14:54:17 +00:00
|
|
|
context, ActivityView.PI_UNIFIED, content, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-09-01 09:32:18 +00:00
|
|
|
Intent clear = new Intent(context, ServiceUI.class).setAction("clear:" + group);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piClear = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_CLEAR, clear, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-07-15 18:39:24 +00:00
|
|
|
|
|
|
|
// Build title
|
|
|
|
String title = context.getResources().getQuantityString(
|
2021-04-08 06:03:06 +00:00
|
|
|
R.plurals.title_notification_unseen, total_messages, total_messages);
|
2019-07-15 18:39:24 +00:00
|
|
|
|
2020-06-15 10:34:25 +00:00
|
|
|
long cgroup = (group >= 0
|
|
|
|
? group
|
|
|
|
: (pro && messages.size() > 0 && messages.get(0).accountNotify ? messages.get(0).account : 0));
|
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
// Build notification
|
|
|
|
NotificationCompat.Builder builder =
|
2020-06-15 10:34:25 +00:00
|
|
|
new NotificationCompat.Builder(context, EntityAccount.getNotificationChannelId(cgroup))
|
2019-11-12 17:52:42 +00:00
|
|
|
.setSmallIcon(messages.size() > 1
|
|
|
|
? R.drawable.baseline_mail_more_white_24
|
|
|
|
: R.drawable.baseline_mail_white_24)
|
2019-07-15 18:39:24 +00:00
|
|
|
.setContentTitle(title)
|
2020-06-15 10:34:25 +00:00
|
|
|
.setContentIntent(piContent)
|
2021-04-08 06:03:06 +00:00
|
|
|
.setNumber(total_messages)
|
2019-07-15 18:39:24 +00:00
|
|
|
.setDeleteIntent(piClear)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
2020-01-10 15:38:38 +00:00
|
|
|
.setCategory(notify_summary
|
|
|
|
? NotificationCompat.CATEGORY_EMAIL : NotificationCompat.CATEGORY_STATUS)
|
2019-10-22 16:45:21 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
|
|
|
|
.setAllowSystemGeneratedContextualActions(false);
|
2019-09-30 09:49:42 +00:00
|
|
|
|
|
|
|
if (notify_summary) {
|
2021-02-15 09:47:03 +00:00
|
|
|
builder.setOnlyAlertOnce(new_messages <= 0);
|
2019-09-30 09:49:42 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
if (new_messages > 0)
|
|
|
|
setLightAndSound(builder, light, sound);
|
|
|
|
else
|
|
|
|
builder.setSound(null);
|
|
|
|
} else {
|
|
|
|
builder
|
|
|
|
.setGroup(Long.toString(group))
|
|
|
|
.setGroupSummary(true)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
builder.setSound(null);
|
|
|
|
}
|
2019-07-15 18:39:24 +00:00
|
|
|
|
2021-07-14 08:26:40 +00:00
|
|
|
if (pro) {
|
|
|
|
Integer color = null;
|
|
|
|
for (TupleMessageEx message : messages) {
|
|
|
|
Integer mcolor = getColor(message);
|
|
|
|
if (mcolor == null) {
|
|
|
|
color = null;
|
|
|
|
break;
|
|
|
|
} else if (color == null)
|
|
|
|
color = mcolor;
|
|
|
|
else if (!color.equals(mcolor)) {
|
|
|
|
color = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (color != null) {
|
2020-06-15 10:34:25 +00:00
|
|
|
builder.setColor(color);
|
2019-09-01 14:05:42 +00:00
|
|
|
builder.setColorized(true);
|
|
|
|
}
|
2019-09-01 11:00:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 08:26:40 +00:00
|
|
|
// Subtext should not be set, to show number of new messages
|
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
Notification pub = builder.build();
|
|
|
|
builder
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
|
|
|
.setPublicVersion(pub);
|
2019-05-07 17:28:46 +00:00
|
|
|
|
2019-10-03 15:02:26 +00:00
|
|
|
if (notify_preview)
|
|
|
|
if (redacted)
|
2020-07-28 19:26:48 +00:00
|
|
|
builder.setContentText(context.getString(R.string.title_notification_redacted));
|
2019-10-03 15:02:26 +00:00
|
|
|
else {
|
|
|
|
DateFormat DTF = Helper.getDateTimeInstance(context, SimpleDateFormat.SHORT, SimpleDateFormat.SHORT);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (EntityMessage message : messages) {
|
2020-02-24 09:11:05 +00:00
|
|
|
Address[] afrom = messageFrom.get(message.id);
|
2021-06-06 10:10:43 +00:00
|
|
|
String from = MessageHelper.formatAddresses(afrom, email_format, false);
|
2020-11-08 11:47:03 +00:00
|
|
|
sb.append("<strong>").append(Html.escapeHtml(from)).append("</strong>");
|
2019-10-03 15:02:26 +00:00
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
2020-11-08 11:47:03 +00:00
|
|
|
sb.append(": ").append(Html.escapeHtml(message.subject));
|
|
|
|
sb.append(" ").append(Html.escapeHtml(DTF.format(message.received)));
|
2019-10-03 15:02:26 +00:00
|
|
|
sb.append("<br>");
|
|
|
|
}
|
2019-07-15 18:39:24 +00:00
|
|
|
|
2019-10-21 14:30:42 +00:00
|
|
|
// Wearables
|
2019-10-05 11:22:33 +00:00
|
|
|
builder.setContentText(title);
|
2019-10-02 20:13:31 +00:00
|
|
|
|
2019-10-21 14:30:42 +00:00
|
|
|
// Device
|
2019-10-03 15:02:26 +00:00
|
|
|
builder.setStyle(new NotificationCompat.BigTextStyle()
|
2020-11-08 20:09:31 +00:00
|
|
|
.bigText(HtmlHelper.fromHtml(sb.toString(), context))
|
2019-10-03 15:02:26 +00:00
|
|
|
.setSummaryText(title));
|
|
|
|
}
|
2019-07-12 09:11:01 +00:00
|
|
|
|
2019-10-17 15:14:46 +00:00
|
|
|
notifications.add(builder);
|
2019-07-12 09:11:01 +00:00
|
|
|
}
|
|
|
|
|
2019-09-30 09:49:42 +00:00
|
|
|
if (notify_summary)
|
|
|
|
return notifications;
|
|
|
|
|
2019-05-07 17:28:46 +00:00
|
|
|
// Message notifications
|
2021-02-14 20:14:59 +00:00
|
|
|
for (int m = 0; m < messages.size() && m < MAX_NOTIFICATION_DISPLAY; m++) {
|
|
|
|
TupleMessageEx message = messages.get(m);
|
2020-02-24 09:11:05 +00:00
|
|
|
ContactInfo[] info = messageInfo.get(message.id);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build arguments
|
2019-10-21 14:56:55 +00:00
|
|
|
long id = (message.content ? message.id : -message.id);
|
2019-02-27 17:33:46 +00:00
|
|
|
Bundle args = new Bundle();
|
2019-10-21 14:56:55 +00:00
|
|
|
args.putLong("id", id);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-03-07 12:29:03 +00:00
|
|
|
// Build pending intents
|
2021-07-15 11:38:19 +00:00
|
|
|
Intent thread = new Intent(context, ActivityView.class);
|
|
|
|
thread.setAction("thread:" + message.id);
|
|
|
|
thread.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
thread.putExtra("account", message.account);
|
|
|
|
thread.putExtra("folder", message.folder);
|
|
|
|
thread.putExtra("thread", message.thread);
|
|
|
|
thread.putExtra("filter_archive", !EntityFolder.ARCHIVE.equals(message.folderType));
|
|
|
|
thread.putExtra("ignore", notify_remove);
|
|
|
|
PendingIntent piContent = PendingIntentCompat.getActivity(
|
|
|
|
context, ActivityView.PI_THREAD, thread, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2019-05-05 20:28:07 +00:00
|
|
|
Intent ignore = new Intent(context, ServiceUI.class).setAction("ignore:" + message.id);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piIgnore = PendingIntentCompat.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
|
2019-11-12 09:57:26 +00:00
|
|
|
String channelName = EntityAccount.getNotificationChannelId(0);
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && pro) {
|
2019-05-16 08:04:20 +00:00
|
|
|
NotificationChannel channel = null;
|
|
|
|
|
2019-11-12 09:57:26 +00:00
|
|
|
String channelId = message.getNotificationChannelId();
|
|
|
|
if (channelId != null)
|
|
|
|
channel = nm.getNotificationChannel(channelId);
|
2019-05-16 08:04:20 +00:00
|
|
|
|
|
|
|
if (channel == null)
|
|
|
|
channel = nm.getNotificationChannel(EntityFolder.getNotificationChannelId(message.folder));
|
|
|
|
|
2019-11-12 09:57:26 +00:00
|
|
|
if (channel == null) {
|
|
|
|
if (message.accountNotify)
|
|
|
|
channelName = EntityAccount.getNotificationChannelId(message.account);
|
|
|
|
} else
|
2019-03-07 12:29:03 +00:00
|
|
|
channelName = channel.getId();
|
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2020-10-23 15:17:48 +00:00
|
|
|
String sortKey = String.format(Locale.ROOT, "%13d",
|
|
|
|
notify_newest_first ? (10000000000000L - message.received) : message.received);
|
|
|
|
|
2019-06-23 08:23:49 +00:00
|
|
|
NotificationCompat.Builder mbuilder =
|
|
|
|
new NotificationCompat.Builder(context, channelName)
|
|
|
|
.addExtras(args)
|
2019-11-12 16:35:59 +00:00
|
|
|
.setSmallIcon(R.drawable.baseline_mail_white_24)
|
2019-06-23 08:23:49 +00:00
|
|
|
.setContentIntent(piContent)
|
|
|
|
.setWhen(message.received)
|
2020-10-23 14:50:15 +00:00
|
|
|
.setShowWhen(true)
|
2020-10-23 15:17:48 +00:00
|
|
|
.setSortKey(sortKey)
|
2019-06-23 08:23:49 +00:00
|
|
|
.setDeleteIntent(piIgnore)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
2019-07-23 09:38:36 +00:00
|
|
|
.setCategory(NotificationCompat.CATEGORY_EMAIL)
|
2020-01-23 19:52:58 +00:00
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
|
2019-09-14 15:39:08 +00:00
|
|
|
.setOnlyAlertOnce(alert_once)
|
|
|
|
.setAllowSystemGeneratedContextualActions(false);
|
2019-09-08 14:11:43 +00:00
|
|
|
|
2021-02-15 07:58:05 +00:00
|
|
|
if (message.ui_silent) {
|
|
|
|
mbuilder.setSilent(true);
|
|
|
|
Log.i("Notify silent=" + message.id);
|
|
|
|
}
|
|
|
|
|
2020-10-24 10:47:09 +00:00
|
|
|
if (notify_messaging) {
|
|
|
|
// https://developer.android.com/training/cars/messaging
|
2021-06-06 10:10:43 +00:00
|
|
|
String meName = MessageHelper.formatAddresses(message.to, email_format, false);
|
|
|
|
String youName = MessageHelper.formatAddresses(message.from, email_format, false);
|
2020-11-11 09:46:53 +00:00
|
|
|
|
|
|
|
// Names cannot be empty
|
|
|
|
if (TextUtils.isEmpty(meName))
|
|
|
|
meName = "-";
|
|
|
|
if (TextUtils.isEmpty(youName))
|
|
|
|
youName = "-";
|
|
|
|
|
|
|
|
Person.Builder me = new Person.Builder().setName(meName);
|
|
|
|
Person.Builder you = new Person.Builder().setName(youName);
|
2020-10-24 12:31:01 +00:00
|
|
|
|
|
|
|
if (info[0].hasPhoto())
|
|
|
|
you.setIcon(IconCompat.createWithBitmap(info[0].getPhotoBitmap()));
|
|
|
|
|
|
|
|
if (info[0].hasLookupUri())
|
|
|
|
you.setUri(info[0].getLookupUri().toString());
|
|
|
|
|
|
|
|
NotificationCompat.MessagingStyle messagingStyle = new NotificationCompat.MessagingStyle(me.build());
|
2020-10-24 10:47:09 +00:00
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
messagingStyle.setConversationTitle(message.subject);
|
2020-10-24 12:31:01 +00:00
|
|
|
|
|
|
|
messagingStyle.addMessage(
|
|
|
|
notify_preview && message.preview != null ? message.preview : "",
|
|
|
|
message.received,
|
|
|
|
you.build());
|
|
|
|
|
|
|
|
mbuilder.setStyle(messagingStyle);
|
2020-10-24 10:47:09 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 18:39:24 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
|
|
mbuilder
|
2019-09-01 09:32:18 +00:00
|
|
|
.setGroup(Long.toString(group))
|
2019-07-15 18:39:24 +00:00
|
|
|
.setGroupSummary(false)
|
|
|
|
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_CHILDREN);
|
|
|
|
|
2019-09-30 09:49:42 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
setLightAndSound(mbuilder, light, sound);
|
2019-07-15 18:39:24 +00:00
|
|
|
|
2020-02-24 09:11:05 +00:00
|
|
|
Address[] afrom = messageFrom.get(message.id);
|
2021-06-06 10:10:43 +00:00
|
|
|
String from = MessageHelper.formatAddresses(afrom, email_format, false);
|
2020-06-15 10:34:25 +00:00
|
|
|
mbuilder.setContentTitle(from);
|
2021-06-05 14:43:11 +00:00
|
|
|
if (notify_subtext)
|
2021-07-01 18:06:11 +00:00
|
|
|
if (message.folderUnified && EntityFolder.INBOX.equals(message.folderType))
|
2021-06-05 14:43:11 +00:00
|
|
|
mbuilder.setSubText(message.accountName);
|
2021-07-01 18:06:11 +00:00
|
|
|
else
|
|
|
|
mbuilder.setSubText(message.accountName + " - " + message.getFolderName(context));
|
2019-07-11 08:33:51 +00:00
|
|
|
|
2019-08-05 13:48:04 +00:00
|
|
|
DB db = DB.getInstance(context);
|
|
|
|
|
2019-10-01 14:47:18 +00:00
|
|
|
List<NotificationCompat.Action> wactions = new ArrayList<>();
|
|
|
|
|
2019-08-05 13:48:04 +00:00
|
|
|
if (notify_trash &&
|
2020-04-08 14:24:04 +00:00
|
|
|
message.accountProtocol == EntityAccount.TYPE_IMAP &&
|
2019-08-05 13:48:04 +00:00
|
|
|
db.folder().getFolderByType(message.account, EntityFolder.TRASH) != null) {
|
2019-05-21 13:55:10 +00:00
|
|
|
Intent trash = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("trash:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piTrash = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_TRASH, trash, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-05-05 20:28:07 +00:00
|
|
|
NotificationCompat.Action.Builder actionTrash = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_delete_24,
|
2019-05-05 20:28:07 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_trash),
|
2019-10-01 09:58:42 +00:00
|
|
|
piTrash)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_DELETE)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-05-05 20:28:07 +00:00
|
|
|
mbuilder.addAction(actionTrash.build());
|
2019-10-01 14:47:18 +00:00
|
|
|
|
|
|
|
wactions.add(actionTrash.build());
|
2019-05-05 20:28:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 19:28:05 +00:00
|
|
|
if (notify_junk &&
|
2020-04-08 14:24:04 +00:00
|
|
|
message.accountProtocol == EntityAccount.TYPE_IMAP &&
|
2019-09-26 19:28:05 +00:00
|
|
|
db.folder().getFolderByType(message.account, EntityFolder.JUNK) != null) {
|
|
|
|
Intent junk = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("junk:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piJunk = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_JUNK, junk, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-09-26 19:28:05 +00:00
|
|
|
NotificationCompat.Action.Builder actionJunk = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_report_problem_24,
|
2019-09-26 19:28:05 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_junk),
|
2019-10-01 09:58:42 +00:00
|
|
|
piJunk)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-09-26 19:28:05 +00:00
|
|
|
mbuilder.addAction(actionJunk.build());
|
2019-10-01 14:47:18 +00:00
|
|
|
|
|
|
|
wactions.add(actionJunk.build());
|
2019-09-26 19:28:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 13:48:04 +00:00
|
|
|
if (notify_archive &&
|
2020-04-08 14:24:04 +00:00
|
|
|
message.accountProtocol == EntityAccount.TYPE_IMAP &&
|
2019-08-05 13:48:04 +00:00
|
|
|
db.folder().getFolderByType(message.account, EntityFolder.ARCHIVE) != null) {
|
2019-05-21 13:55:10 +00:00
|
|
|
Intent archive = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("archive:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piArchive = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_ARCHIVE, archive, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-05-05 20:28:07 +00:00
|
|
|
NotificationCompat.Action.Builder actionArchive = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_archive_24,
|
2019-05-05 20:28:07 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_archive),
|
2019-10-01 09:58:42 +00:00
|
|
|
piArchive)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_ARCHIVE)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-05-05 20:28:07 +00:00
|
|
|
mbuilder.addAction(actionArchive.build());
|
2019-10-01 14:47:18 +00:00
|
|
|
|
|
|
|
wactions.add(actionArchive.build());
|
2019-05-05 20:28:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 14:24:04 +00:00
|
|
|
if (notify_move &&
|
|
|
|
message.accountProtocol == EntityAccount.TYPE_IMAP) {
|
2019-10-23 10:51:20 +00:00
|
|
|
EntityAccount account = db.account().getAccount(message.account);
|
|
|
|
if (account != null && account.move_to != null) {
|
|
|
|
EntityFolder folder = db.folder().getFolder(account.move_to);
|
|
|
|
if (folder != null) {
|
|
|
|
Intent move = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("move:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piMove = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_MOVE, move, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-10-23 10:51:20 +00:00
|
|
|
NotificationCompat.Action.Builder actionMove = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_folder_24,
|
2019-10-23 10:51:20 +00:00
|
|
|
folder.getDisplayName(context),
|
|
|
|
piMove)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-23 10:51:20 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
|
|
|
mbuilder.addAction(actionMove.build());
|
|
|
|
|
|
|
|
wactions.add(actionMove.build());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-21 06:39:45 +00:00
|
|
|
if (notify_reply && message.content) {
|
|
|
|
List<TupleIdentityEx> identities = db.identity().getComposableIdentities(message.account);
|
|
|
|
if (identities != null && identities.size() > 0) {
|
|
|
|
Intent reply = new Intent(context, ActivityCompose.class)
|
|
|
|
.putExtra("action", "reply")
|
|
|
|
.putExtra("reference", message.id)
|
|
|
|
.putExtra("group", group);
|
|
|
|
reply.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piReply = PendingIntentCompat.getActivity(
|
|
|
|
context, ActivityCompose.PI_REPLY, reply, PendingIntent.FLAG_UPDATE_CURRENT);
|
2021-03-21 06:39:45 +00:00
|
|
|
NotificationCompat.Action.Builder actionReply = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.twotone_reply_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_reply),
|
|
|
|
piReply)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
|
|
|
|
.setShowsUserInterface(true)
|
|
|
|
.setAllowGeneratedReplies(false);
|
|
|
|
mbuilder.addAction(actionReply.build());
|
|
|
|
}
|
2019-05-05 20:28:07 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:00:14 +00:00
|
|
|
if (notify_reply_direct &&
|
|
|
|
message.content &&
|
|
|
|
message.identity != null &&
|
|
|
|
message.from != null && message.from.length > 0 &&
|
|
|
|
db.folder().getOutbox() != null) {
|
|
|
|
Intent reply = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("reply:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2021-04-24 13:12:06 +00:00
|
|
|
PendingIntent piReply = PendingIntent.getService(
|
2021-03-27 17:54:55 +00:00
|
|
|
context, ServiceUI.PI_REPLY_DIRECT, reply, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-08-24 13:00:14 +00:00
|
|
|
NotificationCompat.Action.Builder actionReply = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_reply_24,
|
2019-08-24 13:00:14 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_reply_direct),
|
2019-10-01 09:58:42 +00:00
|
|
|
piReply)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-08-24 13:00:14 +00:00
|
|
|
RemoteInput.Builder input = new RemoteInput.Builder("text")
|
|
|
|
.setLabel(context.getString(R.string.title_advanced_notify_action_reply));
|
2020-04-24 15:43:10 +00:00
|
|
|
actionReply.addRemoteInput(input.build())
|
|
|
|
.setAllowGeneratedReplies(false);
|
2019-08-24 13:00:14 +00:00
|
|
|
mbuilder.addAction(actionReply.build());
|
2021-06-26 09:11:43 +00:00
|
|
|
|
|
|
|
wactions.add(actionReply.build());
|
2019-08-24 13:00:14 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piFlag = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_FLAG, flag, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-05-06 08:33:29 +00:00
|
|
|
NotificationCompat.Action.Builder actionFlag = new NotificationCompat.Action.Builder(
|
2020-09-29 10:57:28 +00:00
|
|
|
R.drawable.baseline_star_24,
|
2019-05-06 08:33:29 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_flag),
|
2019-10-01 09:58:42 +00:00
|
|
|
piFlag)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_THUMBS_UP)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-05-06 08:33:29 +00:00
|
|
|
mbuilder.addAction(actionFlag.build());
|
2019-10-01 14:47:18 +00:00
|
|
|
|
|
|
|
wactions.add(actionFlag.build());
|
2019-05-06 08:33:29 +00:00
|
|
|
}
|
|
|
|
|
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);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piSeen = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_SEEN, seen, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-05-05 20:28:07 +00:00
|
|
|
NotificationCompat.Action.Builder actionSeen = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_visibility_24,
|
2019-05-05 20:28:07 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_seen),
|
2019-10-01 09:58:42 +00:00
|
|
|
piSeen)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-05-05 20:28:07 +00:00
|
|
|
mbuilder.addAction(actionSeen.build());
|
2019-10-01 14:47:18 +00:00
|
|
|
|
|
|
|
wactions.add(actionSeen.build());
|
2019-05-05 20:28:07 +00:00
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2021-04-12 12:59:38 +00:00
|
|
|
if (notify_hide) {
|
|
|
|
Intent hide = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("hide:" + message.id)
|
|
|
|
.putExtra("group", group);
|
|
|
|
PendingIntent piHide = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_HIDE, hide, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
|
NotificationCompat.Action.Builder actionHide = new NotificationCompat.Action.Builder(
|
|
|
|
R.drawable.twotone_visibility_off_24,
|
|
|
|
context.getString(R.string.title_advanced_notify_action_hide),
|
|
|
|
piHide)
|
|
|
|
.setShowsUserInterface(false)
|
|
|
|
.setAllowGeneratedReplies(false);
|
|
|
|
mbuilder.addAction(actionHide.build());
|
|
|
|
|
|
|
|
wactions.add(actionHide.build());
|
|
|
|
}
|
|
|
|
|
2019-09-24 07:34:43 +00:00
|
|
|
if (notify_snooze) {
|
|
|
|
Intent snooze = new Intent(context, ServiceUI.class)
|
|
|
|
.setAction("snooze:" + message.id)
|
|
|
|
.putExtra("group", group);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent piSnooze = PendingIntentCompat.getService(
|
|
|
|
context, ServiceUI.PI_SNOOZE, snooze, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-09-24 07:34:43 +00:00
|
|
|
NotificationCompat.Action.Builder actionSnooze = new NotificationCompat.Action.Builder(
|
2020-09-28 07:18:56 +00:00
|
|
|
R.drawable.twotone_timelapse_24,
|
2019-09-24 07:34:43 +00:00
|
|
|
context.getString(R.string.title_advanced_notify_action_snooze),
|
2019-10-01 09:58:42 +00:00
|
|
|
piSnooze)
|
|
|
|
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MUTE)
|
2020-10-24 10:47:09 +00:00
|
|
|
.setShowsUserInterface(false)
|
2019-10-01 09:58:42 +00:00
|
|
|
.setAllowGeneratedReplies(false);
|
2019-09-24 07:34:43 +00:00
|
|
|
mbuilder.addAction(actionSnooze.build());
|
2019-10-01 14:47:18 +00:00
|
|
|
|
|
|
|
wactions.add(actionSnooze.build());
|
2019-09-24 07:34:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 08:34:16 +00:00
|
|
|
if (message.content && notify_preview) {
|
2020-02-05 10:18:01 +00:00
|
|
|
// Android will truncate the text
|
2020-02-11 18:55:03 +00:00
|
|
|
String preview = message.preview;
|
|
|
|
if (notify_preview_all)
|
|
|
|
try {
|
2020-02-20 09:35:01 +00:00
|
|
|
File file = message.getFile(context);
|
|
|
|
preview = HtmlHelper.getFullText(file);
|
2020-10-13 15:24:39 +00:00
|
|
|
if (preview != null && preview.length() > MAX_PREVIEW)
|
|
|
|
preview = preview.substring(0, MAX_PREVIEW);
|
2020-02-11 18:55:03 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
2020-02-05 10:18:01 +00:00
|
|
|
|
2019-10-21 08:34:16 +00:00
|
|
|
// Wearables
|
2019-10-21 13:04:32 +00:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
2021-06-14 08:49:33 +00:00
|
|
|
sb.append(TextHelper.transliterate(context, message.subject));
|
2021-06-14 08:02:56 +00:00
|
|
|
if (wearable_preview && !TextUtils.isEmpty(preview)) {
|
|
|
|
if (sb.length() > 0)
|
2019-10-21 13:04:32 +00:00
|
|
|
sb.append(" - ");
|
2021-06-14 08:49:33 +00:00
|
|
|
sb.append(TextHelper.transliterate(context, preview));
|
2019-10-21 13:04:32 +00:00
|
|
|
}
|
2020-11-12 08:56:21 +00:00
|
|
|
if (sb.length() > 0)
|
|
|
|
mbuilder.setContentText(sb.toString());
|
2019-10-03 16:07:56 +00:00
|
|
|
|
2019-10-21 08:34:16 +00:00
|
|
|
// Device
|
2020-10-24 12:31:01 +00:00
|
|
|
if (!notify_messaging) {
|
2020-10-24 10:47:09 +00:00
|
|
|
StringBuilder sbm = new StringBuilder();
|
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
2020-11-08 11:47:03 +00:00
|
|
|
sbm.append("<em>").append(Html.escapeHtml(message.subject)).append("</em>").append("<br>");
|
2020-10-24 10:47:09 +00:00
|
|
|
|
|
|
|
if (!TextUtils.isEmpty(preview))
|
2020-11-08 11:47:03 +00:00
|
|
|
sbm.append(Html.escapeHtml(preview));
|
2019-10-03 16:07:56 +00:00
|
|
|
|
2020-10-24 10:47:09 +00:00
|
|
|
if (sbm.length() > 0) {
|
|
|
|
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle()
|
2020-11-08 20:09:31 +00:00
|
|
|
.bigText(HtmlHelper.fromHtml(sbm.toString(), context));
|
2020-10-24 10:47:09 +00:00
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
|
|
|
bigText.setSummaryText(message.subject);
|
2019-10-13 15:22:52 +00:00
|
|
|
|
2020-10-24 10:47:09 +00:00
|
|
|
mbuilder.setStyle(bigText);
|
|
|
|
}
|
2020-02-05 10:18:01 +00:00
|
|
|
}
|
2019-10-21 08:34:16 +00:00
|
|
|
} else {
|
2019-10-05 11:22:33 +00:00
|
|
|
if (!TextUtils.isEmpty(message.subject))
|
2021-06-14 08:49:33 +00:00
|
|
|
mbuilder.setContentText(TextHelper.transliterate(context, message.subject));
|
2019-10-05 11:22:33 +00:00
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2020-02-09 11:14:11 +00:00
|
|
|
if (info[0].hasPhoto())
|
|
|
|
mbuilder.setLargeIcon(info[0].getPhotoBitmap());
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2020-10-24 10:53:30 +00:00
|
|
|
if (info[0].hasLookupUri()) {
|
|
|
|
Person.Builder you = new Person.Builder()
|
|
|
|
.setUri(info[0].getLookupUri().toString());
|
|
|
|
mbuilder.addPerson(you.build());
|
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
|
2021-07-14 08:26:40 +00:00
|
|
|
if (pro) {
|
|
|
|
Integer color = getColor(message);
|
2021-07-15 17:02:46 +00:00
|
|
|
if (color != null) {
|
|
|
|
mbuilder.setColor(color);
|
|
|
|
mbuilder.setColorized(true);
|
|
|
|
}
|
2019-02-27 17:33:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 14:56:55 +00:00
|
|
|
// https://developer.android.com/training/wearables/notifications
|
2021-04-05 18:00:44 +00:00
|
|
|
// https://developer.android.com/reference/androidx/core/app/NotificationCompat.Action.WearableExtender
|
2019-10-21 14:56:55 +00:00
|
|
|
mbuilder.extend(new NotificationCompat.WearableExtender()
|
2020-02-05 10:18:01 +00:00
|
|
|
.addActions(wactions)
|
|
|
|
.setDismissalId(BuildConfig.APPLICATION_ID + ":" + id)
|
2020-02-05 08:12:48 +00:00
|
|
|
/* .setBridgeTag(id < 0 ? "header" : "body") */);
|
2019-10-21 14:56:55 +00:00
|
|
|
|
2021-04-05 18:00:44 +00:00
|
|
|
// https://developer.android.com/reference/androidx/car/app/notification/CarAppExtender
|
|
|
|
mbuilder.extend(new NotificationCompat.CarExtender());
|
|
|
|
|
2019-10-17 15:14:46 +00:00
|
|
|
notifications.add(mbuilder);
|
2019-02-27 17:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return notifications;
|
|
|
|
}
|
|
|
|
|
2020-06-15 10:34:25 +00:00
|
|
|
private static Integer getColor(TupleMessageEx message) {
|
|
|
|
if (!message.folderUnified && message.folderColor != null)
|
|
|
|
return message.folderColor;
|
|
|
|
return message.accountColor;
|
|
|
|
}
|
|
|
|
|
2019-09-30 09:49:42 +00:00
|
|
|
private static void setLightAndSound(NotificationCompat.Builder builder, boolean light, String sound) {
|
|
|
|
int def = 0;
|
|
|
|
|
|
|
|
if (light) {
|
|
|
|
def |= DEFAULT_LIGHTS;
|
|
|
|
Log.i("Notify light enabled");
|
|
|
|
}
|
|
|
|
|
2019-12-31 08:47:01 +00:00
|
|
|
if (!"".equals(sound)) {
|
|
|
|
// Not silent sound
|
|
|
|
Uri uri = (sound == null ? null : Uri.parse(sound));
|
|
|
|
if (uri != null && !"content".equals(uri.getScheme()))
|
|
|
|
uri = null;
|
|
|
|
Log.i("Notify sound=" + uri);
|
|
|
|
|
|
|
|
if (uri == null)
|
|
|
|
def |= DEFAULT_SOUND;
|
|
|
|
else
|
|
|
|
builder.setSound(uri);
|
|
|
|
}
|
2019-09-30 09:49:42 +00:00
|
|
|
|
|
|
|
builder.setDefaults(def);
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-06-26 11:18:53 +00:00
|
|
|
// Build pending intent
|
2019-03-19 07:05:00 +00:00
|
|
|
Intent intent = new Intent(context, ActivityView.class);
|
2020-01-13 10:33:34 +00:00
|
|
|
intent.setAction("error");
|
2019-02-27 15:05:15 +00:00
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2021-03-27 17:54:55 +00:00
|
|
|
PendingIntent pi = PendingIntentCompat.getActivity(
|
2021-05-22 14:54:17 +00:00
|
|
|
context, ActivityView.PI_ERROR, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-06-26 11:18:53 +00:00
|
|
|
// Build notification
|
|
|
|
NotificationCompat.Builder builder =
|
|
|
|
new NotificationCompat.Builder(context, channel)
|
|
|
|
.setSmallIcon(R.drawable.baseline_warning_white_24)
|
|
|
|
.setContentTitle(context.getString(R.string.title_notification_failed, title))
|
|
|
|
.setContentText(Log.formatThrowable(ex, false))
|
|
|
|
.setContentIntent(pi)
|
|
|
|
.setAutoCancel(false)
|
|
|
|
.setShowWhen(true)
|
|
|
|
.setPriority(NotificationCompat.PRIORITY_MAX)
|
|
|
|
.setOnlyAlertOnce(true)
|
|
|
|
.setCategory(NotificationCompat.CATEGORY_ERROR)
|
|
|
|
.setVisibility(NotificationCompat.VISIBILITY_SECRET)
|
|
|
|
.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(Log.formatThrowable(ex, "\n", false)));
|
|
|
|
|
|
|
|
return builder;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static class State {
|
2019-12-11 10:43:32 +00:00
|
|
|
private int backoff;
|
2020-09-23 18:31:37 +00:00
|
|
|
private boolean backingoff = false;
|
2019-05-12 16:41:51 +00:00
|
|
|
private ConnectionHelper.NetworkState networkState;
|
2020-03-11 19:56:14 +00:00
|
|
|
private Thread thread = new Thread();
|
2019-02-27 15:05:15 +00:00
|
|
|
private Semaphore semaphore = new Semaphore(0);
|
|
|
|
private boolean running = true;
|
2019-04-10 18:17:58 +00:00
|
|
|
private boolean recoverable = true;
|
2020-10-30 11:16:49 +00:00
|
|
|
private Throwable unrecoverable = null;
|
2019-12-28 19:35:41 +00:00
|
|
|
private Long lastActivity = null;
|
2019-02-27 15:05:15 +00:00
|
|
|
|
2020-09-24 06:20:09 +00:00
|
|
|
private boolean process = false;
|
2020-09-22 12:04:26 +00:00
|
|
|
private Map<FolderPriority, Long> sequence = new HashMap<>();
|
|
|
|
private Map<FolderPriority, Long> batch = new HashMap<>();
|
|
|
|
|
2019-05-12 16:41:51 +00:00
|
|
|
State(ConnectionHelper.NetworkState networkState) {
|
2019-03-16 13:12:31 +00:00
|
|
|
this.networkState = networkState;
|
|
|
|
}
|
|
|
|
|
2019-12-07 14:14:35 +00:00
|
|
|
void setNetworkState(ConnectionHelper.NetworkState networkState) {
|
|
|
|
this.networkState = networkState;
|
2019-03-16 13:12:31 +00:00
|
|
|
}
|
|
|
|
|
2019-05-12 16:41:51 +00:00
|
|
|
ConnectionHelper.NetworkState getNetworkState() {
|
2019-03-16 13:12:31 +00:00
|
|
|
return networkState;
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:39:18 +00:00
|
|
|
void setBackoff(int value) {
|
|
|
|
this.backoff = value;
|
2019-12-11 10:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int getBackoff() {
|
|
|
|
return backoff;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
void runnable(Runnable runnable, String name) {
|
|
|
|
thread = new Thread(runnable, name);
|
|
|
|
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
|
|
|
|
}
|
|
|
|
|
2020-08-10 14:45:36 +00:00
|
|
|
boolean release() {
|
|
|
|
if (!thread.isAlive())
|
|
|
|
return false;
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
semaphore.release();
|
|
|
|
yield();
|
2020-08-10 14:45:36 +00:00
|
|
|
return true;
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 18:31:37 +00:00
|
|
|
boolean acquire(long milliseconds, boolean backingoff) throws InterruptedException {
|
2020-09-21 20:11:42 +00:00
|
|
|
try {
|
2020-09-23 18:31:37 +00:00
|
|
|
this.backingoff = backingoff;
|
2020-09-21 20:11:42 +00:00
|
|
|
return semaphore.tryAcquire(milliseconds, TimeUnit.MILLISECONDS);
|
|
|
|
} finally {
|
2020-09-23 18:31:37 +00:00
|
|
|
this.backingoff = false;
|
2020-09-21 20:11:42 +00:00
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +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()) ||
|
2019-11-12 18:16:36 +00:00
|
|
|
"Not connected".equals(ex.getMessage()) || // POP3
|
2019-06-13 12:31:46 +00:00
|
|
|
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;
|
|
|
|
|
2020-03-06 14:29:33 +00:00
|
|
|
if (ex instanceof StoreClosedException ||
|
|
|
|
ex instanceof FolderClosedException ||
|
2019-06-24 06:35:13 +00:00
|
|
|
ex instanceof FolderNotFoundException)
|
2020-02-09 13:11:46 +00:00
|
|
|
// Lost folder connection to server
|
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;
|
|
|
|
|
2020-02-11 15:57:12 +00:00
|
|
|
if (ex instanceof OperationCanceledException)
|
|
|
|
recoverable = false;
|
|
|
|
|
2020-10-30 11:16:49 +00:00
|
|
|
if (!recoverable)
|
|
|
|
unrecoverable = ex;
|
|
|
|
|
2020-09-23 18:31:37 +00:00
|
|
|
if (!backingoff) {
|
2020-09-21 20:11:42 +00:00
|
|
|
thread.interrupt();
|
|
|
|
yield();
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 19:46:39 +00:00
|
|
|
void reset() {
|
2019-04-10 18:17:58 +00:00
|
|
|
recoverable = true;
|
2019-12-28 19:35:41 +00:00
|
|
|
lastActivity = null;
|
2020-09-22 12:04:26 +00:00
|
|
|
resetBatches();
|
2020-09-24 06:20:09 +00:00
|
|
|
process = true;
|
2020-09-22 12:04:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void resetBatches() {
|
2020-09-24 06:20:09 +00:00
|
|
|
process = false;
|
2020-09-22 12:04:26 +00:00
|
|
|
synchronized (this) {
|
|
|
|
for (FolderPriority key : sequence.keySet()) {
|
|
|
|
batch.put(key, sequence.get(key));
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("=== Reset " + key.folder + ":" + key.priority + " batch=" + batch.get(key));
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 18:17:58 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:04:48 +00:00
|
|
|
void ensureRunning(String reason) {
|
2021-07-27 16:32:10 +00:00
|
|
|
if (!recoverable && unrecoverable != null)
|
2021-07-27 19:04:48 +00:00
|
|
|
throw new OperationCanceledExceptionEx(reason, unrecoverable);
|
2021-07-27 16:32:10 +00:00
|
|
|
if (!running)
|
|
|
|
throw new OperationCanceledException(reason);
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:50:46 +00:00
|
|
|
boolean isRunning() {
|
2019-02-27 15:05:15 +00:00
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2019-08-12 13:50:46 +00:00
|
|
|
boolean isRecoverable() {
|
2019-04-10 18:17:58 +00:00
|
|
|
return recoverable;
|
|
|
|
}
|
|
|
|
|
2020-10-30 11:16:49 +00:00
|
|
|
Throwable getUnrecoverable() {
|
|
|
|
return unrecoverable;
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
void join(Thread thread) {
|
|
|
|
boolean joined = false;
|
2020-08-26 07:42:52 +00:00
|
|
|
boolean interrupted = false;
|
2020-08-28 07:13:52 +00:00
|
|
|
String name = thread.getName();
|
2019-02-27 15:05:15 +00:00
|
|
|
while (!joined)
|
|
|
|
try {
|
2020-08-28 07:13:52 +00:00
|
|
|
Log.i("Joining " + name +
|
|
|
|
" alive=" + thread.isAlive() +
|
|
|
|
" state=" + thread.getState());
|
|
|
|
|
2020-08-26 07:42:52 +00:00
|
|
|
thread.join(JOIN_WAIT);
|
2020-08-28 07:13:52 +00:00
|
|
|
|
|
|
|
// https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html
|
|
|
|
Thread.State state = thread.getState();
|
2020-08-26 07:42:52 +00:00
|
|
|
if (thread.isAlive()) {
|
2020-08-29 18:10:31 +00:00
|
|
|
if (interrupted)
|
2021-01-31 11:18:20 +00:00
|
|
|
Log.w("Join " + name + " failed state=" + state + " interrupted=" + interrupted);
|
2020-08-26 07:42:52 +00:00
|
|
|
if (interrupted)
|
|
|
|
joined = true; // give up
|
|
|
|
else {
|
|
|
|
thread.interrupt();
|
|
|
|
interrupted = true;
|
|
|
|
}
|
|
|
|
} else {
|
2020-08-28 07:13:52 +00:00
|
|
|
Log.i("Joined " + name + " " + " state=" + state);
|
2020-08-26 07:42:52 +00:00
|
|
|
joined = true;
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
} catch (InterruptedException ex) {
|
|
|
|
Log.w(thread.getName() + " join " + ex.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 19:35:41 +00:00
|
|
|
synchronized void activity() {
|
|
|
|
lastActivity = SystemClock.elapsedRealtime();
|
|
|
|
}
|
|
|
|
|
|
|
|
long getIdleTime() {
|
2020-05-20 06:56:59 +00:00
|
|
|
Long last = lastActivity;
|
|
|
|
return (last == null ? 0 : SystemClock.elapsedRealtime() - last);
|
2019-12-28 19:35:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-22 12:04:26 +00:00
|
|
|
long getSequence(long folder, int priority) {
|
|
|
|
synchronized (this) {
|
|
|
|
FolderPriority key = new FolderPriority(folder, priority);
|
|
|
|
if (!sequence.containsKey(key)) {
|
|
|
|
sequence.put(key, 0L);
|
|
|
|
batch.put(key, 0L);
|
|
|
|
}
|
|
|
|
long result = sequence.get(key);
|
|
|
|
sequence.put(key, result + 1);
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("=== Get " + folder + ":" + priority + " sequence=" + result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean batchCanRun(long folder, int priority, long current) {
|
2020-09-24 06:20:09 +00:00
|
|
|
if (!process) {
|
|
|
|
Log.i("=== Can " + folder + ":" + priority + " process=" + process);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-22 12:04:26 +00:00
|
|
|
synchronized (this) {
|
|
|
|
FolderPriority key = new FolderPriority(folder, priority);
|
|
|
|
boolean can = batch.get(key).equals(current);
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("=== Can " + folder + ":" + priority + " can=" + can);
|
|
|
|
return can;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void batchCompleted(long folder, int priority, long current) {
|
|
|
|
synchronized (this) {
|
|
|
|
FolderPriority key = new FolderPriority(folder, priority);
|
|
|
|
if (batch.get(key).equals(current))
|
|
|
|
batch.put(key, batch.get(key) + 1);
|
|
|
|
if (BuildConfig.DEBUG)
|
|
|
|
Log.i("=== Completed " + folder + ":" + priority + " next=" + batch.get(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 15:05:15 +00:00
|
|
|
@NonNull
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2019-12-28 19:35:41 +00:00
|
|
|
return "[running=" + running +
|
|
|
|
",recoverable=" + recoverable +
|
2020-02-05 17:42:53 +00:00
|
|
|
",idle=" + getIdleTime() + "]";
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-02-11 15:09:29 +00:00
|
|
|
|
|
|
|
private static class FolderPriority {
|
|
|
|
private long folder;
|
|
|
|
private int priority;
|
|
|
|
|
|
|
|
FolderPriority(long folder, int priority) {
|
|
|
|
this.folder = folder;
|
|
|
|
this.priority = priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return (int) (this.folder * 37 + priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(@Nullable Object obj) {
|
|
|
|
if (obj instanceof FolderPriority) {
|
|
|
|
FolderPriority other = (FolderPriority) obj;
|
|
|
|
return (this.folder == other.folder && this.priority == other.priority);
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|
2020-07-15 16:58:03 +00:00
|
|
|
|
2021-07-27 19:04:48 +00:00
|
|
|
static class OperationCanceledExceptionEx extends OperationCanceledException {
|
|
|
|
private Throwable cause;
|
|
|
|
|
|
|
|
OperationCanceledExceptionEx(String message, Throwable cause) {
|
|
|
|
super(message);
|
|
|
|
this.cause = cause;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public Throwable getCause() {
|
|
|
|
return this.cause;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
private static class SyncStats {
|
|
|
|
long search_ms;
|
|
|
|
int flags;
|
|
|
|
long flags_ms;
|
2020-07-25 05:33:58 +00:00
|
|
|
int uids;
|
2020-07-15 16:58:03 +00:00
|
|
|
long uids_ms;
|
|
|
|
int headers;
|
|
|
|
long headers_ms;
|
|
|
|
long content;
|
|
|
|
long attachments;
|
|
|
|
long total;
|
|
|
|
|
2020-07-25 05:33:58 +00:00
|
|
|
boolean isEmpty() {
|
|
|
|
return (search_ms == 0 &&
|
|
|
|
flags == 0 &&
|
|
|
|
flags_ms == 0 &&
|
|
|
|
uids == 0 &&
|
|
|
|
uids_ms == 0 &&
|
|
|
|
headers == 0 &&
|
|
|
|
headers_ms == 0 &&
|
|
|
|
content == 0 &&
|
|
|
|
attachments == 0 &&
|
|
|
|
total == 0);
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:58:03 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "search=" + search_ms + " ms" +
|
|
|
|
" flags=" + flags + "/" + flags_ms + " ms" +
|
|
|
|
" uids=" + uids + "/" + uids_ms + " ms" +
|
|
|
|
" headers=" + headers + "/" + headers_ms + " ms" +
|
|
|
|
" content=" + Helper.humanReadableByteCount(content) +
|
|
|
|
" attachments=" + Helper.humanReadableByteCount(attachments) +
|
2020-07-25 05:42:51 +00:00
|
|
|
" total=" + total + " ms";
|
2020-07-15 16:58:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 09:47:03 +00:00
|
|
|
|
|
|
|
static class NotificationData {
|
|
|
|
private Map<Long, List<Long>> groupNotifying = new HashMap<>();
|
|
|
|
|
|
|
|
NotificationData(Context context) {
|
|
|
|
// Get existing notifications
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
|
|
try {
|
|
|
|
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
for (StatusBarNotification sbn : nm.getActiveNotifications()) {
|
|
|
|
String tag = sbn.getTag();
|
|
|
|
if (tag != null && tag.startsWith("unseen.")) {
|
|
|
|
String[] p = tag.split(("\\."));
|
|
|
|
long group = Long.parseLong(p[1]);
|
|
|
|
long id = sbn.getNotification().extras.getLong("id", 0);
|
|
|
|
|
|
|
|
if (!groupNotifying.containsKey(group))
|
|
|
|
groupNotifying.put(group, new ArrayList<>());
|
|
|
|
|
|
|
|
if (id > 0) {
|
2021-06-08 14:53:28 +00:00
|
|
|
EntityLog.log(context, "Notify restore " + tag + " id=" + id);
|
2021-02-15 09:47:03 +00:00
|
|
|
groupNotifying.get(group).add(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
/*
|
|
|
|
java.lang.RuntimeException: Unable to create service eu.faircode.email.ServiceSynchronize: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List android.content.pm.ParceledListSlice.getList()' on a null object reference
|
|
|
|
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2944)
|
|
|
|
at android.app.ActivityThread.access$1900(ActivityThread.java:154)
|
|
|
|
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
|
|
|
|
at android.os.Handler.dispatchMessage(Handler.java:102)
|
|
|
|
at android.os.Looper.loop(Looper.java:234)
|
|
|
|
at android.app.ActivityThread.main(ActivityThread.java:5526)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-27 15:05:15 +00:00
|
|
|
}
|