Fixed IMAP event listener

pull/169/head
M66B 6 years ago
parent f8a7c1f42f
commit 1a596ea511

@ -339,7 +339,6 @@ public class ActivityEML extends ActivityBase {
try (MailService iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, false, true)) { try (MailService iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, false, true)) {
iservice.setPartialFetch(account.partial_fetch); iservice.setPartialFetch(account.partial_fetch);
iservice.setIgnoreBodyStructureSize(account.ignore_size); iservice.setIgnoreBodyStructureSize(account.ignore_size);
iservice.setSeparateStoreConnection();
iservice.connect(account); iservice.connect(account);
IMAPFolder ifolder = (IMAPFolder) iservice.getStore().getFolder(inbox.name); IMAPFolder ifolder = (IMAPFolder) iservice.getStore().getFolder(inbox.name);

@ -282,7 +282,6 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback<TupleMe
state.iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, false, debug); state.iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, false, debug);
state.iservice.setPartialFetch(account.partial_fetch); state.iservice.setPartialFetch(account.partial_fetch);
state.iservice.setIgnoreBodyStructureSize(account.ignore_size); state.iservice.setIgnoreBodyStructureSize(account.ignore_size);
state.iservice.setSeparateStoreConnection();
state.iservice.connect(account); state.iservice.connect(account);
Log.i("Boundary server opening folder=" + browsable.name); Log.i("Boundary server opening folder=" + browsable.name);

@ -34,6 +34,7 @@ import javax.mail.NoSuchProviderException;
import javax.mail.Service; import javax.mail.Service;
import javax.mail.Session; import javax.mail.Session;
import javax.mail.Store; import javax.mail.Store;
import javax.mail.event.StoreListener;
public class MailService implements AutoCloseable { public class MailService implements AutoCloseable {
private Context context; private Context context;
@ -43,6 +44,7 @@ public class MailService implements AutoCloseable {
private Properties properties; private Properties properties;
private Session isession; private Session isession;
private Service iservice; private Service iservice;
private StoreListener listener;
private ExecutorService executor = Helper.getBackgroundExecutor(0, "mail"); private ExecutorService executor = Helper.getBackgroundExecutor(0, "mail");
@ -123,6 +125,7 @@ public class MailService implements AutoCloseable {
properties.put("mail.imap.starttls.enable", "true"); properties.put("mail.imap.starttls.enable", "true");
properties.put("mail.imap.starttls.required", Boolean.toString(!insecure)); properties.put("mail.imap.starttls.required", Boolean.toString(!insecure));
properties.put("mail." + protocol + ".separatestoreconnection", "true");
properties.put("mail." + protocol + ".connectionpool.debug", "true"); properties.put("mail." + protocol + ".connectionpool.debug", "true");
properties.put("mail." + protocol + ".connectionpoolsize", "1"); properties.put("mail." + protocol + ".connectionpoolsize", "1");
properties.put("mail." + protocol + ".connectionpooltimeout", Integer.toString(POOL_TIMEOUT)); properties.put("mail." + protocol + ".connectionpooltimeout", Integer.toString(POOL_TIMEOUT));
@ -169,14 +172,14 @@ public class MailService implements AutoCloseable {
useip = enabled; useip = enabled;
} }
void setSeparateStoreConnection() {
properties.put("mail." + protocol + ".separatestoreconnection", "true");
}
void setLeaveOnServer(boolean keep) { void setLeaveOnServer(boolean keep) {
properties.put("mail." + protocol + ".rsetbeforequit", Boolean.toString(keep)); properties.put("mail." + protocol + ".rsetbeforequit", Boolean.toString(keep));
} }
void setListener(StoreListener listener) {
this.listener = listener;
}
public void connect(EntityAccount account) throws MessagingException { public void connect(EntityAccount account) throws MessagingException {
String password = connect(account.host, account.port, account.auth_type, account.user, account.password); String password = connect(account.host, account.port, account.auth_type, account.user, account.password);
if (password != null) { if (password != null) {
@ -265,6 +268,8 @@ public class MailService implements AutoCloseable {
} else if ("imap".equals(protocol) || "imaps".equals(protocol)) { } else if ("imap".equals(protocol) || "imaps".equals(protocol)) {
iservice = isession.getStore(protocol); iservice = isession.getStore(protocol);
if (listener != null)
((IMAPStore) iservice).addStoreListener(listener);
iservice.connect(host, port, user, password); iservice.connect(host, port, user, password);
// https://www.ietf.org/rfc/rfc2971.txt // https://www.ietf.org/rfc/rfc2971.txt

@ -714,6 +714,35 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
iservice.setIgnoreBodyStructureSize(account.ignore_size); iservice.setIgnoreBodyStructureSize(account.ignore_size);
if (account.protocol != EntityAccount.TYPE_IMAP) if (account.protocol != EntityAccount.TYPE_IMAP)
iservice.setLeaveOnServer(account.browse); iservice.setLeaveOnServer(account.browse);
iservice.setListener(new StoreListener() {
@Override
public void notification(StoreEvent e) {
if (e.getMessageType() == StoreEvent.NOTICE)
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + e.getMessage());
else
try {
wlFolder.acquire();
String message = e.getMessage();
Log.w(account.name + " alert: " + message);
EntityLog.log(
ServiceSynchronize.this, account.name + " " +
Log.formatThrowable(new Core.AlertException(message), false));
db.account().setAccountError(account.id, message);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("alert:" + account.id, 1,
Core.getNotificationError(
ServiceSynchronize.this, "warning", account.name,
new Core.AlertException(message))
.build());
state.error(null);
} finally {
wlFolder.release();
}
}
});
final Map<EntityFolder, IMAPFolder> mapFolders = new HashMap<>(); final Map<EntityFolder, IMAPFolder> mapFolders = new HashMap<>();
List<Thread> idlers = new ArrayList<>(); List<Thread> idlers = new ArrayList<>();
@ -766,37 +795,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
db.account().setAccountWarning(account.id, null); db.account().setAccountWarning(account.id, null);
EntityLog.log(this, account.name + " connected"); EntityLog.log(this, account.name + " connected");
// Listen for store events
iservice.getStore().addStoreListener(new StoreListener() {
@Override
public void notification(StoreEvent e) {
if (e.getMessageType() == StoreEvent.NOTICE)
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + e.getMessage());
else
try {
wlFolder.acquire();
String message = e.getMessage();
Log.w(account.name + " alert: " + message);
EntityLog.log(
ServiceSynchronize.this, account.name + " " +
Log.formatThrowable(new Core.AlertException(message), false));
db.account().setAccountError(account.id, message);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("alert:" + account.id, 1,
Core.getNotificationError(
ServiceSynchronize.this, "warning", account.name,
new Core.AlertException(message))
.build());
state.error(null);
} finally {
wlFolder.release();
}
}
});
// Listen for folder events // Listen for folder events
iservice.getStore().addFolderListener(new FolderAdapter() { iservice.getStore().addFolderListener(new FolderAdapter() {
@Override @Override

Loading…
Cancel
Save