1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-02 00:25:10 -04: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") +
" Command '" + command.description + "' completed");
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(command.listener))
{
l.controllerCommandCompleted(mCommands.size() > 0);
}
if (command.listener != null && !getListeners().contains(command.listener))
{
command.listener.controllerCommandCompleted(mCommands.size() > 0);
}
}
}
catch (Exception e)
@ -360,6 +356,19 @@ public class MessagingController implements Runnable
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
* listFoldersCallback for local folders before it returns, and then for
@ -379,14 +388,10 @@ public class MessagingController implements Runnable
{
public void run()
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listFoldersStarted(account);
}
if (listener != null)
{
listener.listFoldersStarted(account);
}
List<? extends Folder> localFolders = null;
try
{
@ -400,25 +405,19 @@ public class MessagingController implements Runnable
doRefreshRemote(account, listener);
return;
}
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listFolders(account, folderArray);
}
if (listener != null)
{
listener.listFolders(account, folderArray);
}
}
catch (Exception e)
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listFoldersFailed(account, e.getMessage());
}
if (listener != null)
{
listener.listFoldersFailed(account, e.getMessage());
}
addErrorMessage(account, null, e);
return;
}
@ -436,14 +435,10 @@ public class MessagingController implements Runnable
}
}
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
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)
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listLocalMessagesStarted(account, folder);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesStarted(account, folder);
}
Folder localFolder = null;
MessageRetrievalListener retrievalListener =
new MessageRetrievalListener()
@ -602,15 +592,10 @@ public class MessagingController implements Runnable
}
else
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listLocalMessagesRemoveMessage(account, folder, message);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesRemoveMessage(account, folder, message);
}
}
}
public void messagesFinished(int number)
@ -619,14 +604,10 @@ public class MessagingController implements Runnable
}
private void addPendingMessages()
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listLocalMessagesAddMessages(account, folder, pendingMessages);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesAddMessages(account, folder, pendingMessages);
}
pendingMessages.clear();
}
};
@ -646,25 +627,17 @@ public class MessagingController implements Runnable
if (K9.DEBUG)
Log.v(K9.LOG_TAG, "Got ack that callbackRunner finished");
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listLocalMessagesFinished(account, folder);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesFinished(account, folder);
}
}
catch (Exception e)
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.listLocalMessagesFailed(account, folder, e.getMessage());
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.listLocalMessagesFailed(account, folder, e.getMessage());
}
addErrorMessage(account, null, e);
}
finally
@ -974,27 +947,19 @@ public class MessagingController implements Runnable
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "Synchronizing folder " + account.getDescription() + ":" + folder);
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
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
*/
if (folder.equals(account.getOutboxFolderName()) || folder.equals(account.getErrorFolderName()))
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.synchronizeMailboxFinished(account, folder, 0, 0);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFinished(account, folder, 0, 0);
}
return;
}
@ -1159,14 +1124,10 @@ public class MessagingController implements Runnable
{
localMessage.setFlag(Flag.X_DESTROYED, true);
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
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() +
" with " + newMessages + " new messages");
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.synchronizeMailboxFinished(account, folder, remoteMessageCount, newMessages);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFinished(account, folder, remoteMessageCount, newMessages);
}
if (commandException != null)
@ -1214,14 +1171,10 @@ public class MessagingController implements Runnable
Log.e(K9.LOG_TAG, "Root cause failure in " + account.getDescription() + ":" +
tLocalFolder.getName() + " was '" + rootMessage + "'");
localFolder.setStatus(rootMessage);
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.synchronizeMailboxFailed(account, folder, rootMessage);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFailed(account, folder, rootMessage);
}
}
if (K9.DEBUG)
@ -1248,20 +1201,13 @@ public class MessagingController implements Runnable
}
}
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.synchronizeMailboxFailed(
account,
folder,
rootMessage);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFailed(
account,
folder,
rootMessage);
}
addErrorMessage(account, null, e);
Log.e(K9.LOG_TAG, "Failed synchronizing folder " +
account.getDescription() + ":" + folder + " @ " + new Date());
@ -1298,14 +1244,10 @@ public class MessagingController implements Runnable
{
if (!remoteFolder.create(FolderType.HOLDS_MESSAGES))
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.synchronizeMailboxFinished(account, folder, 0, 0);
}
if (listener != null && getListeners().contains(listener) == false)
{
listener.synchronizeMailboxFinished(account, folder, 0, 0);
}
if (K9.DEBUG)
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);
}
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewBodyAvailable(account, folder, uid, message);
}
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.loadMessageForViewBodyAvailable(account, folder, uid, message);
}
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFinished(account, folder, uid, message);
}
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.loadMessageForViewFinished(account, folder, uid, message);
}
}
catch (Exception e)
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.loadMessageForViewFailed(account, folder, uid, e);
}
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFailed(account, folder, uid, 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,
final MessagingListener listener)
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.loadMessageForViewStarted(account, folder, uid);
}
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewStarted(account, folder, uid);
}
threadPool.execute(new Runnable()
{
public void run()
@ -3141,14 +3067,10 @@ public class MessagingController implements Runnable
setFlag(new Message[] { message }, Flag.SEEN, true);
}
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
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))
{
@ -3169,35 +3091,23 @@ public class MessagingController implements Runnable
}, fp, null);
localFolder.close();
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
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);
}
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFinished(account, folder, uid, message);
}
}
catch (Exception e)
{
for (MessagingListener l : getListeners())
for (MessagingListener l : getListeners(listener))
{
l.loadMessageForViewFailed(account, folder, uid, e);
}
if (listener != null && !getListeners().contains(listener))
{
listener.loadMessageForViewFailed(account, folder, uid, e);
}
addErrorMessage(account, null, e);
}