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;
|
2018-09-21 11:53:54 +00:00
|
|
|
import com.sun.mail.util.FolderClosedIOException;
|
2018-09-02 06:59:49 +00:00
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
import java.util.Arrays;
|
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;
|
2018-09-21 11:53:54 +00:00
|
|
|
import javax.mail.FolderClosedException;
|
2018-09-02 06:59:49 +00:00
|
|
|
import javax.mail.Message;
|
2018-09-21 11:53:54 +00:00
|
|
|
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;
|
|
|
|
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;
|
2018-09-21 11:53:54 +00:00
|
|
|
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-09-21 11:53:54 +00:00
|
|
|
private int index;
|
|
|
|
private boolean searching = false;
|
2018-09-21 12:43:12 +00:00
|
|
|
private int loaded = 0;
|
2018-09-23 04:28:13 +00:00
|
|
|
private boolean destroyed = false;
|
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
|
|
|
}
|
|
|
|
|
2018-09-21 11:53:54 +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;
|
2018-09-21 11:53:54 +00:00
|
|
|
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) {
|
|
|
|
destroyed = true;
|
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-09-21 11:53:54 +00:00
|
|
|
DB.getInstance(context).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 {
|
2018-09-21 11:53:54 +00:00
|
|
|
context = null;
|
2018-09-02 06:59:49 +00:00
|
|
|
istore = null;
|
|
|
|
ifolder = null;
|
|
|
|
imessages = null;
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 10:15:11 +00:00
|
|
|
});
|
2018-09-23 04:28:13 +00:00
|
|
|
}
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
boolean isSearching() {
|
|
|
|
return searching;
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:43:12 +00:00
|
|
|
int getLoaded() {
|
|
|
|
return loaded;
|
|
|
|
}
|
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
@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) {
|
2018-09-21 11:53:54 +00:00
|
|
|
Log.i(Helper.TAG, "onItemAtEndLoaded");
|
|
|
|
load();
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 11:53:54 +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 {
|
2018-09-21 11:53:54 +00:00
|
|
|
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-23 04:28:13 +00:00
|
|
|
if (!destroyed)
|
|
|
|
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");
|
2018-09-21 11:53:54 +00:00
|
|
|
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);
|
|
|
|
|
2018-09-21 11:53:54 +00:00
|
|
|
Log.i(Helper.TAG, "Boundary searching=" + search);
|
|
|
|
if (search == null)
|
|
|
|
imessages = ifolder.getMessages();
|
|
|
|
else
|
|
|
|
imessages = ifolder.search(
|
|
|
|
new OrTerm(
|
|
|
|
new FromStringTerm(search),
|
|
|
|
new OrTerm(
|
|
|
|
new SubjectTerm(search),
|
|
|
|
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
|
|
|
|
2018-09-21 11:53:54 +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);
|
2018-09-21 11:53:54 +00:00
|
|
|
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);
|
|
|
|
if (db.message().getMessageByUid(fid, uid) == null) {
|
2018-09-21 13:37:00 +00:00
|
|
|
ServiceSynchronize.synchronizeMessage(context, folder, ifolder, (IMAPMessage) isub[j], search != null);
|
2018-09-21 11:53:54 +00:00
|
|
|
count++;
|
2018-09-21 12:43:12 +00:00
|
|
|
loaded++;
|
2018-09-21 11:53:54 +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-23 04:28:13 +00:00
|
|
|
if (!destroyed)
|
|
|
|
intf.onError(context, ex);
|
2018-09-02 08:55:06 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} finally {
|
2018-09-21 11:53:54 +00:00
|
|
|
searching = false;
|
2018-09-23 04:28:13 +00:00
|
|
|
handler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!destroyed)
|
|
|
|
intf.onLoaded();
|
|
|
|
}
|
|
|
|
});
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-02 08:55:06 +00:00
|
|
|
});
|
2018-09-02 06:59:49 +00:00
|
|
|
}
|
|
|
|
}
|