|
@ -23,53 +23,20 @@ import android.content.Context; |
|
|
import android.os.Handler; |
|
|
import android.os.Handler; |
|
|
import android.util.Log; |
|
|
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; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
import java.util.Properties; |
|
|
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
import java.util.concurrent.ExecutorService; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
|
import javax.mail.FetchProfile; |
|
|
|
|
|
import javax.mail.Folder; |
|
|
|
|
|
import javax.mail.FolderClosedException; |
|
|
|
|
|
import javax.mail.Message; |
|
|
|
|
|
import javax.mail.MessageRemovedException; |
|
|
|
|
|
import javax.mail.Session; |
|
|
|
|
|
import javax.mail.UIDFolder; |
|
|
|
|
|
import javax.mail.search.BodyTerm; |
|
|
|
|
|
import javax.mail.search.FromStringTerm; |
|
|
|
|
|
import javax.mail.search.OrTerm; |
|
|
|
|
|
import javax.mail.search.RecipientStringTerm; |
|
|
|
|
|
import javax.mail.search.SubjectTerm; |
|
|
|
|
|
|
|
|
|
|
|
import androidx.lifecycle.GenericLifecycleObserver; |
|
|
import androidx.lifecycle.GenericLifecycleObserver; |
|
|
import androidx.lifecycle.Lifecycle; |
|
|
import androidx.lifecycle.Lifecycle; |
|
|
import androidx.lifecycle.LifecycleOwner; |
|
|
import androidx.lifecycle.LifecycleOwner; |
|
|
import androidx.paging.PagedList; |
|
|
import androidx.paging.PagedList; |
|
|
|
|
|
|
|
|
public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMessageEx> { |
|
|
public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMessageEx> { |
|
|
private Context context; |
|
|
|
|
|
private long fid; |
|
|
|
|
|
private String search; |
|
|
|
|
|
private int pageSize; |
|
|
|
|
|
|
|
|
private ViewModelBrowse model; |
|
|
private Handler handler; |
|
|
private Handler handler; |
|
|
private IBoundaryCallbackMessages intf; |
|
|
private IBoundaryCallbackMessages intf; |
|
|
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); |
|
|
private ExecutorService executor = Executors.newSingleThreadExecutor(Helper.backgroundThreadFactory); |
|
|
|
|
|
|
|
|
private IMAPStore istore = null; |
|
|
|
|
|
private IMAPFolder ifolder = null; |
|
|
|
|
|
private Message[] imessages = null; |
|
|
|
|
|
private List<Long> existing = new ArrayList<>(); |
|
|
|
|
|
private int index; |
|
|
|
|
|
private boolean searching = false; |
|
|
|
|
|
private int loaded = 0; |
|
|
|
|
|
|
|
|
|
|
|
interface IBoundaryCallbackMessages { |
|
|
interface IBoundaryCallbackMessages { |
|
|
void onLoading(); |
|
|
void onLoading(); |
|
|
|
|
|
|
|
@ -78,53 +45,31 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe |
|
|
void onError(Context context, Throwable ex); |
|
|
void onError(Context context, Throwable ex); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
BoundaryCallbackMessages(Context _context, LifecycleOwner owner, long folder, String search, int pageSize, IBoundaryCallbackMessages intf) { |
|
|
|
|
|
this.context = _context; |
|
|
|
|
|
this.fid = folder; |
|
|
|
|
|
this.search = search; |
|
|
|
|
|
this.pageSize = pageSize; |
|
|
|
|
|
|
|
|
BoundaryCallbackMessages(LifecycleOwner owner, ViewModelBrowse _model, IBoundaryCallbackMessages intf) { |
|
|
|
|
|
this.model = _model; |
|
|
this.handler = new Handler(); |
|
|
this.handler = new Handler(); |
|
|
this.intf = intf; |
|
|
this.intf = intf; |
|
|
|
|
|
|
|
|
owner.getLifecycle().addObserver(new GenericLifecycleObserver() { |
|
|
owner.getLifecycle().addObserver(new GenericLifecycleObserver() { |
|
|
@Override |
|
|
@Override |
|
|
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { |
|
|
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) { |
|
|
if (event == Lifecycle.Event.ON_DESTROY) { |
|
|
|
|
|
|
|
|
if (event == Lifecycle.Event.ON_DESTROY) |
|
|
executor.submit(new Runnable() { |
|
|
executor.submit(new Runnable() { |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
Log.i(Helper.TAG, "Boundary close"); |
|
|
|
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context); |
|
|
|
|
|
for (long id : existing) |
|
|
|
|
|
db.message().setMessageFound(id, false); |
|
|
|
|
|
db.message().deleteFoundMessages(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
if (istore != null) |
|
|
|
|
|
istore.close(); |
|
|
|
|
|
} catch (Throwable ex) { |
|
|
|
|
|
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex)); |
|
|
|
|
|
} finally { |
|
|
|
|
|
context = null; |
|
|
|
|
|
istore = null; |
|
|
|
|
|
ifolder = null; |
|
|
|
|
|
imessages = null; |
|
|
|
|
|
existing.clear(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
model.clear(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
boolean isSearching() { |
|
|
boolean isSearching() { |
|
|
return searching; |
|
|
|
|
|
|
|
|
return model.isSearching(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int getLoaded() { |
|
|
int getLoaded() { |
|
|
return loaded; |
|
|
|
|
|
|
|
|
return model.getLoaded(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -144,116 +89,22 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
try { |
|
|
try { |
|
|
searching = true; |
|
|
|
|
|
handler.post(new Runnable() { |
|
|
handler.post(new Runnable() { |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
intf.onLoading(); |
|
|
intf.onLoading(); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
DB db = DB.getInstance(context); |
|
|
|
|
|
EntityFolder folder = db.folder().getFolder(fid); |
|
|
|
|
|
if (folder.account == null) // outbox |
|
|
|
|
|
return; |
|
|
|
|
|
EntityAccount account = db.account().getAccount(folder.account); |
|
|
|
|
|
|
|
|
|
|
|
if (imessages == null) { |
|
|
|
|
|
Properties props = MessageHelper.getSessionProperties(account.auth_type); |
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
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( |
|
|
|
|
|
new OrTerm( |
|
|
|
|
|
new FromStringTerm(search), |
|
|
|
|
|
new RecipientStringTerm(Message.RecipientType.TO, search) |
|
|
|
|
|
), |
|
|
|
|
|
new OrTerm( |
|
|
|
|
|
new SubjectTerm(search), |
|
|
|
|
|
new BodyTerm(search) |
|
|
|
|
|
) |
|
|
|
|
|
) |
|
|
|
|
|
); |
|
|
|
|
|
Log.i(Helper.TAG, "Boundary found messages=" + imessages.length); |
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
ServiceSynchronize.synchronizeMessage(context, folder, ifolder, (IMAPMessage) isub[j], search != null); |
|
|
|
|
|
count++; |
|
|
|
|
|
loaded++; |
|
|
|
|
|
} else if (search != null) { |
|
|
|
|
|
existing.add(message.id); |
|
|
|
|
|
db.message().setMessageFound(message.id, true); |
|
|
|
|
|
} |
|
|
|
|
|
} 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(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Boundary done"); |
|
|
|
|
|
|
|
|
model.load(); |
|
|
} catch (final Throwable ex) { |
|
|
} catch (final Throwable ex) { |
|
|
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex)); |
|
|
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex)); |
|
|
handler.post(new Runnable() { |
|
|
handler.post(new Runnable() { |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
intf.onError(context, ex); |
|
|
|
|
|
|
|
|
intf.onError(model.getContext(), ex); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
} finally { |
|
|
} finally { |
|
|
searching = false; |
|
|
|
|
|
handler.post(new Runnable() { |
|
|
handler.post(new Runnable() { |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|