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

267 lines
11 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.
NetGuard 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 NetGuard. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018 by Marcel Bokhorst (M66B)
*/
2018-09-02 06:59:49 +00:00
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPMessage;
import com.sun.mail.imap.IMAPStore;
import com.sun.mail.util.FolderClosedIOException;
2018-09-02 06:59:49 +00:00
2018-10-11 05:48:44 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2018-10-11 05:48:44 +00:00
import java.util.List;
2018-09-02 06:59:49 +00:00
import java.util.Properties;
2018-09-02 08:55:06 +00:00
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
2018-09-02 06:59:49 +00:00
2018-09-16 18:07:26 +00:00
import javax.mail.FetchProfile;
2018-09-02 06:59:49 +00:00
import javax.mail.Folder;
import javax.mail.FolderClosedException;
2018-09-02 06:59:49 +00:00
import javax.mail.Message;
import javax.mail.MessageRemovedException;
2018-09-02 06:59:49 +00:00
import javax.mail.Session;
2018-09-16 18:07:26 +00:00
import javax.mail.UIDFolder;
2018-09-02 06:59:49 +00:00
import javax.mail.search.BodyTerm;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
2018-10-06 21:06:39 +00:00
import javax.mail.search.RecipientStringTerm;
2018-09-02 06:59:49 +00:00
import javax.mail.search.SubjectTerm;
import androidx.lifecycle.GenericLifecycleObserver;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.paging.PagedList;
public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMessageEx> {
private Context context;
private long fid;
private String search;
private int pageSize;
2018-09-23 04:28:13 +00:00
private Handler handler;
2018-09-02 06:59:49 +00:00
private IBoundaryCallbackMessages intf;
2018-09-04 07:02:54 +00:00
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory);
2018-09-02 06:59:49 +00:00
private IMAPStore istore = null;
private IMAPFolder ifolder = null;
private Message[] imessages = null;
2018-10-11 05:48:44 +00:00
private List<Long> existing = new ArrayList<>();
private int index;
private boolean searching = false;
private int loaded = 0;
2018-09-15 06:35:59 +00:00
2018-09-02 06:59:49 +00:00
interface IBoundaryCallbackMessages {
void onLoading();
void onLoaded();
2018-09-05 07:38:53 +00:00
void onError(Context context, Throwable ex);
2018-09-02 06:59:49 +00:00
}
BoundaryCallbackMessages(Context _context, LifecycleOwner owner, long folder, String search, int pageSize, IBoundaryCallbackMessages intf) {
this.context = _context;
2018-09-02 06:59:49 +00:00
this.fid = folder;
this.search = search;
this.pageSize = pageSize;
2018-09-23 04:28:13 +00:00
this.handler = new Handler();
2018-09-02 06:59:49 +00:00
this.intf = intf;
owner.getLifecycle().addObserver(new GenericLifecycleObserver() {
@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
2018-09-23 04:28:13 +00:00
if (event == Lifecycle.Event.ON_DESTROY) {
2018-09-12 10:15:11 +00:00
executor.submit(new Runnable() {
2018-09-02 06:59:49 +00:00
@Override
public void run() {
Log.i(Helper.TAG, "Boundary close");
2018-10-11 05:48:44 +00:00
DB db = DB.getInstance(context);
for (long id : existing)
db.message().setMessageFound(id, false);
db.message().deleteFoundMessages();
2018-09-02 06:59:49 +00:00
try {
if (istore != null)
istore.close();
} catch (Throwable ex) {
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
} finally {
context = null;
2018-09-02 06:59:49 +00:00
istore = null;
ifolder = null;
imessages = null;
2018-10-11 05:48:44 +00:00
existing.clear();
2018-09-02 06:59:49 +00:00
}
}
2018-09-12 10:15:11 +00:00
});
2018-09-23 04:28:13 +00:00
}
2018-09-02 06:59:49 +00:00
}
});
}
boolean isSearching() {
return searching;
}
int getLoaded() {
return loaded;
}
@Override
public void onZeroItemsLoaded() {
Log.i(Helper.TAG, "onZeroItemsLoaded");
load();
2018-09-02 06:59:49 +00:00
}
@Override
public void onItemAtEndLoaded(final TupleMessageEx itemAtEnd) {
Log.i(Helper.TAG, "onItemAtEndLoaded");
load();
2018-09-02 06:59:49 +00:00
}
private void load() {
2018-09-02 08:55:06 +00:00
executor.submit(new Runnable() {
2018-09-02 06:59:49 +00:00
@Override
public void run() {
2018-09-02 08:55:06 +00:00
try {
searching = true;
2018-09-23 04:28:13 +00:00
handler.post(new Runnable() {
2018-09-02 08:55:06 +00:00
@Override
public void run() {
2018-09-26 11:32:24 +00:00
intf.onLoading();
2018-09-02 08:55:06 +00:00
}
});
2018-09-02 06:59:49 +00:00
2018-09-02 08:55:06 +00:00
DB db = DB.getInstance(context);
EntityFolder folder = db.folder().getFolder(fid);
2018-09-23 04:28:13 +00:00
if (folder.account == null) // outbox
return;
2018-09-02 08:55:06 +00:00
EntityAccount account = db.account().getAccount(folder.account);
2018-09-02 06:59:49 +00:00
2018-09-02 08:55:06 +00:00
if (imessages == null) {
2018-09-21 13:18:26 +00:00
Properties props = MessageHelper.getSessionProperties(account.auth_type);
2018-09-02 08:55:06 +00:00
props.setProperty("mail.imap.throwsearchexception", "true");
Session isession = Session.getInstance(props, null);
Log.i(Helper.TAG, "Boundary connecting account=" + account.name);
istore = (IMAPStore) isession.getStore("imaps");
Helper.connect(context, istore, account);
2018-09-02 08:55:06 +00:00
Log.i(Helper.TAG, "Boundary opening folder=" + folder.name);
ifolder = (IMAPFolder) istore.getFolder(folder.name);
ifolder.open(Folder.READ_WRITE);
Log.i(Helper.TAG, "Boundary searching=" + search);
if (search == null)
imessages = ifolder.getMessages();
else
imessages = ifolder.search(
new OrTerm(
2018-10-06 21:06:39 +00:00
new OrTerm(
new FromStringTerm(search),
new RecipientStringTerm(Message.RecipientType.TO, search)
),
new OrTerm(
new SubjectTerm(search),
2018-10-06 21:06:39 +00:00
new BodyTerm(search)
)
)
);
2018-09-02 08:55:06 +00:00
Log.i(Helper.TAG, "Boundary found messages=" + imessages.length);
2018-09-16 18:07:26 +00:00
index = imessages.length - 1;
}
int count = 0;
while (index >= 0 && count < pageSize) {
Log.i(Helper.TAG, "Boundary index=" + index);
int from = Math.max(0, index - (pageSize - count) + 1);
Message[] isub = Arrays.copyOfRange(imessages, from, index + 1);
index -= (pageSize - count);
2018-09-16 18:07:26 +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(FetchProfile.Item.SIZE);
fp.add(IMAPFolder.FetchProfileItem.INTERNALDATE);
ifolder.fetch(isub, fp);
try {
db.beginTransaction();
for (int j = isub.length - 1; j >= 0; j--)
try {
long uid = ifolder.getUID(isub[j]);
Log.i(Helper.TAG, "Boundary sync uid=" + uid);
EntityMessage message = db.message().getMessageByUid(fid, uid);
if (message == null) {
2018-09-21 13:37:00 +00:00
ServiceSynchronize.synchronizeMessage(context, folder, ifolder, (IMAPMessage) isub[j], search != null);
count++;
loaded++;
2018-10-11 05:48:44 +00:00
} else if (search != null) {
existing.add(message.id);
db.message().setMessageFound(message.id, true);
2018-10-11 05:48:44 +00:00
}
} catch (MessageRemovedException ex) {
Log.w(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
} catch (FolderClosedException ex) {
throw ex;
} catch (FolderClosedIOException ex) {
throw ex;
} catch (Throwable ex) {
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
} finally {
((IMAPMessage) isub[j]).invalidateHeaders();
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
2018-09-02 06:59:49 +00:00
}
2018-09-02 08:55:06 +00:00
Log.i(Helper.TAG, "Boundary done");
} catch (final Throwable ex) {
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex));
2018-09-23 04:28:13 +00:00
handler.post(new Runnable() {
2018-09-02 08:55:06 +00:00
@Override
public void run() {
2018-09-26 11:32:24 +00:00
intf.onError(context, ex);
2018-09-02 08:55:06 +00:00
}
});
} finally {
searching = false;
2018-09-23 04:28:13 +00:00
handler.post(new Runnable() {
@Override
public void run() {
2018-09-26 11:32:24 +00:00
intf.onLoaded();
2018-09-23 04:28:13 +00:00
}
});
2018-09-02 06:59:49 +00:00
}
}
2018-09-02 08:55:06 +00:00
});
2018-09-02 06:59:49 +00:00
}
}