@Override public void listFolders(Account account, Folder[] folders) { if (account.equals(mAccount)) { List<FolderInfoHolder> newFolders = new LinkedList<FolderInfoHolder>(); List<FolderInfoHolder> topFolders = new LinkedList<FolderInfoHolder>(); Account.FolderMode aMode = account.getFolderDisplayMode(); Preferences prefs = Preferences.getPreferences(getApplication().getApplicationContext()); for (Folder folder : folders) { try { folder.refresh(prefs); Folder.FolderClass fMode = folder.getDisplayClass(); if ((aMode == Account.FolderMode.FIRST_CLASS && fMode != Folder.FolderClass.FIRST_CLASS) || (aMode == Account.FolderMode.FIRST_AND_SECOND_CLASS && fMode != Folder.FolderClass.FIRST_CLASS && fMode != Folder.FolderClass.SECOND_CLASS) || (aMode == Account.FolderMode.NOT_SECOND_CLASS && fMode == Folder.FolderClass.SECOND_CLASS)) { continue; } } catch (MessagingException me) { Log.e( K9.LOG_TAG, "Couldn't get prefs to check for displayability of folder " + folder.getName(), me); } FolderInfoHolder holder = null; int folderIndex = getFolderIndex(folder.getName()); if (folderIndex >= 0) { holder = (FolderInfoHolder) getItem(folderIndex); } int unreadMessageCount = 0; try { unreadMessageCount = folder.getUnreadMessageCount(); } catch (Exception e) { Log.e( K9.LOG_TAG, "Unable to get unreadMessageCount for " + mAccount.getDescription() + ":" + folder.getName()); } if (holder == null) { holder = new FolderInfoHolder(context, folder, mAccount, unreadMessageCount); } else { holder.populate(context, folder, mAccount, unreadMessageCount); } if (folder.isInTopGroup()) { topFolders.add(holder); } else { newFolders.add(holder); } } Collections.sort(newFolders); Collections.sort(topFolders); topFolders.addAll(newFolders); mHandler.newFolders(topFolders); } super.listFolders(account, folders); }
@Override public void listFolders(Account account, Folder[] folders) { if (!account.equals(mAccount)) { return; } Account.FolderMode aMode = mMode; Preferences prefs = Preferences.getPreferences(getApplication().getApplicationContext()); ArrayList<String> localFolders = new ArrayList<String>(); for (Folder folder : folders) { String name = folder.getName(); // Inbox needs to be compared case-insensitively if (mHideCurrentFolder && (name.equals(mFolder) || (mAccount.getInboxFolderName().equalsIgnoreCase(mFolder) && mAccount.getInboxFolderName().equalsIgnoreCase(name)))) { continue; } try { folder.refresh(prefs); Folder.FolderClass fMode = folder.getDisplayClass(); if ((aMode == Account.FolderMode.FIRST_CLASS && fMode != Folder.FolderClass.FIRST_CLASS) || (aMode == Account.FolderMode.FIRST_AND_SECOND_CLASS && fMode != Folder.FolderClass.FIRST_CLASS && fMode != Folder.FolderClass.SECOND_CLASS) || (aMode == Account.FolderMode.NOT_SECOND_CLASS && fMode == Folder.FolderClass.SECOND_CLASS)) { continue; } } catch (MessagingException me) { Log.e( K9.LOG_TAG, "Couldn't get prefs to check for displayability of folder " + folder.getName(), me); } localFolders.add(folder.getName()); } if (mShowOptionNone) { localFolders.add(K9.FOLDER_NONE); } Collections.sort( localFolders, new Comparator<String>() { @Override public int compare(String aName, String bName) { if (K9.FOLDER_NONE.equalsIgnoreCase(aName)) { return -1; } if (K9.FOLDER_NONE.equalsIgnoreCase(bName)) { return 1; } if (mAccount.getInboxFolderName().equalsIgnoreCase(aName)) { return -1; } if (mAccount.getInboxFolderName().equalsIgnoreCase(bName)) { return 1; } return aName.compareToIgnoreCase(bName); } }); int selectedFolder = -1; /* * We're not allowed to change the adapter from a background thread, so we collect the * folder names and update the adapter in the UI thread (see finally block). */ final List<String> folderList = new ArrayList<String>(); try { int position = 0; for (String name : localFolders) { if (mAccount.getInboxFolderName().equalsIgnoreCase(name)) { folderList.add(getString(R.string.special_mailbox_name_inbox)); mHeldInbox = name; } else if (!K9.ERROR_FOLDER_NAME.equals(name) && !account.getOutboxFolderName().equals(name)) { folderList.add(name); } if (mSelectFolder != null) { /* * Never select EXTRA_CUR_FOLDER (mFolder) if EXTRA_SEL_FOLDER * (mSelectedFolder) was provided. */ if (name.equals(mSelectFolder)) { selectedFolder = position; } } else if (name.equals(mFolder) || (mAccount.getInboxFolderName().equalsIgnoreCase(mFolder) && mAccount.getInboxFolderName().equalsIgnoreCase(name))) { selectedFolder = position; } position++; } } finally { runOnUiThread( new Runnable() { @Override public void run() { // Now we're in the UI-thread, we can safely change the contents of the adapter. mAdapter.clear(); for (String folderName : folderList) { mAdapter.add(folderName); } mAdapter.notifyDataSetChanged(); /* * Only enable the text filter after the list has been * populated to avoid possible race conditions because our * FolderListFilter isn't really thread-safe. */ getListView().setTextFilterEnabled(true); } }); } if (selectedFolder != -1) { mHandler.setSelectedFolder(selectedFolder); } }