diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 36a6e5bab..6f896ab68 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -1736,19 +1736,8 @@ public class MessagingController { * other command processes. This maintains the order of the commands. */ try { - if (command instanceof PendingAppend) { - processPendingAppend((PendingAppend) command, account); - } else if (command instanceof PendingSetFlag) { - processPendingSetFlag((PendingSetFlag) command, account); - } else if (command instanceof PendingMarkAllAsRead) { - processPendingMarkAllAsRead((PendingMarkAllAsRead) command, account); - } else if (command instanceof PendingMoveOrCopy) { - processPendingMoveOrCopy((PendingMoveOrCopy) command, account); - } else if (command instanceof PendingEmptyTrash) { - processPendingEmptyTrash((PendingEmptyTrash) command, account); - } else if (command instanceof PendingExpunge) { - processPendingExpunge((PendingExpunge) command, account); - } + command.execute(this, account); + localStore.removePendingCommand(command); if (K9.DEBUG) { Log.d(K9.LOG_TAG, "Done processing pending command '" + command + "'"); @@ -1789,7 +1778,7 @@ public class MessagingController { * created. * TODO update the local message UID instead of deleteing it */ - private void processPendingAppend(PendingAppend command, Account account) throws MessagingException { + void processPendingAppend(PendingAppend command, Account account) throws MessagingException { Folder remoteFolder = null; LocalFolder localFolder = null; try { @@ -1915,7 +1904,7 @@ public class MessagingController { if (account.getErrorFolderName().equals(srcFolder)) { return; } - PendingCommand command = MessagingControllerCommands.createMoveOrCopyBulk(srcFolder, destFolder, isCopy, uids); + PendingCommand command = PendingMoveOrCopy.create(srcFolder, destFolder, isCopy, uids); queuePendingCommand(account, command); } @@ -1927,12 +1916,12 @@ public class MessagingController { if (account.getErrorFolderName().equals(srcFolder)) { return; } - PendingCommand command = MessagingControllerCommands.createMoveOrCopyBulk(srcFolder, destFolder, isCopy, uidMap); + PendingCommand command = PendingMoveOrCopy.create(srcFolder, destFolder, isCopy, uidMap); queuePendingCommand(account, command); } } - private void processPendingMoveOrCopy(PendingMoveOrCopy command, Account account) throws MessagingException { + void processPendingMoveOrCopy(PendingMoveOrCopy command, Account account) throws MessagingException { Folder remoteSrcFolder = null; Folder remoteDestFolder = null; LocalFolder localDestFolder; @@ -2031,7 +2020,7 @@ public class MessagingController { putBackground("queueSetFlag " + account.getDescription() + ":" + folderName, null, new Runnable() { @Override public void run() { - PendingCommand command = MessagingControllerCommands.createSetFlag(folderName, newState, flag, uids); + PendingCommand command = PendingSetFlag.create(folderName, newState, flag, uids); queuePendingCommand(account, command); processPendingCommands(account); } @@ -2040,7 +2029,7 @@ public class MessagingController { /** * Processes a pending mark read or unread command. */ - private void processPendingSetFlag(PendingSetFlag command, Account account) throws MessagingException { + void processPendingSetFlag(PendingSetFlag command, Account account) throws MessagingException { String folder = command.folder; if (account.getErrorFolderName().equals(folder) || account.getOutboxFolderName().equals(folder)) { @@ -2081,13 +2070,13 @@ public class MessagingController { putBackground("queueExpunge " + account.getDescription() + ":" + folderName, null, new Runnable() { @Override public void run() { - PendingCommand command = MessagingControllerCommands.createExpunge(folderName); + PendingCommand command = PendingExpunge.create(folderName); queuePendingCommand(account, command); processPendingCommands(account); } }); } - private void processPendingExpunge(PendingExpunge command, Account account) throws MessagingException { + void processPendingExpunge(PendingExpunge command, Account account) throws MessagingException { String folder = command.folder; if (account.getErrorFolderName().equals(folder)) { @@ -2114,7 +2103,7 @@ public class MessagingController { } } - private void processPendingMarkAllAsRead(PendingMarkAllAsRead command, Account account) throws MessagingException { + void processPendingMarkAllAsRead(PendingMarkAllAsRead command, Account account) throws MessagingException { String folder = command.folder; Folder remoteFolder = null; LocalFolder localFolder = null; @@ -2238,7 +2227,7 @@ public class MessagingController { if (K9.DEBUG) { Log.i(K9.LOG_TAG, "Marking all messages in " + account.getDescription() + ":" + folder + " as read"); } - PendingCommand command = MessagingControllerCommands.createMarkAllAsRead(folder); + PendingCommand command = PendingMarkAllAsRead.create(folder); queuePendingCommand(account, command); processPendingCommands(account); } @@ -2821,7 +2810,7 @@ public class MessagingController { if (K9.DEBUG) Log.i(K9.LOG_TAG, "Moved sent message to folder '" + account.getSentFolderName() + "' (" + localSentFolder.getId() + ") "); - PendingCommand command = MessagingControllerCommands.createAppend(localSentFolder.getName(), message.getUid()); + PendingCommand command = PendingAppend.create(localSentFolder.getName(), message.getUid()); queuePendingCommand(account, command); processPendingCommands(account); } @@ -3420,7 +3409,7 @@ public class MessagingController { for (Message message : messages) { // If the message was in the Outbox, then it has been copied to local Trash, and has // to be copied to remote trash - PendingCommand command = MessagingControllerCommands.createAppend(account.getTrashFolderName(), message.getUid()); + PendingCommand command = PendingAppend.create(account.getTrashFolderName(), message.getUid()); queuePendingCommand(account, command); } processPendingCommands(account); @@ -3461,8 +3450,7 @@ public class MessagingController { return uids; } - @SuppressWarnings("UnusedParameters") // for consistency with other PendingCommand methods - private void processPendingEmptyTrash(PendingEmptyTrash command, Account account) throws MessagingException { + void processPendingEmptyTrash(Account account) throws MessagingException { Store remoteStore = account.getRemoteStore(); Folder remoteFolder = remoteStore.getFolder(account.getTrashFolderName()); @@ -3508,7 +3496,7 @@ public class MessagingController { } if (!isTrashLocalOnly) { - PendingCommand command = MessagingControllerCommands.createEmptyTrash(); + PendingCommand command = PendingEmptyTrash.create(); queuePendingCommand(account, command); processPendingCommands(account); } @@ -4001,7 +3989,7 @@ public class MessagingController { localMessage.setFlag(Flag.X_DOWNLOADED_FULL, true); if (saveRemotely) { - PendingCommand command = MessagingControllerCommands.createAppend(localFolder.getName(), localMessage.getUid()); + PendingCommand command = PendingAppend.create(localFolder.getName(), localMessage.getUid()); queuePendingCommand(account, command); processPendingCommands(account); } diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingControllerCommands.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingControllerCommands.java index b2d0fed66..fc1552a95 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingControllerCommands.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingControllerCommands.java @@ -4,7 +4,9 @@ package com.fsck.k9.controller; import java.util.List; import java.util.Map; +import com.fsck.k9.Account; import com.fsck.k9.mail.Flag; +import com.fsck.k9.mail.MessagingException; public class MessagingControllerCommands { @@ -16,41 +18,14 @@ public class MessagingControllerCommands { static final String COMMAND_EMPTY_TRASH = "empty_trash"; - public static PendingSetFlag createSetFlag(String folder, boolean newState, Flag flag, List uids) { - return new PendingSetFlag(folder, newState, flag, uids); - } - - public static PendingExpunge createExpunge(String folderName) { - return new PendingExpunge(folderName); - } - - public static PendingMarkAllAsRead createMarkAllAsRead(String folder) { - return new PendingMarkAllAsRead(folder); - } - - public static PendingAppend createAppend(String folderName, String uid) { - return new PendingAppend(folderName, uid); - } - - public static PendingEmptyTrash createEmptyTrash() { - return new PendingEmptyTrash(); - } - - public static PendingMoveOrCopy createMoveOrCopyBulk(String srcFolder, String destFolder, boolean isCopy, Map uidMap) { - return new PendingMoveOrCopy(srcFolder, destFolder, isCopy, null, uidMap); - } - - public static PendingMoveOrCopy createMoveOrCopyBulk(String srcFolder, String destFolder, boolean isCopy, List uids) { - return new PendingMoveOrCopy(srcFolder, destFolder, isCopy, uids, null); - } + public abstract static class PendingCommand { + public long databaseId; - public static abstract class PendingCommand { - public transient long databaseId; + PendingCommand() { } public abstract String getCommandName(); - - private PendingCommand() { } + public abstract void execute(MessagingController controller, Account account) throws MessagingException; } public static class PendingMoveOrCopy extends PendingCommand { @@ -60,8 +35,18 @@ public class MessagingControllerCommands { public final List uids; public final Map newUidMap; - public PendingMoveOrCopy( - String srcFolder, String destFolder, boolean isCopy, List uids, Map newUidMap) { + + public static PendingMoveOrCopy create(String srcFolder, String destFolder, boolean isCopy, + Map uidMap) { + return new PendingMoveOrCopy(srcFolder, destFolder, isCopy, null, uidMap); + } + + public static PendingMoveOrCopy create(String srcFolder, String destFolder, boolean isCopy, List uids) { + return new PendingMoveOrCopy(srcFolder, destFolder, isCopy, uids, null); + } + + private PendingMoveOrCopy(String srcFolder, String destFolder, boolean isCopy, List uids, + Map newUidMap) { this.srcFolder = srcFolder; this.destFolder = destFolder; this.isCopy = isCopy; @@ -69,15 +54,31 @@ public class MessagingControllerCommands { this.newUidMap = newUidMap; } + @Override public String getCommandName() { return COMMAND_MOVE_OR_COPY; } + + @Override + public void execute(MessagingController controller, Account account) throws MessagingException { + controller.processPendingMoveOrCopy(this, account); + } } public static class PendingEmptyTrash extends PendingCommand { + public static PendingEmptyTrash create() { + return new PendingEmptyTrash(); + } + + @Override public String getCommandName() { return COMMAND_EMPTY_TRASH; } + + @Override + public void execute(MessagingController controller, Account account) throws MessagingException { + controller.processPendingEmptyTrash(account); + } } public static class PendingSetFlag extends PendingCommand { @@ -86,53 +87,97 @@ public class MessagingControllerCommands { public final Flag flag; public final List uids; - public PendingSetFlag(String folder, boolean newState, Flag flag, List uids) { + + public static PendingSetFlag create(String folder, boolean newState, Flag flag, List uids) { + return new PendingSetFlag(folder, newState, flag, uids); + } + + private PendingSetFlag(String folder, boolean newState, Flag flag, List uids) { this.folder = folder; this.newState = newState; this.flag = flag; this.uids = uids; } + @Override public String getCommandName() { return COMMAND_SET_FLAG; } + + @Override + public void execute(MessagingController controller, Account account) throws MessagingException { + controller.processPendingSetFlag(this, account); + } } public static class PendingAppend extends PendingCommand { public final String folder; public final String uid; - public PendingAppend(String folder, String uid) { + + public static PendingAppend create(String folderName, String uid) { + return new PendingAppend(folderName, uid); + } + + private PendingAppend(String folder, String uid) { this.folder = folder; this.uid = uid; } + @Override public String getCommandName() { return COMMAND_APPEND; } + + @Override + public void execute(MessagingController controller, Account account) throws MessagingException { + controller.processPendingAppend(this, account); + } } public static class PendingMarkAllAsRead extends PendingCommand { public final String folder; - public PendingMarkAllAsRead(String folder) { + + public static PendingMarkAllAsRead create(String folder) { + return new PendingMarkAllAsRead(folder); + } + + private PendingMarkAllAsRead(String folder) { this.folder = folder; } + @Override public String getCommandName() { return COMMAND_MARK_ALL_AS_READ; } + + @Override + public void execute(MessagingController controller, Account account) throws MessagingException { + controller.processPendingMarkAllAsRead(this, account); + } } public static class PendingExpunge extends PendingCommand { public final String folder; - public PendingExpunge(String folder) { + + public static PendingExpunge create(String folderName) { + return new PendingExpunge(folderName); + } + + private PendingExpunge(String folder) { this.folder = folder; } + @Override public String getCommandName() { return COMMAND_EXPUNGE; } + + @Override + public void execute(MessagingController controller, Account account) throws MessagingException { + controller.processPendingExpunge(this, account); + } } } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo60.java b/k9mail/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo60.java index 42734d681..36e4c54ad 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo60.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/migrations/MigrationTo60.java @@ -13,8 +13,13 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.annotation.VisibleForTesting; -import com.fsck.k9.controller.MessagingControllerCommands; +import com.fsck.k9.controller.MessagingControllerCommands.PendingAppend; import com.fsck.k9.controller.MessagingControllerCommands.PendingCommand; +import com.fsck.k9.controller.MessagingControllerCommands.PendingEmptyTrash; +import com.fsck.k9.controller.MessagingControllerCommands.PendingExpunge; +import com.fsck.k9.controller.MessagingControllerCommands.PendingMarkAllAsRead; +import com.fsck.k9.controller.MessagingControllerCommands.PendingMoveOrCopy; +import com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag; import com.fsck.k9.controller.PendingCommandSerializer; import com.fsck.k9.helper.Utility; import com.fsck.k9.mail.Flag; @@ -112,11 +117,11 @@ class MigrationTo60 { private static PendingCommand migrateCommandExpunge(OldPendingCommand command) { String folder = command.arguments[0]; - return MessagingControllerCommands.createExpunge(folder); + return PendingExpunge.create(folder); } private static PendingCommand migrateCommandEmptyTrash() { - return MessagingControllerCommands.createEmptyTrash(); + return PendingEmptyTrash.create(); } private static PendingCommand migrateCommandMoveOrCopy(OldPendingCommand command) { @@ -125,7 +130,7 @@ class MigrationTo60 { String destFolder = command.arguments[2]; boolean isCopy = Boolean.parseBoolean(command.arguments[3]); - return MessagingControllerCommands.createMoveOrCopyBulk(srcFolder, destFolder, isCopy, singletonList(uid)); + return PendingMoveOrCopy.create(srcFolder, destFolder, isCopy, singletonList(uid)); } private static PendingCommand migrateCommandMoveOrCopyBulkNew(OldPendingCommand command) { @@ -141,12 +146,12 @@ class MigrationTo60 { uidMap.put(command.arguments[i], command.arguments[i + offset]); } - return MessagingControllerCommands.createMoveOrCopyBulk(srcFolder, destFolder, isCopy, uidMap); + return PendingMoveOrCopy.create(srcFolder, destFolder, isCopy, uidMap); } else { List uids = new ArrayList<>(command.arguments.length - 4); uids.addAll(Arrays.asList(command.arguments).subList(4, command.arguments.length)); - return MessagingControllerCommands.createMoveOrCopyBulk(srcFolder, destFolder, isCopy, uids); + return PendingMoveOrCopy.create(srcFolder, destFolder, isCopy, uids); } } @@ -166,7 +171,7 @@ class MigrationTo60 { } private static PendingCommand migrateCommandMarkAllAsRead(OldPendingCommand command) { - return MessagingControllerCommands.createMarkAllAsRead(command.arguments[0]); + return PendingMarkAllAsRead.create(command.arguments[0]); } private static PendingCommand migrateCommandSetFlag(OldPendingCommand command) { @@ -175,7 +180,7 @@ class MigrationTo60 { boolean newState = Boolean.parseBoolean(command.arguments[2]); Flag flag = Flag.valueOf(command.arguments[3]); - return MessagingControllerCommands.createSetFlag(folder, newState, flag, singletonList(uid)); + return PendingSetFlag.create(folder, newState, flag, singletonList(uid)); } private static PendingCommand migrateCommandSetFlagBulk(OldPendingCommand command) { @@ -186,13 +191,13 @@ class MigrationTo60 { List uids = new ArrayList<>(command.arguments.length - 3); uids.addAll(Arrays.asList(command.arguments).subList(3, command.arguments.length)); - return MessagingControllerCommands.createSetFlag(folder, newState, flag, uids); + return PendingSetFlag.create(folder, newState, flag, uids); } private static PendingCommand migrateCommandAppend(OldPendingCommand command) { String folder = command.arguments[0]; String uid = command.arguments[1]; - return MessagingControllerCommands.createAppend(folder, uid); + return PendingAppend.create(folder, uid); } private static List getPendingCommands(SQLiteDatabase db) { diff --git a/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java b/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java index eccdb8f54..56ba9be9a 100644 --- a/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java +++ b/k9mail/src/test/java/com/fsck/k9/controller/PendingCommandSerializerTest.java @@ -32,7 +32,7 @@ public class PendingCommandSerializerTest { @Test public void testSerializeDeserialize__withoutArguments() { - PendingCommand pendingCommand = MessagingControllerCommands.createEmptyTrash(); + PendingCommand pendingCommand = PendingEmptyTrash.create(); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); PendingEmptyTrash unserializedCommand = (PendingEmptyTrash) pendingCommandSerializer.unserialize( @@ -43,7 +43,7 @@ public class PendingCommandSerializerTest { @Test public void testSerializeDeserialize__withArguments() { - PendingCommand pendingCommand = MessagingControllerCommands.createAppend(SOURCE_FOLDER, UID); + PendingCommand pendingCommand = PendingAppend.create(SOURCE_FOLDER, UID); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); PendingAppend unserializedCommand = (PendingAppend) pendingCommandSerializer.unserialize( @@ -56,7 +56,7 @@ public class PendingCommandSerializerTest { @Test public void testSerializeDeserialize__withComplexArguments() { - PendingCommand pendingCommand = MessagingControllerCommands.createMoveOrCopyBulk( + PendingCommand pendingCommand = PendingMoveOrCopy.create( SOURCE_FOLDER, DEST_FOLDER, IS_COPY, UID_MAP); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); @@ -71,10 +71,10 @@ public class PendingCommandSerializerTest { @Test(expected = IllegalArgumentException.class) public void testDeserialize__withUnknownCommandName__shouldFail() { - PendingCommand pendingCommand = MessagingControllerCommands.createEmptyTrash(); + PendingCommand pendingCommand = PendingEmptyTrash.create(); String serializedCommand = pendingCommandSerializer.serialize(pendingCommand); pendingCommandSerializer.unserialize(DATABASE_ID, "BAD_COMMAND_NAME", serializedCommand); } -} \ No newline at end of file +}