diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java index 9e5d7b235..ca0334d6a 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapStore.java @@ -8,6 +8,8 @@ import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; +import java.util.ArrayList; +import java.util.Collection; import java.util.Deque; import java.util.EnumSet; import java.util.HashMap; @@ -24,7 +26,6 @@ import com.beetstra.jutf7.CharsetProvider; import com.fsck.k9.mail.AuthType; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.Flag; -import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.K9MailLib; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.NetworkType; @@ -107,7 +108,7 @@ public class ImapStore extends RemoteStore { } @Override - public Folder getFolder(String name) { + public ImapFolder getFolder(String name) { ImapFolder folder; synchronized (folderCache) { folder = folderCache.get(name); @@ -141,27 +142,21 @@ public class ImapStore extends RemoteStore { } @Override - public List getPersonalNamespaces(boolean forceListAll) throws MessagingException { + public List getPersonalNamespaces(boolean forceListAll) throws MessagingException { ImapConnection connection = getConnection(); try { - List allFolders = listFolders(connection, false); + Set folderNames = listFolders(connection, false); + if (forceListAll || !mStoreConfig.subscribedFoldersOnly()) { - return allFolders; - } else { - List resultFolders = new LinkedList<>(); - Set subscribedFolderNames = new HashSet<>(); - List subscribedFolders = listFolders(connection, true); - for (Folder subscribedFolder : subscribedFolders) { - subscribedFolderNames.add(subscribedFolder.getName()); - } - for (Folder folder : allFolders) { - if (subscribedFolderNames.contains(folder.getName())) { - resultFolders.add(folder); - } - } - return resultFolders; + return getFolders(folderNames); } + + Set subscribedFolders = listFolders(connection, true); + + folderNames.retainAll(subscribedFolders); + + return getFolders(folderNames); } catch (IOException | MessagingException ioe) { connection.close(); throw new MessagingException("Unable to get folder list.", ioe); @@ -170,12 +165,10 @@ public class ImapStore extends RemoteStore { } } - private List listFolders(ImapConnection connection, boolean subscribedOnly) throws IOException, + private Set listFolders(ImapConnection connection, boolean subscribedOnly) throws IOException, MessagingException { String commandResponse = subscribedOnly ? "LSUB" : "LIST"; - LinkedList folders = new LinkedList<>(); - List responses = connection.executeSimpleCommand(String.format("%s \"\" %s", commandResponse, ImapUtility.encodeString(getCombinedPrefix() + "*"))); @@ -183,6 +176,8 @@ public class ImapStore extends RemoteStore { List listResponses = (subscribedOnly) ? ListResponse.parseLsub(responses) : ListResponse.parseList(responses); + Set folderNames = new HashSet<>(listResponses.size()); + for (ListResponse listResponse : listResponses) { boolean includeFolder = true; @@ -234,13 +229,13 @@ public class ImapStore extends RemoteStore { } if (includeFolder) { - folders.add(getFolder(folder)); + folderNames.add(folder); } } - folders.add(getFolder(mStoreConfig.getInboxFolderName())); - return folders; + folderNames.add(mStoreConfig.getInboxFolderName()); + return folderNames; } void autoconfigureFolders(final ImapConnection connection) throws IOException, MessagingException { @@ -350,6 +345,17 @@ public class ImapStore extends RemoteStore { return new ImapConnection(new StoreImapSettings(), mTrustedSocketFactory, connectivityManager); } + private List getFolders(Collection folderNames) { + List folders = new ArrayList<>(folderNames.size()); + + for (String folderName : folderNames) { + ImapFolder imapFolder = getFolder(folderName); + folders.add(imapFolder); + } + + return folders; + } + String encodeFolderName(String name) { ByteBuffer bb = modifiedUtf7Charset.encode(name); byte[] b = new byte[bb.limit()];