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

626 lines
26 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;
2018-09-02 06:59:49 +00:00
import androidx.paging.PagedList;
import androidx.preference.PreferenceManager;
2018-09-02 06:59:49 +00:00
import com.sun.mail.iap.Argument;
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;
2019-10-02 19:13:10 +00:00
import java.io.File;
import java.io.IOException;
2019-10-30 12:38:25 +00:00
import java.nio.charset.StandardCharsets;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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;
import javax.mail.search.FlagTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.RecipientStringTerm;
import javax.mail.search.SearchTerm;
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;
private String query;
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;
2019-10-10 11:26:44 +00:00
private ExecutorService executor = Helper.getBackgroundExecutor(1, "boundary");
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;
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-02-02 16:17:58 +00:00
BoundaryCallbackMessages(Context context, long account, long folder, boolean server, String query, 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;
this.query = query;
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-03-24 13:57:51 +00:00
queue_load();
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-03-24 13:57:51 +00:00
queue_load();
2018-09-02 06:59:49 +00:00
}
2020-03-24 12:20:27 +00:00
void retry() {
state.reset();
2020-03-24 13:57:51 +00:00
queue_load();
2020-03-24 12:20:27 +00:00
}
2020-03-24 13:57:51 +00:00
private void queue_load() {
2019-08-09 17:29:17 +00:00
final State state = this.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);
close();
state.reset();
// 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-10-02 19:13:10 +00:00
Boolean seen = null;
Boolean flagged = null;
Boolean snoozed = null;
Boolean encrypted = null;
Boolean attachments = null;
2020-02-15 14:53:11 +00:00
String find = (TextUtils.isEmpty(query) ? null : query.toLowerCase());
2019-10-02 19:13:10 +00:00
if (find != null && find.startsWith(context.getString(R.string.title_search_special_prefix) + ":")) {
String special = find.split(":")[1];
if (context.getString(R.string.title_search_special_unseen).equals(special))
seen = false;
else if (context.getString(R.string.title_search_special_flagged).equals(special))
flagged = true;
else if (context.getString(R.string.title_search_special_snoozed).equals(special))
snoozed = true;
else if (context.getString(R.string.title_search_special_encrypted).equals(special))
encrypted = true;
else if (context.getString(R.string.title_search_special_attachments).equals(special))
attachments = true;
2019-06-13 11:20:51 +00:00
}
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);
if (fts && pro && seen == null && flagged == null && snoozed == null && encrypted == null && attachments == null) {
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-02-02 16:17:58 +00:00
state.ids = FtsDbHelper.match(sdb, account, folder, query);
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,
2019-10-29 10:34:03 +00:00
"%" + find + "%",
seen, flagged, snoozed, encrypted, attachments,
2019-10-29 10:34:03 +00:00
SEARCH_LIMIT, state.offset);
Log.i("Boundary device folder=" + folder +
" query=" + query +
" seen=" + seen +
" flagged=" + flagged +
" snoozed=" + snoozed +
" encrypted=" + encrypted +
" attachments=" + attachments +
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-02-26 16:36:19 +00:00
if (find == null || seen != null || flagged != null || snoozed != null || encrypted != null || attachments != null)
2019-10-29 08:46:39 +00:00
match.matched = true;
else {
if (match.matched == null || !match.matched)
try {
File file = EntityMessage.getFile(context, match.id);
if (file.exists()) {
String html = Helper.readText(file);
2020-02-15 14:53:11 +00:00
if (html.toLowerCase().contains(find)) {
2020-02-11 19:07:00 +00:00
String text = HtmlHelper.getFullText(html);
2020-02-15 14:53:11 +00:00
if (text.toLowerCase().contains(find))
2019-10-29 12:43:03 +00:00
match.matched = true;
}
2019-10-29 08:46:39 +00:00
}
} catch (IOException ex) {
Log.e(ex);
2019-07-24 10:00:47 +00:00
}
2019-10-29 08:46:39 +00:00
}
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);
2020-02-06 08:37:08 +00:00
final boolean search_text = prefs.getBoolean("search_text", false);
final boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.BETA_RELEASE);
2019-05-12 12:28:18 +00:00
final EntityFolder browsable = db.folder().getBrowsableFolder(folder, query != 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));
2019-05-12 12:28:18 +00:00
Log.i("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);
2019-05-12 12:28:18 +00:00
Log.i("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);
2019-05-12 12:28:18 +00:00
Log.i("Boundary server query=" + query);
2019-08-15 14:39:07 +00:00
if (query == 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);
2019-10-05 14:33:33 +00:00
Log.i("Boundary filter seen=" + filter_seen + " unflagged=" + filter_unflagged);
2019-08-15 14:39:07 +00:00
2019-10-05 14:33:33 +00:00
SearchTerm searchUnseen = null;
2019-09-13 11:28:46 +00:00
if (filter_seen && state.ifolder.getPermanentFlags().contains(Flags.Flag.SEEN))
2019-10-05 14:33:33 +00:00
searchUnseen = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
2019-09-13 11:28:46 +00:00
2019-10-05 14:33:33 +00:00
SearchTerm searchFlagged = null;
if (filter_unflagged && state.ifolder.getPermanentFlags().contains(Flags.Flag.FLAGGED))
searchFlagged = new FlagTerm(new Flags(Flags.Flag.FLAGGED), true);
if (searchUnseen != null && searchFlagged != null)
state.imessages = state.ifolder.search(new AndTerm(searchUnseen, searchFlagged));
else if (searchUnseen != null)
state.imessages = state.ifolder.search(searchUnseen);
else if (searchFlagged != null)
state.imessages = state.ifolder.search(searchFlagged);
else
state.imessages = state.ifolder.getMessages();
2019-08-15 14:39:07 +00:00
} else if (query.startsWith(context.getString(R.string.title_search_special_prefix) + ":")) {
2019-07-24 16:46:39 +00:00
String special = query.split(":")[1];
if (context.getString(R.string.title_search_special_unseen).equals(special))
2019-08-09 17:29:17 +00:00
state.imessages = state.ifolder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
2019-07-24 16:46:39 +00:00
else if (context.getString(R.string.title_search_special_flagged).equals(special))
2019-08-09 17:29:17 +00:00
state.imessages = state.ifolder.search(new FlagTerm(new Flags(Flags.Flag.FLAGGED), true));
2019-07-24 16:46:39 +00:00
else
2019-08-09 17:29:17 +00:00
state.imessages = new Message[0];
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
public Object doCommand(IMAPProtocol protocol) {
// Yahoo! does not support keyword search, but uses the flags $Forwarded $Junk $NotJunk
boolean keywords = false;
2019-05-12 09:40:54 +00:00
for (String keyword : browsable.keywords)
if (!keyword.startsWith("$")) {
keywords = true;
break;
}
try {
// https://tools.ietf.org/html/rfc3501#section-6.4.4
Argument arg = new Argument();
2019-09-19 15:41:26 +00:00
if (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
arg.writeAtom("X-GM-RAW");
2019-05-12 12:28:18 +00:00
arg.writeString(query.substring(4));
} else {
if (!protocol.supportsUtf8()) {
arg.writeAtom("CHARSET");
2019-10-30 12:38:25 +00:00
arg.writeAtom(StandardCharsets.UTF_8.name());
}
arg.writeAtom("OR");
arg.writeAtom("OR");
2020-04-09 07:09:01 +00:00
arg.writeAtom("OR");
if (search_text)
arg.writeAtom("OR");
if (keywords)
arg.writeAtom("OR");
arg.writeAtom("FROM");
2019-05-12 12:28:18 +00:00
arg.writeBytes(query.getBytes());
arg.writeAtom("TO");
2019-05-12 12:28:18 +00:00
arg.writeBytes(query.getBytes());
2020-04-09 07:09:01 +00:00
arg.writeAtom("CC");
arg.writeBytes(query.getBytes());
arg.writeAtom("SUBJECT");
2019-05-12 12:28:18 +00:00
arg.writeBytes(query.getBytes());
if (search_text) {
arg.writeAtom("BODY");
arg.writeBytes(query.getBytes());
}
if (keywords) {
arg.writeAtom("KEYWORD");
2019-05-12 12:28:18 +00:00
arg.writeBytes(query.getBytes());
}
}
Response[] responses = protocol.command("SEARCH", arg);
if (responses.length > 0 && responses[responses.length - 1].isOK()) {
2020-04-09 07:47:56 +00:00
Log.i("Boundary UTF8 search=" + query);
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-04-09 07:47:56 +00:00
if (responses.length > 0)
Log.e("Search response=" + responses[responses.length - 1]);
// Assume no UTF-8 support
2019-05-12 12:28:18 +00:00
String search = query.replace("ß", "ss"); // Eszett
search = Normalizer.normalize(search, Normalizer.Form.NFD)
.replaceAll("[^\\p{ASCII}]", "");
Log.i("Boundary ASCII search=" + search);
2020-04-09 07:09:01 +00:00
SearchTerm term = new FromStringTerm(search);
term = new OrTerm(term, new RecipientStringTerm(Message.RecipientType.TO, search));
term = new OrTerm(term, new RecipientStringTerm(Message.RecipientType.CC, search));
term = new OrTerm(term, new SubjectTerm(search));
if (search_text)
term = new OrTerm(term, new BodyTerm(search));
if (keywords)
term = new OrTerm(term, new FlagTerm(
2019-09-26 10:11:46 +00:00
new Flags(MessageHelper.sanitizeKeyword(search)), true));
2019-08-09 17:29:17 +00:00
return state.ifolder.search(term);
}
} catch (MessagingException ex) {
Log.e(ex);
return ex;
}
}
});
if (result instanceof MessagingException)
throw (MessagingException) result;
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);
} catch (Throwable ignored) {
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);
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]);
2019-05-12 12:28:18 +00:00
Log.i("Boundary server sync 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-01-29 09:53:08 +00:00
if (message != null && query != null /* browsed */)
2020-01-09 19:11:43 +00:00
db.message().setMessageFound(message.id);
} catch (MessageRemovedException ex) {
2019-05-12 12:28:18 +00:00
Log.w(browsable.name + " boundary server", ex);
} catch (FolderClosedException ex) {
throw ex;
} catch (IOException 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) {
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() {
2019-08-09 17:29:17 +00:00
final State state = this.state;
this.state = new State();
state.destroyed = true;
2019-05-14 16:34:09 +00:00
executor.submit(new Runnable() {
@Override
public void run() {
2020-03-22 09:14:36 +00:00
close();
2019-05-14 16:34:09 +00:00
}
});
}
2019-08-09 17:29:17 +00:00
2020-03-24 13:57:51 +00:00
private void close() {
2020-03-22 09:14:36 +00:00
Log.i("Boundary close");
try {
if (state.ifolder != null)
state.ifolder.close();
} catch (Throwable ex) {
Log.e("Boundary", ex);
}
try {
if (state.iservice != null)
state.iservice.close();
} catch (Throwable ex) {
Log.e("Boundary", ex);
}
}
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() {
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
}
2018-09-02 06:59:49 +00:00
}