|
@ -10,6 +10,8 @@ import com.sun.mail.imap.IMAPStore; |
|
|
|
|
|
|
|
|
import java.util.Date; |
|
|
import java.util.Date; |
|
|
import java.util.Properties; |
|
|
import java.util.Properties; |
|
|
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
|
|
|
import javax.mail.Folder; |
|
|
import javax.mail.Folder; |
|
|
import javax.mail.Message; |
|
|
import javax.mail.Message; |
|
@ -33,6 +35,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe |
|
|
private String search; |
|
|
private String search; |
|
|
private Handler mainHandler; |
|
|
private Handler mainHandler; |
|
|
private IBoundaryCallbackMessages intf; |
|
|
private IBoundaryCallbackMessages intf; |
|
|
|
|
|
private ExecutorService executor = Executors.newSingleThreadExecutor(); |
|
|
|
|
|
|
|
|
private boolean enabled = false; |
|
|
private boolean enabled = false; |
|
|
private IMAPStore istore = null; |
|
|
private IMAPStore istore = null; |
|
@ -91,82 +94,80 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void load(final long before) { |
|
|
void load(final long before) { |
|
|
new Thread(new Runnable() { |
|
|
executor.submit(new Runnable() { |
|
|
@Override |
|
|
@Override |
|
|
public void run() { |
|
|
public void run() { |
|
|
synchronized (context.getApplicationContext()) { |
|
|
try { |
|
|
try { |
|
|
mainHandler.post(new Runnable() { |
|
|
mainHandler.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); |
|
|
|
|
|
EntityAccount account = db.account().getAccount(folder.account); |
|
|
|
|
|
|
|
|
|
|
|
if (imessages == null) { |
|
|
DB db = DB.getInstance(context); |
|
|
// Refresh token |
|
|
EntityFolder folder = db.folder().getFolder(fid); |
|
|
if (account.auth_type == Helper.AUTH_TYPE_GMAIL) { |
|
|
EntityAccount account = db.account().getAccount(folder.account); |
|
|
account.password = Helper.refreshToken(context, "com.google", account.user, account.password); |
|
|
|
|
|
db.account().setAccountPassword(account.id, account.password); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Properties props = MessageHelper.getSessionProperties(context, account.auth_type); |
|
|
if (imessages == null) { |
|
|
props.setProperty("mail.imap.throwsearchexception", "true"); |
|
|
// Refresh token |
|
|
Session isession = Session.getInstance(props, null); |
|
|
if (account.auth_type == Helper.AUTH_TYPE_GMAIL) { |
|
|
|
|
|
account.password = Helper.refreshToken(context, "com.google", account.user, account.password); |
|
|
Log.i(Helper.TAG, "Boundary connecting account=" + account.name); |
|
|
db.account().setAccountPassword(account.id, account.password); |
|
|
istore = (IMAPStore) isession.getStore("imaps"); |
|
|
|
|
|
istore.connect(account.host, account.port, account.user, account.password); |
|
|
|
|
|
|
|
|
|
|
|
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 + " before=" + new Date(before)); |
|
|
|
|
|
imessages = ifolder.search( |
|
|
|
|
|
new AndTerm( |
|
|
|
|
|
new ReceivedDateTerm(ComparisonTerm.LT, new Date(before)), |
|
|
|
|
|
new OrTerm( |
|
|
|
|
|
new FromStringTerm(search), |
|
|
|
|
|
new OrTerm( |
|
|
|
|
|
new SubjectTerm(search), |
|
|
|
|
|
new BodyTerm(search))))); |
|
|
|
|
|
Log.i(Helper.TAG, "Boundary found messages=" + imessages.length); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int index = imessages.length - 1; |
|
|
Properties props = MessageHelper.getSessionProperties(context, account.auth_type); |
|
|
while (index >= 0) { |
|
|
props.setProperty("mail.imap.throwsearchexception", "true"); |
|
|
if (imessages[index].getReceivedDate().getTime() < before) { |
|
|
Session isession = Session.getInstance(props, null); |
|
|
Log.i(Helper.TAG, "Boundary sync uid=" + ifolder.getUID(imessages[index])); |
|
|
Log.i(Helper.TAG, "Boundary connecting account=" + account.name); |
|
|
ServiceSynchronize.synchronizeMessage(context, folder, ifolder, (IMAPMessage) imessages[index], true); |
|
|
istore = (IMAPStore) isession.getStore("imaps"); |
|
|
break; |
|
|
istore.connect(account.host, account.port, account.user, account.password); |
|
|
} |
|
|
Log.i(Helper.TAG, "Boundary opening folder=" + folder.name); |
|
|
index--; |
|
|
ifolder = (IMAPFolder) istore.getFolder(folder.name); |
|
|
} |
|
|
ifolder.open(Folder.READ_WRITE); |
|
|
|
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Boundary searching=" + search + " before=" + new Date(before)); |
|
|
|
|
|
imessages = ifolder.search( |
|
|
|
|
|
new AndTerm( |
|
|
|
|
|
new ReceivedDateTerm(ComparisonTerm.LT, new Date(before)), |
|
|
|
|
|
new OrTerm( |
|
|
|
|
|
new FromStringTerm(search), |
|
|
|
|
|
new OrTerm( |
|
|
|
|
|
new SubjectTerm(search), |
|
|
|
|
|
new BodyTerm(search))))); |
|
|
|
|
|
Log.i(Helper.TAG, "Boundary found messages=" + imessages.length); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Boundary done"); |
|
|
int index = imessages.length - 1; |
|
|
} catch (final Throwable ex) { |
|
|
while (index >= 0) { |
|
|
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex)); |
|
|
if (imessages[index].getReceivedDate().getTime() < before) { |
|
|
mainHandler.post(new Runnable() { |
|
|
Log.i(Helper.TAG, "Boundary sync uid=" + ifolder.getUID(imessages[index])); |
|
|
@Override |
|
|
ServiceSynchronize.synchronizeMessage(context, folder, ifolder, (IMAPMessage) imessages[index], true); |
|
|
public void run() { |
|
|
break; |
|
|
intf.onError(ex); |
|
|
} |
|
|
} |
|
|
index--; |
|
|
}); |
|
|
|
|
|
} finally { |
|
|
|
|
|
mainHandler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
intf.onLoaded(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log.i(Helper.TAG, "Boundary done"); |
|
|
|
|
|
} catch (final Throwable ex) { |
|
|
|
|
|
Log.e(Helper.TAG, "Boundary " + ex + "\n" + Log.getStackTraceString(ex)); |
|
|
|
|
|
mainHandler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
intf.onError(ex); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} finally { |
|
|
|
|
|
mainHandler.post(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
intf.onLoaded(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}).start(); |
|
|
}); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |