1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Refactored code to calculate number of unread messages some more

This commit is contained in:
cketti 2011-04-20 23:58:10 +02:00
parent 92f7c3a19f
commit 49308922d9

View File

@ -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<String> queryParam = new ArrayList<String>();
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);