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

824 lines
35 KiB
Java
Raw Normal View History

2018-09-02 06:59:49 +00:00
package eu.faircode.email;
2018-09-04 07:02:54 +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.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-09-04 07:02:54 +00:00
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
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-09-04 07:02:54 +00:00
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-09-04 07:02:54 +00:00
*/
import android.content.Context;
import android.content.SharedPreferences;
2018-09-02 06:59:49 +00:00
import android.os.Handler;
2019-07-24 10:00:47 +00:00
import android.text.TextUtils;
2018-09-02 06:59:49 +00:00
2019-06-12 20:22:17 +00:00
import androidx.annotation.NonNull;
2020-04-09 15:50:29 +00:00
import androidx.annotation.Nullable;
2018-09-02 06:59:49 +00:00
import androidx.paging.PagedList;
import androidx.preference.PreferenceManager;
2018-09-02 06:59:49 +00:00
2020-05-01 19:47:59 +00:00
import com.sun.mail.gimap.GmailFolder;
import com.sun.mail.iap.Argument;
2020-04-09 15:50:29 +00:00
import com.sun.mail.iap.ProtocolException;
import com.sun.mail.iap.Response;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
2019-10-05 18:44:45 +00:00
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.imap.protocol.IMAPProtocol;
import com.sun.mail.imap.protocol.IMAPResponse;
2020-05-03 21:28:14 +00:00
import com.sun.mail.imap.protocol.SearchSequence;
2019-10-02 19:13:10 +00:00
import java.io.File;
import java.io.IOException;
2020-04-09 15:50:29 +00:00
import java.io.Serializable;
2020-05-03 21:28:14 +00:00
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
2020-04-10 09:51:50 +00:00
import java.util.Date;
import java.util.List;
2020-04-09 15:50:29 +00:00
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import javax.mail.FetchProfile;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.FolderClosedException;
import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
2019-12-05 19:05:07 +00:00
import javax.mail.ReadOnlyFolderException;
import javax.mail.UIDFolder;
2019-09-26 08:53:27 +00:00
import javax.mail.internet.MimeMessage;
2019-09-13 11:28:46 +00:00
import javax.mail.search.AndTerm;
import javax.mail.search.BodyTerm;
2020-04-10 09:51:50 +00:00
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FlagTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
2020-04-10 09:51:50 +00:00
import javax.mail.search.ReceivedDateTerm;
import javax.mail.search.RecipientStringTerm;
import javax.mail.search.SearchTerm;
2020-06-12 16:49:46 +00:00
import javax.mail.search.SizeTerm;
import javax.mail.search.SubjectTerm;
2020-01-14 21:39:35 +00:00
import io.requery.android.database.sqlite.SQLiteDatabase;
2018-09-02 06:59:49 +00:00
public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMessageEx> {
private Context context;
2020-02-02 16:17:58 +00:00
private Long account;
2019-05-12 09:40:54 +00:00
private Long folder;
2019-05-12 12:28:18 +00:00
private boolean server;
2020-04-09 15:50:29 +00:00
private SearchCriteria criteria;
private int pageSize;
2019-05-14 16:34:09 +00:00
2018-09-02 06:59:49 +00:00
private IBoundaryCallbackMessages intf;
2018-10-20 17:56:09 +00:00
private Handler handler;
2018-09-02 06:59:49 +00:00
2019-08-09 17:29:17 +00:00
private State state;
private static final int SEARCH_LIMIT = 1000;
2020-04-09 19:19:44 +00:00
private static ExecutorService executor = Helper.getBackgroundExecutor(1, "boundary");
2019-10-29 08:46:39 +00:00
2018-09-02 06:59:49 +00:00
interface IBoundaryCallbackMessages {
void onLoading();
2020-03-24 13:57:51 +00:00
void onLoaded();
2018-09-02 06:59:49 +00:00
2019-06-13 06:10:47 +00:00
void onException(@NonNull Throwable ex);
2018-09-02 06:59:49 +00:00
}
2020-04-09 15:50:29 +00:00
BoundaryCallbackMessages(Context context, long account, long folder, boolean server, SearchCriteria criteria, int pageSize) {
this.context = context.getApplicationContext();
2020-02-02 16:17:58 +00:00
this.account = (account < 0 ? null : account);
2019-05-12 09:40:54 +00:00
this.folder = (folder < 0 ? null : folder);
2019-05-12 12:28:18 +00:00
this.server = server;
2020-04-09 15:50:29 +00:00
this.criteria = criteria;
this.pageSize = pageSize;
2019-05-14 16:34:09 +00:00
}
2018-09-02 06:59:49 +00:00
2019-05-14 16:34:09 +00:00
void setCallback(IBoundaryCallbackMessages intf) {
this.handler = new Handler();
2019-05-14 16:34:09 +00:00
this.intf = intf;
2019-08-09 17:29:17 +00:00
this.state = new State();
2018-09-02 06:59:49 +00:00
}
@Override
public void onZeroItemsLoaded() {
2019-05-12 12:28:18 +00:00
Log.i("Boundary zero loaded");
2020-04-10 07:02:09 +00:00
queue_load(state);
2018-09-02 06:59:49 +00:00
}
@Override
2019-08-09 17:29:17 +00:00
public void onItemAtEndLoaded(@NonNull final TupleMessageEx itemAtEnd) {
2019-05-12 12:28:18 +00:00
Log.i("Boundary at end");
2020-04-10 07:02:09 +00:00
queue_load(state);
2018-09-02 06:59:49 +00:00
}
2020-03-24 12:20:27 +00:00
void retry() {
2020-04-10 07:02:09 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
close(state);
}
});
queue_load(state);
2020-03-24 12:20:27 +00:00
}
2020-04-10 07:02:09 +00:00
private void queue_load(final State state) {
2019-01-22 15:55:21 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
try {
2019-08-09 17:29:17 +00:00
if (state.destroyed || state.error)
2019-05-12 12:28:18 +00:00
return;
2019-05-14 16:34:09 +00:00
if (intf != null)
handler.post(new Runnable() {
@Override
public void run() {
intf.onLoading();
}
});
2019-05-12 12:28:18 +00:00
if (server)
2020-03-22 09:14:36 +00:00
try {
2020-03-24 13:57:51 +00:00
load_server(state);
2020-03-22 09:14:36 +00:00
} catch (Throwable ex) {
if (state.error || ex instanceof IllegalArgumentException)
throw ex;
Log.w("Boundary", ex);
2020-04-10 07:02:09 +00:00
close(state);
2020-03-22 09:14:36 +00:00
// Retry
2020-03-24 13:57:51 +00:00
load_server(state);
2020-03-22 09:14:36 +00:00
}
2019-05-12 12:28:18 +00:00
else
2020-03-24 13:57:51 +00:00
load_device(state);
2019-01-22 15:55:21 +00:00
} catch (final Throwable ex) {
2020-03-24 12:25:16 +00:00
state.error = true;
2019-01-22 15:55:21 +00:00
Log.e("Boundary", ex);
2019-05-14 16:34:09 +00:00
if (intf != null)
handler.post(new Runnable() {
@Override
public void run() {
2019-06-13 06:10:47 +00:00
intf.onException(ex);
2019-05-14 16:34:09 +00:00
}
});
2019-01-22 15:55:21 +00:00
} finally {
2019-05-14 16:34:09 +00:00
if (intf != null)
handler.post(new Runnable() {
@Override
public void run() {
2020-03-24 13:57:51 +00:00
intf.onLoaded();
2019-05-14 16:34:09 +00:00
}
});
2018-09-02 06:59:49 +00:00
}
2019-01-22 15:55:21 +00:00
}
});
2018-09-02 06:59:49 +00:00
}
2019-01-29 08:35:42 +00:00
2019-08-09 17:29:17 +00:00
private int load_device(State state) {
DB db = DB.getInstance(context);
2019-05-12 12:28:18 +00:00
int found = 0;
2020-01-14 20:58:27 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean fts = prefs.getBoolean("fts", false);
2020-01-16 17:21:03 +00:00
boolean pro = ActivityBilling.isPro(context);
2020-04-09 15:50:29 +00:00
if (fts && pro && criteria.isQueryOnly()) {
2020-01-14 20:58:27 +00:00
if (state.ids == null) {
2020-01-15 13:48:33 +00:00
SQLiteDatabase sdb = FtsDbHelper.getInstance(context);
2020-04-10 09:51:50 +00:00
state.ids = FtsDbHelper.match(sdb, account, folder, criteria);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary FTS " +
" account=" + account +
" folder=" + folder +
" criteria=" + criteria +
" ids=" + state.ids.size());
EntityLog.log(context, "FTS results=" + state.ids.size());
2020-01-14 20:58:27 +00:00
}
try {
db.beginTransaction();
for (; state.index < state.ids.size() && found < pageSize && !state.destroyed; state.index++) {
found++;
db.message().setMessageFound(state.ids.get(state.index));
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
return found;
}
2019-05-12 09:40:54 +00:00
try {
db.beginTransaction();
2019-10-29 08:46:39 +00:00
while (found < pageSize && !state.destroyed) {
2019-10-29 10:34:03 +00:00
if (state.matches == null ||
(state.matches.size() > 0 && state.index >= state.matches.size())) {
state.matches = db.message().matchMessages(
2020-02-02 16:17:58 +00:00
account, folder,
2020-04-09 15:50:29 +00:00
criteria.query == null ? null : "%" + criteria.query + "%",
2020-05-27 09:04:36 +00:00
criteria.in_senders,
criteria.in_recipients,
criteria.in_subject,
criteria.in_keywords,
criteria.in_message,
2020-04-09 15:50:29 +00:00
criteria.with_unseen,
criteria.with_flagged,
criteria.with_hidden,
criteria.with_encrypted,
criteria.with_attachments,
2020-06-19 15:39:02 +00:00
criteria.with_types == null ? 0 : criteria.with_types.length,
criteria.with_types == null ? new String[]{} : criteria.with_types,
2020-06-12 16:49:46 +00:00
criteria.with_size,
2020-04-10 10:26:23 +00:00
criteria.after,
criteria.before,
2019-10-29 10:34:03 +00:00
SEARCH_LIMIT, state.offset);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary device" +
" account=" + account +
" folder=" + folder +
2020-04-09 15:50:29 +00:00
" criteria=" + criteria +
2019-10-29 10:34:03 +00:00
" offset=" + state.offset +
" size=" + state.matches.size());
state.offset += Math.min(state.matches.size(), SEARCH_LIMIT);
state.index = 0;
}
2019-10-29 08:46:39 +00:00
2019-10-29 11:09:50 +00:00
if (state.matches.size() == 0)
break;
2019-10-29 08:46:39 +00:00
for (int i = state.index; i < state.matches.size() && found < pageSize && !state.destroyed; i++) {
state.index = i + 1;
TupleMatch match = state.matches.get(i);
2020-05-27 09:04:36 +00:00
if (criteria.query != null &&
criteria.in_message &&
(match.matched == null || !match.matched))
2020-04-09 15:50:29 +00:00
try {
File file = EntityMessage.getFile(context, match.id);
if (file.exists()) {
String html = Helper.readText(file);
if (html.toLowerCase().contains(criteria.query)) {
String text = HtmlHelper.getFullText(html);
if (text.toLowerCase().contains(criteria.query))
match.matched = true;
2019-10-29 08:46:39 +00:00
}
2019-07-24 10:00:47 +00:00
}
2020-04-09 15:50:29 +00:00
} catch (IOException ex) {
Log.e(ex);
}
2019-10-29 08:46:39 +00:00
if (match.matched != null && match.matched) {
found++;
2020-01-09 19:11:43 +00:00
db.message().setMessageFound(match.id);
2019-10-29 08:46:39 +00:00
}
2019-05-12 09:40:54 +00:00
}
}
2019-05-12 09:40:54 +00:00
db.setTransactionSuccessful();
2019-05-12 12:28:18 +00:00
if (found == pageSize)
return found;
2019-05-12 09:40:54 +00:00
} finally {
db.endTransaction();
}
2019-05-12 12:28:18 +00:00
Log.i("Boundary device done");
return found;
}
2019-08-09 17:29:17 +00:00
private int load_server(State state) throws MessagingException, IOException {
2019-05-12 12:28:18 +00:00
DB db = DB.getInstance(context);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
2020-04-09 15:50:29 +00:00
final EntityFolder browsable = db.folder().getBrowsableFolder(folder, criteria != null);
2020-03-03 09:37:15 +00:00
if (browsable == null || !browsable.selectable) {
Log.w("Boundary not browsable=" + (folder != null));
2019-06-07 17:41:23 +00:00
return 0;
2020-03-03 09:37:15 +00:00
}
2019-05-12 12:28:18 +00:00
2019-05-12 09:40:54 +00:00
EntityAccount account = db.account().getAccount(browsable.account);
if (account == null)
2019-06-07 17:41:23 +00:00
return 0;
2019-08-09 17:29:17 +00:00
if (state.imessages == null)
try {
// Check connectivity
2019-05-12 16:41:51 +00:00
if (!ConnectionHelper.getNetworkState(context).isSuitable())
2019-06-13 06:09:01 +00:00
throw new IllegalStateException(context.getString(R.string.title_no_internet));
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary server connecting account=" + account.name);
2020-02-06 12:06:10 +00:00
state.iservice = new EmailService(
context, account.getProtocol(), account.realm, account.insecure, EmailService.PURPOSE_SEARCH, debug);
2019-08-09 17:29:17 +00:00
state.iservice.setPartialFetch(account.partial_fetch);
state.iservice.setIgnoreBodyStructureSize(account.ignore_size);
2019-08-09 17:29:17 +00:00
state.iservice.connect(account);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary server opening folder=" + browsable.name);
2019-08-09 17:29:17 +00:00
state.ifolder = (IMAPFolder) state.iservice.getStore().getFolder(browsable.name);
2019-12-05 19:05:07 +00:00
try {
state.ifolder.open(Folder.READ_WRITE);
db.folder().setFolderReadOnly(browsable.id, state.ifolder.getUIDNotSticky());
} catch (ReadOnlyFolderException ex) {
state.ifolder.open(Folder.READ_ONLY);
db.folder().setFolderReadOnly(browsable.id, true);
}
2020-03-22 10:34:11 +00:00
db.folder().setFolderError(browsable.id, null);
2019-08-21 18:23:13 +00:00
int count = state.ifolder.getMessageCount();
db.folder().setFolderTotal(browsable.id, count < 0 ? null : count);
2020-04-09 15:50:29 +00:00
if (criteria == null) {
2019-09-13 11:28:46 +00:00
boolean filter_seen = prefs.getBoolean("filter_seen", false);
boolean filter_unflagged = prefs.getBoolean("filter_unflagged", false);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary filter seen=" + filter_seen + " unflagged=" + filter_unflagged);
2019-08-15 14:39:07 +00:00
2020-04-09 19:19:44 +00:00
List<SearchTerm> and = new ArrayList<>();
2019-09-13 11:28:46 +00:00
2020-04-09 19:19:44 +00:00
if (filter_seen &&
state.ifolder.getPermanentFlags().contains(Flags.Flag.SEEN))
and.add(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
2019-10-05 14:33:33 +00:00
2020-04-09 19:19:44 +00:00
if (filter_unflagged &&
state.ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED))
and.add(new FlagTerm(new Flags(Flags.Flag.FLAGGED), true));
if (and.size() == 0)
2019-10-05 14:33:33 +00:00
state.imessages = state.ifolder.getMessages();
2020-04-09 19:19:44 +00:00
else
state.imessages = state.ifolder.search(new AndTerm(and.toArray(new SearchTerm[0])));
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary filter messages=" + state.imessages.length);
2019-07-24 16:46:39 +00:00
} else {
2019-08-09 17:29:17 +00:00
Object result = state.ifolder.doCommand(new IMAPFolder.ProtocolCommand() {
@Override
2020-04-09 15:50:29 +00:00
public Object doCommand(IMAPProtocol protocol) throws ProtocolException {
try {
// https://tools.ietf.org/html/rfc3501#section-6.4.4
2020-04-09 15:50:29 +00:00
if (criteria.query != null &&
criteria.query.startsWith("raw:") &&
state.iservice.hasCapability("X-GM-EXT-1")) {
// https://support.google.com/mail/answer/7190
// https://developers.google.com/gmail/imap/imap-extensions#extension_of_the_search_command_x-gm-raw
2020-05-04 08:23:10 +00:00
Log.i("Boundary raw search=" + criteria.query);
Argument arg = new Argument();
arg.writeAtom("X-GM-RAW");
2020-04-09 15:50:29 +00:00
arg.writeString(criteria.query.substring(4));
Response[] responses = protocol.command("SEARCH", arg);
if (responses.length == 0)
throw new ProtocolException("No response");
if (!responses[responses.length - 1].isOK())
throw new ProtocolException(responses[responses.length - 1]);
2020-04-09 07:47:56 +00:00
List<Integer> msgnums = new ArrayList<>();
for (Response response : responses)
if (((IMAPResponse) response).keyEquals("SEARCH")) {
int msgnum;
while ((msgnum = response.readNumber()) != -1)
msgnums.add(msgnum);
}
Message[] imessages = new Message[msgnums.size()];
for (int i = 0; i < msgnums.size(); i++)
2019-08-09 17:29:17 +00:00
imessages[i] = state.ifolder.getMessage(msgnums.get(i));
return imessages;
} else {
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary server" +
" account=" + account +
" folder=" + folder +
" search=" + criteria);
2020-04-09 15:50:29 +00:00
2020-05-04 08:23:10 +00:00
if (protocol.supportsUtf8())
try {
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Search unicode");
2020-05-04 08:23:10 +00:00
SearchTerm terms = criteria.getTerms(
true,
state.ifolder.getPermanentFlags(),
browsable.keywords);
2020-07-07 15:00:52 +00:00
if (terms == null) {
EntityLog.log(context, "Search unicode no terms");
2020-07-07 06:22:11 +00:00
return new Message[0];
2020-07-07 15:00:52 +00:00
}
2020-05-04 08:23:10 +00:00
SearchSequence ss = new SearchSequence(protocol);
Argument args = ss.generateSequence(terms, StandardCharsets.UTF_8.name());
args.writeAtom("ALL");
Response[] responses = protocol.command("SEARCH", args);
2020-07-07 12:44:06 +00:00
for (Response response : responses)
EntityLog.log(context, "Search unicode response=" + response);
2020-05-04 08:23:10 +00:00
if (responses.length == 0)
throw new ProtocolException("No response");
if (!responses[responses.length - 1].isOK())
throw new ProtocolException(responses[responses.length - 1]);
List<Integer> msgnums = new ArrayList<>();
for (Response response : responses)
if (((IMAPResponse) response).keyEquals("SEARCH")) {
int msgnum;
while ((msgnum = response.readNumber()) != -1)
msgnums.add(msgnum);
2020-05-03 21:28:14 +00:00
}
2020-07-07 12:44:06 +00:00
EntityLog.log(context, "Search unicode messages=" + msgnums.size());
2020-05-04 08:23:10 +00:00
Message[] imessages = new Message[msgnums.size()];
for (int i = 0; i < msgnums.size(); i++)
imessages[i] = state.ifolder.getMessage(msgnums.get(i));
return imessages;
} catch (Throwable ex) {
ProtocolException pex = new ProtocolException(
"Search unicode " + account.host + " " + criteria, ex);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, pex.toString());
2020-05-04 08:23:10 +00:00
Log.e(pex);
// Fallback to ASCII search
}
2020-04-09 15:50:29 +00:00
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Search ASCII");
2020-05-03 20:22:55 +00:00
SearchTerm terms = criteria.getTerms(
2020-05-03 21:28:14 +00:00
false,
2020-05-03 20:22:55 +00:00
state.ifolder.getPermanentFlags(),
browsable.keywords);
2020-07-07 15:00:52 +00:00
if (terms == null) {
EntityLog.log(context, "Search ASCII no terms");
2020-07-07 06:22:11 +00:00
return new Message[0];
2020-07-07 15:00:52 +00:00
}
2020-07-07 12:44:06 +00:00
Message[] messages = state.ifolder.search(terms);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Search ASCII" +
" messages=" + (messages == null ? null : messages.length));
2020-07-07 12:44:06 +00:00
return messages;
}
2020-07-08 06:03:03 +00:00
} catch (Throwable ex) {
2020-04-10 07:23:28 +00:00
ProtocolException pex = new ProtocolException(
"Search " + account.host + " " + criteria, ex);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, pex.toString());
2020-04-10 07:23:28 +00:00
Log.e(pex);
throw pex;
}
}
});
2019-08-09 17:29:17 +00:00
state.imessages = (Message[]) result;
}
2019-08-09 17:29:17 +00:00
Log.i("Boundary server found messages=" + state.imessages.length);
2019-08-09 17:29:17 +00:00
state.index = state.imessages.length - 1;
} catch (Throwable ex) {
2019-08-09 17:29:17 +00:00
state.error = true;
if (ex instanceof FolderClosedException)
Log.w("Search", ex);
2019-06-18 19:30:57 +00:00
else
Log.e("Search", ex);
2019-06-18 19:30:57 +00:00
throw ex;
}
2019-06-13 08:40:09 +00:00
List<EntityRule> rules = db.rule().getEnabledRules(browsable.id);
2019-05-12 12:28:18 +00:00
int found = 0;
2019-08-09 17:29:17 +00:00
while (state.index >= 0 && found < pageSize && !state.destroyed) {
Log.i("Boundary server index=" + state.index);
int from = Math.max(0, state.index - (pageSize - found) + 1);
Message[] isub = Arrays.copyOfRange(state.imessages, from, state.index + 1);
state.index -= (pageSize - found);
2019-10-05 14:33:33 +00:00
FetchProfile fp0 = new FetchProfile();
fp0.add(UIDFolder.FetchProfileItem.UID);
state.ifolder.fetch(isub, fp0);
List<Message> add = new ArrayList<>();
for (Message m : isub)
try {
long uid = state.ifolder.getUID(m);
if (db.message().getMessageByUid(browsable.id, uid) == null)
add.add(m);
2020-06-14 15:15:39 +00:00
} catch (Throwable ex) {
Log.w(ex);
2019-10-05 14:33:33 +00:00
add.add(m);
}
Log.i("Boundary fetching " + add.size() + "/" + isub.length);
if (add.size() > 0) {
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);
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-10-05 14:33:33 +00:00
state.ifolder.fetch(add.toArray(new Message[0]), fp);
}
try {
db.beginTransaction();
2020-03-09 09:08:20 +00:00
Core.State astate = new Core.State(ConnectionHelper.getNetworkState(context));
for (int j = isub.length - 1; j >= 0 && found < pageSize && !state.destroyed && astate.isRecoverable(); j--)
try {
2019-08-09 17:29:17 +00:00
long uid = state.ifolder.getUID(isub[j]);
2020-07-07 15:00:52 +00:00
EntityLog.log(context, "Boundary server sync index=" + state.index + " j=" + j + " uid=" + uid);
2019-05-12 09:40:54 +00:00
EntityMessage message = db.message().getMessageByUid(browsable.id, uid);
if (message == null) {
message = Core.synchronizeMessage(context,
2019-05-12 09:40:54 +00:00
account, browsable,
2019-10-05 18:44:45 +00:00
(IMAPStore) state.iservice.getStore(), state.ifolder, (MimeMessage) isub[j],
true, true,
2020-03-09 09:08:20 +00:00
rules, astate);
2019-05-12 12:28:18 +00:00
found++;
}
2020-04-09 15:50:29 +00:00
if (message != null && criteria != null /* browsed */)
2020-01-09 19:11:43 +00:00
db.message().setMessageFound(message.id);
} catch (MessageRemovedException ex) {
2020-07-07 15:00:52 +00:00
EntityLog.log(context, browsable.name + " boundary server ex=" + ex);
2019-05-12 12:28:18 +00:00
Log.w(browsable.name + " boundary server", ex);
} catch (FolderClosedException ex) {
2020-07-07 15:00:52 +00:00
EntityLog.log(context, browsable.name + " boundary server ex=" + ex);
throw ex;
} catch (IOException ex) {
2020-07-07 15:00:52 +00:00
EntityLog.log(context, browsable.name + " boundary server ex=" + ex);
if (ex.getCause() instanceof MessagingException) {
2019-05-12 12:28:18 +00:00
Log.w(browsable.name + " boundary server", ex);
2019-12-06 07:50:46 +00:00
db.folder().setFolderError(browsable.id, Log.formatThrowable(ex));
} else
throw ex;
} catch (Throwable ex) {
2020-07-07 15:00:52 +00:00
EntityLog.log(context, browsable.name + " boundary server ex=" + ex);
2019-05-12 12:28:18 +00:00
Log.e(browsable.name + " boundary server", ex);
2019-12-06 07:50:46 +00:00
db.folder().setFolderError(browsable.id, Log.formatThrowable(ex));
} finally {
((IMAPMessage) isub[j]).invalidateHeaders();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
2019-05-12 12:28:18 +00:00
Log.i("Boundary server done");
return found;
}
2019-05-14 16:34:09 +00:00
2020-03-22 09:14:36 +00:00
void destroy() {
2020-04-10 07:02:09 +00:00
final State old = this.state;
old.destroyed = true;
2019-08-09 17:29:17 +00:00
this.state = new State();
2019-05-14 16:34:09 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
2020-04-10 07:02:09 +00:00
close(old);
2019-05-14 16:34:09 +00:00
}
});
}
2019-08-09 17:29:17 +00:00
2020-04-10 07:02:09 +00:00
private void close(State state) {
2020-03-22 09:14:36 +00:00
Log.i("Boundary close");
2020-04-10 07:02:09 +00:00
2020-03-22 09:14:36 +00:00
try {
if (state.ifolder != null)
state.ifolder.close();
} catch (Throwable ex) {
Log.e("Boundary", ex);
}
2020-04-10 07:02:09 +00:00
2020-03-22 09:14:36 +00:00
try {
if (state.iservice != null)
state.iservice.close();
} catch (Throwable ex) {
Log.e("Boundary", ex);
}
2020-04-10 07:02:09 +00:00
state.reset();
2020-03-22 09:14:36 +00:00
}
2019-08-09 17:29:17 +00:00
private class State {
boolean destroyed = false;
boolean error = false;
int index = 0;
2019-10-29 08:46:39 +00:00
int offset = 0;
2020-01-14 20:58:27 +00:00
List<Long> ids = null;
2019-10-23 18:01:43 +00:00
List<TupleMatch> matches = null;
2019-08-09 17:29:17 +00:00
2020-01-29 20:06:45 +00:00
EmailService iservice = null;
2019-08-09 17:29:17 +00:00
IMAPFolder ifolder = null;
Message[] imessages = null;
2020-03-22 09:14:36 +00:00
void reset() {
2020-04-09 19:19:44 +00:00
Log.i("Boundary reset");
2020-03-22 09:14:36 +00:00
destroyed = false;
error = false;
index = 0;
offset = 0;
ids = null;
matches = null;
iservice = null;
ifolder = null;
imessages = null;
}
2019-08-09 17:29:17 +00:00
}
2020-04-09 15:50:29 +00:00
static class SearchCriteria implements Serializable {
String query;
boolean in_senders = true;
2020-05-04 08:23:10 +00:00
boolean in_recipients = true;
2020-04-09 15:50:29 +00:00
boolean in_subject = true;
2020-04-10 14:54:20 +00:00
boolean in_keywords = true;
2020-04-09 15:50:29 +00:00
boolean in_message = true;
boolean with_unseen;
boolean with_flagged;
boolean with_hidden;
boolean with_encrypted;
boolean with_attachments;
2020-06-19 13:18:43 +00:00
String[] with_types;
2020-06-12 16:49:46 +00:00
Integer with_size = null;
2020-04-10 10:26:23 +00:00
Long after = null;
Long before = null;
2020-04-09 15:50:29 +00:00
SearchCriteria() {
}
SearchCriteria(String query) {
this.query = query;
}
boolean isQueryOnly() {
return (!TextUtils.isEmpty(query) &&
in_senders &&
2020-05-04 08:23:10 +00:00
in_recipients &&
2020-04-09 15:50:29 +00:00
in_subject &&
in_keywords &&
in_message &&
isWithout());
}
boolean isWithout() {
return !(with_unseen ||
with_flagged ||
with_hidden ||
with_encrypted ||
2020-06-12 16:49:46 +00:00
with_attachments ||
2020-06-19 13:18:43 +00:00
with_types != null ||
2020-06-12 16:49:46 +00:00
with_size != null);
2020-04-09 15:50:29 +00:00
}
2020-05-03 20:22:55 +00:00
SearchTerm getTerms(boolean utf8, Flags flags, String[] keywords) {
List<SearchTerm> or = new ArrayList<>();
List<SearchTerm> and = new ArrayList<>();
if (query != null) {
String search = query;
if (!utf8) {
search = search.replace("ß", "ss"); // Eszett
search = Normalizer.normalize(search, Normalizer.Form.NFKD)
.replaceAll("[^\\p{ASCII}]", "");
}
// Yahoo! does not support keyword search, but uses the flags $Forwarded $Junk $NotJunk
boolean hasKeywords = false;
for (String keyword : keywords)
if (!keyword.startsWith("$")) {
hasKeywords = true;
break;
}
if (in_senders)
or.add(new FromStringTerm(search));
2020-05-04 08:23:10 +00:00
if (in_recipients) {
2020-05-03 20:22:55 +00:00
or.add(new RecipientStringTerm(Message.RecipientType.TO, search));
or.add(new RecipientStringTerm(Message.RecipientType.CC, search));
or.add(new RecipientStringTerm(Message.RecipientType.BCC, search));
}
if (in_subject)
or.add(new SubjectTerm(search));
if (in_keywords && hasKeywords)
or.add(new FlagTerm(new Flags(MessageHelper.sanitizeKeyword(search)), true));
if (in_message)
or.add(new BodyTerm(search));
}
if (with_unseen && flags.contains(Flags.Flag.SEEN))
and.add(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
if (with_flagged && flags.contains(Flags.Flag.FLAGGED))
and.add(new FlagTerm(new Flags(Flags.Flag.FLAGGED), true));
2020-06-12 16:49:46 +00:00
if (with_size != null)
and.add(new SizeTerm(ComparisonTerm.GT, with_size));
2020-05-03 20:22:55 +00:00
if (after != null)
2020-05-27 06:17:55 +00:00
and.add(new ReceivedDateTerm(ComparisonTerm.GE, new Date(after)));
2020-05-03 20:22:55 +00:00
if (before != null)
2020-05-27 06:17:55 +00:00
and.add(new ReceivedDateTerm(ComparisonTerm.LE, new Date(before)));
2020-05-03 20:22:55 +00:00
SearchTerm term = null;
if (or.size() > 0)
term = new OrTerm(or.toArray(new SearchTerm[0]));
if (and.size() > 0)
if (term == null)
term = new AndTerm(and.toArray(new SearchTerm[0]));
else
term = new AndTerm(term, new AndTerm(and.toArray(new SearchTerm[0])));
return term;
}
2020-04-09 17:33:57 +00:00
String getTitle(Context context) {
List<String> flags = new ArrayList<>();
if (with_unseen)
flags.add(context.getString(R.string.title_search_flag_unseen));
if (with_flagged)
flags.add(context.getString(R.string.title_search_flag_flagged));
if (with_hidden)
flags.add(context.getString(R.string.title_search_flag_hidden));
if (with_encrypted)
flags.add(context.getString(R.string.title_search_flag_encrypted));
if (with_attachments)
flags.add(context.getString(R.string.title_search_flag_attachments));
2020-06-19 13:18:43 +00:00
if (with_types != null)
if (with_types.length == 1 && "text/calendar".equals(with_types[0]))
flags.add(context.getString(R.string.title_search_flag_invite));
else
flags.add(TextUtils.join(", ", with_types));
2020-06-12 16:49:46 +00:00
if (with_size != null)
flags.add(context.getString(R.string.title_search_flag_size,
2020-07-02 08:19:01 +00:00
Helper.humanReadableByteCount(with_size)));
2020-04-09 17:33:57 +00:00
return (query == null ? "" : query)
+ (flags.size() > 0 ? " +" : "")
+ TextUtils.join(",", flags);
2020-04-09 15:50:29 +00:00
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj instanceof SearchCriteria) {
SearchCriteria other = (SearchCriteria) obj;
return (Objects.equals(this.query, other.query) &&
this.in_senders == other.in_senders &&
2020-05-04 08:23:10 +00:00
this.in_recipients == other.in_recipients &&
2020-04-09 15:50:29 +00:00
this.in_subject == other.in_subject &&
this.in_keywords == other.in_keywords &&
this.in_message == other.in_message &&
this.with_unseen == other.with_unseen &&
this.with_flagged == other.with_flagged &&
this.with_hidden == other.with_hidden &&
this.with_encrypted == other.with_encrypted &&
2020-04-10 09:51:50 +00:00
this.with_attachments == other.with_attachments &&
2020-06-19 13:18:43 +00:00
Objects.equals(this.with_types, other.with_types) &&
2020-06-12 16:49:46 +00:00
Objects.equals(this.with_size, other.with_size) &&
2020-04-10 10:26:23 +00:00
Objects.equals(this.after, other.after) &&
Objects.equals(this.before, other.before));
2020-04-09 15:50:29 +00:00
} else
return false;
}
@NonNull
@Override
public String toString() {
return query +
" senders=" + in_senders +
2020-05-04 08:23:10 +00:00
" recipients=" + in_recipients +
2020-04-09 15:50:29 +00:00
" subject=" + in_subject +
" keywords=" + in_keywords +
" message=" + in_message +
" unseen=" + with_unseen +
" flagged=" + with_flagged +
" hidden=" + with_hidden +
" encrypted=" + with_encrypted +
2020-04-10 16:08:19 +00:00
" attachments=" + with_attachments +
2020-06-19 13:18:43 +00:00
" type=" + (with_types == null ? null : TextUtils.join(",", with_types)) +
2020-06-12 16:49:46 +00:00
" size=" + with_size +
2020-04-10 16:08:19 +00:00
" after=" + (after == null ? "" : new Date(after)) +
" before=" + (before == null ? "" : new Date(before));
2020-04-09 15:50:29 +00:00
}
}
2018-09-02 06:59:49 +00:00
}