1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-01-13 06:38:05 -05:00

reduce lots and lots of code duplication when calling out to listeners.

This commit is contained in:
Jesse Vincent 2010-07-13 21:17:10 +00:00
parent 986237d31f
commit e17707330f

View File

@ -276,14 +276,10 @@ public class MessagingController implements Runnable
Log.i(K9.LOG_TAG, (command.isForeground ? "Foreground" : "Background") + Log.i(K9.LOG_TAG, (command.isForeground ? "Foreground" : "Background") +
" Command '" + command.description + "' completed"); " Command '" + command.description + "' completed");
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(command.listener))
{ {
l.controllerCommandCompleted(mCommands.size() > 0); l.controllerCommandCompleted(mCommands.size() > 0);
} }
if (command.listener != null && !getListeners().contains(command.listener))
{
command.listener.controllerCommandCompleted(mCommands.size() > 0);
}
} }
} }
catch (Exception e) catch (Exception e)
@ -360,6 +356,19 @@ public class MessagingController implements Runnable
return mListeners; return mListeners;
} }
public Set<MessagingListener> getListeners(MessagingListener listener)
{
Set<MessagingListener> listeners = mListeners;
if (listener != null && getListeners().contains(listener) == false)
{
listeners.add(listener);
}
return listeners;
}
/** /**
* Lists folders that are available locally and remotely. This method calls * Lists folders that are available locally and remotely. This method calls
* listFoldersCallback for local folders before it returns, and then for * listFoldersCallback for local folders before it returns, and then for
@ -379,14 +388,10 @@ public class MessagingController implements Runnable
{ {
public void run() public void run()
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listFoldersStarted(account); l.listFoldersStarted(account);
} }
if (listener != null)
{
listener.listFoldersStarted(account);
}
List<? extends Folder> localFolders = null; List<? extends Folder> localFolders = null;
try try
{ {
@ -400,25 +405,19 @@ public class MessagingController implements Runnable
doRefreshRemote(account, listener); doRefreshRemote(account, listener);
return; return;
} }
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{ {
l.listFolders(account, folderArray); l.listFolders(account, folderArray);
} }
if (listener != null)
{
listener.listFolders(account, folderArray);
}
} }
catch (Exception e) catch (Exception e)
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listFoldersFailed(account, e.getMessage()); l.listFoldersFailed(account, e.getMessage());
} }
if (listener != null)
{
listener.listFoldersFailed(account, e.getMessage());
}
addErrorMessage(account, null, e); addErrorMessage(account, null, e);
return; return;
} }
@ -436,14 +435,10 @@ public class MessagingController implements Runnable
} }
} }
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listFoldersFinished(account); l.listFoldersFinished(account);
} }
if (listener != null)
{
listener.listFoldersFinished(account);
}
} }
}); });
} }
@ -566,16 +561,11 @@ public class MessagingController implements Runnable
public void listLocalMessagesSynchronous(final Account account, final String folder, final MessagingListener listener) public void listLocalMessagesSynchronous(final Account account, final String folder, final MessagingListener listener)
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listLocalMessagesStarted(account, folder); l.listLocalMessagesStarted(account, folder);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesStarted(account, folder);
}
Folder localFolder = null; Folder localFolder = null;
MessageRetrievalListener retrievalListener = MessageRetrievalListener retrievalListener =
new MessageRetrievalListener() new MessageRetrievalListener()
@ -602,15 +592,10 @@ public class MessagingController implements Runnable
} }
else else
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listLocalMessagesRemoveMessage(account, folder, message); l.listLocalMessagesRemoveMessage(account, folder, message);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesRemoveMessage(account, folder, message);
}
} }
} }
public void messagesFinished(int number) public void messagesFinished(int number)
@ -619,14 +604,10 @@ public class MessagingController implements Runnable
} }
private void addPendingMessages() private void addPendingMessages()
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listLocalMessagesAddMessages(account, folder, pendingMessages); l.listLocalMessagesAddMessages(account, folder, pendingMessages);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesAddMessages(account, folder, pendingMessages);
}
pendingMessages.clear(); pendingMessages.clear();
} }
}; };
@ -646,25 +627,17 @@ public class MessagingController implements Runnable
if (K9.DEBUG) if (K9.DEBUG)
Log.v(K9.LOG_TAG, "Got ack that callbackRunner finished"); Log.v(K9.LOG_TAG, "Got ack that callbackRunner finished");
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listLocalMessagesFinished(account, folder); l.listLocalMessagesFinished(account, folder);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesFinished(account, folder);
}
} }
catch (Exception e) catch (Exception e)
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.listLocalMessagesFailed(account, folder, e.getMessage()); l.listLocalMessagesFailed(account, folder, e.getMessage());
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesFailed(account, folder, e.getMessage());
}
addErrorMessage(account, null, e); addErrorMessage(account, null, e);
} }
finally finally
@ -974,27 +947,19 @@ public class MessagingController implements Runnable
if (K9.DEBUG) if (K9.DEBUG)
Log.i(K9.LOG_TAG, "Synchronizing folder " + account.getDescription() + ":" + folder); Log.i(K9.LOG_TAG, "Synchronizing folder " + account.getDescription() + ":" + folder);
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxStarted(account, folder); l.synchronizeMailboxStarted(account, folder);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxStarted(account, folder);
}
/* /*
* We don't ever sync the Outbox or errors folder * We don't ever sync the Outbox or errors folder
*/ */
if (folder.equals(account.getOutboxFolderName()) || folder.equals(account.getErrorFolderName())) if (folder.equals(account.getOutboxFolderName()) || folder.equals(account.getErrorFolderName()))
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxFinished(account, folder, 0, 0); l.synchronizeMailboxFinished(account, folder, 0, 0);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFinished(account, folder, 0, 0);
}
return; return;
} }
@ -1159,14 +1124,10 @@ public class MessagingController implements Runnable
{ {
localMessage.setFlag(Flag.X_DESTROYED, true); localMessage.setFlag(Flag.X_DESTROYED, true);
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxRemovedMessage(account, folder, localMessage); l.synchronizeMailboxRemovedMessage(account, folder, localMessage);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxRemovedMessage(account, folder, localMessage);
}
} }
} }
} }
@ -1198,14 +1159,10 @@ public class MessagingController implements Runnable
account.getDescription() + ":" + folder + " @ " + new Date() + account.getDescription() + ":" + folder + " @ " + new Date() +
" with " + newMessages + " new messages"); " with " + newMessages + " new messages");
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxFinished(account, folder, remoteMessageCount, newMessages); l.synchronizeMailboxFinished(account, folder, remoteMessageCount, newMessages);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFinished(account, folder, remoteMessageCount, newMessages);
}
if (commandException != null) if (commandException != null)
@ -1214,14 +1171,10 @@ public class MessagingController implements Runnable
Log.e(K9.LOG_TAG, "Root cause failure in " + account.getDescription() + ":" + Log.e(K9.LOG_TAG, "Root cause failure in " + account.getDescription() + ":" +
tLocalFolder.getName() + " was '" + rootMessage + "'"); tLocalFolder.getName() + " was '" + rootMessage + "'");
localFolder.setStatus(rootMessage); localFolder.setStatus(rootMessage);
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxFailed(account, folder, rootMessage); l.synchronizeMailboxFailed(account, folder, rootMessage);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFailed(account, folder, rootMessage);
}
} }
if (K9.DEBUG) if (K9.DEBUG)
@ -1248,20 +1201,13 @@ public class MessagingController implements Runnable
} }
} }
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxFailed( l.synchronizeMailboxFailed(
account, account,
folder, folder,
rootMessage); rootMessage);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFailed(
account,
folder,
rootMessage);
}
addErrorMessage(account, null, e); addErrorMessage(account, null, e);
Log.e(K9.LOG_TAG, "Failed synchronizing folder " + Log.e(K9.LOG_TAG, "Failed synchronizing folder " +
account.getDescription() + ":" + folder + " @ " + new Date()); account.getDescription() + ":" + folder + " @ " + new Date());
@ -1298,14 +1244,10 @@ public class MessagingController implements Runnable
{ {
if (!remoteFolder.create(FolderType.HOLDS_MESSAGES)) if (!remoteFolder.create(FolderType.HOLDS_MESSAGES))
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.synchronizeMailboxFinished(account, folder, 0, 0); l.synchronizeMailboxFinished(account, folder, 0, 0);
} }
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFinished(account, folder, 0, 0);
}
if (K9.DEBUG) if (K9.DEBUG)
Log.i(K9.LOG_TAG, "Done synchronizing folder " + folder); Log.i(K9.LOG_TAG, "Done synchronizing folder " + folder);
@ -3061,33 +3003,21 @@ public class MessagingController implements Runnable
message.setFlag(Flag.X_DOWNLOADED_FULL, true); message.setFlag(Flag.X_DOWNLOADED_FULL, true);
} }
if (listener != null && !getListeners().contains(listener)) for (MessagingListener l : getListeners(listener))
{
listener.loadMessageForViewBodyAvailable(account, folder, uid, message);
}
for (MessagingListener l : getListeners())
{ {
l.loadMessageForViewBodyAvailable(account, folder, uid, message); l.loadMessageForViewBodyAvailable(account, folder, uid, message);
} }
if (listener != null && !getListeners().contains(listener)) for (MessagingListener l : getListeners(listener))
{
listener.loadMessageForViewFinished(account, folder, uid, message);
}
for (MessagingListener l : getListeners())
{ {
l.loadMessageForViewFinished(account, folder, uid, message); l.loadMessageForViewFinished(account, folder, uid, message);
} }
} }
catch (Exception e) catch (Exception e)
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.loadMessageForViewFailed(account, folder, uid, e); l.loadMessageForViewFailed(account, folder, uid, e);
} }
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFailed(account, folder, uid, e);
}
addErrorMessage(account, null, e); addErrorMessage(account, null, e);
} }
@ -3110,14 +3040,10 @@ public class MessagingController implements Runnable
public void loadMessageForView(final Account account, final String folder, final String uid, public void loadMessageForView(final Account account, final String folder, final String uid,
final MessagingListener listener) final MessagingListener listener)
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.loadMessageForViewStarted(account, folder, uid); l.loadMessageForViewStarted(account, folder, uid);
} }
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewStarted(account, folder, uid);
}
threadPool.execute(new Runnable() threadPool.execute(new Runnable()
{ {
public void run() public void run()
@ -3141,14 +3067,10 @@ public class MessagingController implements Runnable
setFlag(new Message[] { message }, Flag.SEEN, true); setFlag(new Message[] { message }, Flag.SEEN, true);
} }
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.loadMessageForViewHeadersAvailable(account, folder, uid, message); l.loadMessageForViewHeadersAvailable(account, folder, uid, message);
} }
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewHeadersAvailable(account, folder, uid, message);
}
if (!message.isSet(Flag.X_DOWNLOADED_FULL)) if (!message.isSet(Flag.X_DOWNLOADED_FULL))
{ {
@ -3169,35 +3091,23 @@ public class MessagingController implements Runnable
}, fp, null); }, fp, null);
localFolder.close(); localFolder.close();
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.loadMessageForViewBodyAvailable(account, folder, uid, message); l.loadMessageForViewBodyAvailable(account, folder, uid, message);
} }
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewBodyAvailable(account, folder, uid, message);
}
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.loadMessageForViewFinished(account, folder, uid, message); l.loadMessageForViewFinished(account, folder, uid, message);
} }
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFinished(account, folder, uid, message);
}
} }
catch (Exception e) catch (Exception e)
{ {
for (MessagingListener l : getListeners()) for (MessagingListener l : getListeners(listener))
{ {
l.loadMessageForViewFailed(account, folder, uid, e); l.loadMessageForViewFailed(account, folder, uid, e);
} }
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFailed(account, folder, uid, e);
}
addErrorMessage(account, null, e); addErrorMessage(account, null, e);
} }