From 49308922d99d622ca62bbaf0594184abe83efd5e Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 20 Apr 2011 23:58:10 +0200 Subject: [PATCH] Refactored code to calculate number of unread messages some more --- src/com/fsck/k9/mail/store/LocalStore.java | 80 ++++++++++++++-------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 7cbb94e8e..31d6c8987 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -506,42 +507,61 @@ public class LocalStore extends Store implements Serializable { public Integer doDbWork(final SQLiteDatabase db) { Cursor cursor = null; try { - String baseQuery = "SELECT SUM(unread_count), SUM(flagged_count) FROM folders WHERE ((name = ?) or ( name != ? AND name != ? AND name != ? AND name != ? AND name != ? )) "; + // Always count messages in the INBOX but exclude special folders and possibly + // more (depending on the folder display mode) + String baseQuery = "SELECT SUM(unread_count), SUM(flagged_count) " + + "FROM folders " + + "WHERE (name = ?)" + /* INBOX */ + " OR (" + + "name NOT IN (?, ?, ?, ?, ?)" + /* special folders */ + "%s)"; /* placeholder for additional constraints */ + List queryParam = new ArrayList(); queryParam.add(mAccount.getInboxFolderName()); - queryParam.add((mAccount.getTrashFolderName() != null) ? mAccount.getTrashFolderName() : ""); - queryParam.add((mAccount.getDraftsFolderName() != null) ? mAccount.getDraftsFolderName() : ""); - queryParam.add((mAccount.getSpamFolderName() != null) ? mAccount.getSpamFolderName() : ""); - queryParam.add((mAccount.getOutboxFolderName() != null) ? mAccount.getOutboxFolderName() : ""); - queryParam.add((mAccount.getSentFolderName() != null) ? mAccount.getSentFolderName() : ""); - if (displayMode == Account.FolderMode.NONE) { - queryParam.add(mAccount.getInboxFolderName()); - cursor = db.rawQuery(baseQuery + "AND (name = ? )", queryParam.toArray(new String[queryParam.size()])); - } else if (displayMode == Account.FolderMode.FIRST_CLASS) { - queryParam.add(mAccount.getInboxFolderName()); - queryParam.add(Folder.FolderClass.FIRST_CLASS.name()); - cursor = db.rawQuery(baseQuery + " AND ( name = ? OR display_class = ?)", queryParam.toArray(new String[queryParam.size()])); + queryParam.add((mAccount.getTrashFolderName() != null) ? + mAccount.getTrashFolderName() : ""); + queryParam.add((mAccount.getDraftsFolderName() != null) ? + mAccount.getDraftsFolderName() : ""); + queryParam.add((mAccount.getSpamFolderName() != null) ? + mAccount.getSpamFolderName() : ""); + queryParam.add((mAccount.getOutboxFolderName() != null) ? + mAccount.getOutboxFolderName() : ""); + queryParam.add((mAccount.getSentFolderName() != null) ? + mAccount.getSentFolderName() : ""); - } else if (displayMode == Account.FolderMode.FIRST_AND_SECOND_CLASS) { - queryParam.add(mAccount.getInboxFolderName()); - queryParam.add(Folder.FolderClass.FIRST_CLASS.name()); - queryParam.add(Folder.FolderClass.SECOND_CLASS.name()); - cursor = db.rawQuery(baseQuery + " AND ( name = ? OR display_class = ? OR display_class = ? )", queryParam.toArray(new String[queryParam.size()])); - - } else if (displayMode == Account.FolderMode.NOT_SECOND_CLASS) { - queryParam.add(mAccount.getInboxFolderName()); - queryParam.add(Folder.FolderClass.SECOND_CLASS.name()); - cursor = db.rawQuery(baseQuery + " AND ( name = ? OR display_class != ?)", queryParam.toArray(new String[queryParam.size()])); - } else if (displayMode == Account.FolderMode.ALL) { - cursor = db.rawQuery(baseQuery, (String [])queryParam.toArray()); - } else { - Log.e(K9.LOG_TAG, "asked to compute account statistics for an impossible folder mode " + displayMode); - stats.unreadMessageCount = 0; - stats.flaggedMessageCount = 0; - return null; + final String extraWhere; + switch (displayMode) { + case FIRST_CLASS: + // Count messages in the INBOX and non-special first class folders + extraWhere = " AND (display_class = ?)"; + queryParam.add(Folder.FolderClass.FIRST_CLASS.name()); + break; + case FIRST_AND_SECOND_CLASS: + // Count messages in the INBOX and non-special first and second class folders + extraWhere = " AND (display_class IN (?, ?))"; + queryParam.add(Folder.FolderClass.FIRST_CLASS.name()); + queryParam.add(Folder.FolderClass.SECOND_CLASS.name()); + break; + case NOT_SECOND_CLASS: + // Count messages in the INBOX and non-special non-second-class folders + extraWhere = " AND (display_class != ?)"; + queryParam.add(Folder.FolderClass.SECOND_CLASS.name()); + break; + case ALL: + // Count messages in the INBOX and non-special folders + extraWhere = ""; + break; + default: + Log.e(K9.LOG_TAG, "asked to compute account statistics for an impossible folder mode " + displayMode); + stats.unreadMessageCount = 0; + stats.flaggedMessageCount = 0; + return null; } + String query = String.format(Locale.US, baseQuery, extraWhere); + cursor = db.rawQuery(query, queryParam.toArray(EMPTY_STRING_ARRAY)); + cursor.moveToFirst(); stats.unreadMessageCount = cursor.getInt(0); stats.flaggedMessageCount = cursor.getInt(1);