From 70798a7892079a71bed4e2c9833b29caaa725cb5 Mon Sep 17 00:00:00 2001 From: Mihai Preda Date: Thu, 4 Jun 2009 11:38:26 -0700 Subject: [PATCH] AI 149573: Display To: instead of From: in Drafts folder && Display unread message count for inbox, total message count everywhere else. Integrates from imode email: 148948,149107,149247,149248,149249,149287. BUG=1591391,1842520,1860085,1459866,1860250 Automated import of CL 149573 --- .../email/activity/FolderMessageList.java | 46 ++++++++--- .../android/email/mail/store/LocalStore.java | 79 ++++++++++++------- .../email/mail/store/LocalStoreUnitTests.java | 64 +++++++++++++++ 3 files changed, 153 insertions(+), 36 deletions(-) diff --git a/src/com/android/email/activity/FolderMessageList.java b/src/com/android/email/activity/FolderMessageList.java index 4bd55530d..45f736ba3 100644 --- a/src/com/android/email/activity/FolderMessageList.java +++ b/src/com/android/email/activity/FolderMessageList.java @@ -33,6 +33,7 @@ import com.android.email.mail.Message; import com.android.email.mail.MessagingException; import com.android.email.mail.Message.RecipientType; import com.android.email.mail.store.LocalStore; +import com.android.email.mail.store.LocalStore.LocalFolder; import com.android.email.mail.store.LocalStore.LocalMessage; import android.app.ExpandableListActivity; @@ -125,6 +126,8 @@ public class FolderMessageList extends ExpandableListActivity { R.drawable.appointment_indicator_leftside_20, R.drawable.appointment_indicator_leftside_21, }; + + private static final Flag[] DELETED_FLAG = new Flag[] { Flag.DELETED }; private ExpandableListView mListView; private int colorChipResId; @@ -667,7 +670,7 @@ public class FolderMessageList extends ExpandableListActivity { mHandler.dataChanged(); } - if (folder.name.equals(mAccount.getDraftsFolderName())) { + if (folder.drafts) { MessageCompose.actionEditDraft(this, mAccount, message.message); } else { @@ -1032,6 +1035,27 @@ public class FolderMessageList extends ExpandableListActivity { mAttachmentIcon = getResources().getDrawable(R.drawable.ic_mms_attachment_small); } + /* + * Set special folder boolean indicator in FolderInfoHolder + */ + private void setSpecialFolderIndicator(FolderInfoHolder holder) { + String folderName = holder.name; + holder.special = true; + if (folderName.equalsIgnoreCase(Email.INBOX)) { + holder.inbox = true; + } else if (folderName.equals(mAccount.getDraftsFolderName())) { + holder.drafts = true; + } else if (folderName.equals(mAccount.getOutboxFolderName())) { + holder.outbox = true; + } else if (folderName.equals(mAccount.getSentFolderName())) { + holder.sent = true; + } else if (folderName.equals(mAccount.getTrashFolderName())) { + holder.trash = true; + } else { + holder.special = false; + } + } + /** * This code is invoked (in the UI thread) in response to a listFolders() callback * into the MessageListener. @@ -1050,15 +1074,14 @@ public class FolderMessageList extends ExpandableListActivity { else { holder.displayName = folder.getName(); } - if (holder.name.equals(mAccount.getOutboxFolderName())) { - holder.outbox = true; - } + setSpecialFolderIndicator(holder); if (holder.messages == null) { holder.messages = new ArrayList(); } try { folder.open(Folder.OpenMode.READ_WRITE, null); - holder.messageCount = folder.getMessageCount(); + holder.messageCount = ((LocalFolder) folder) + .getMessageCount(null, DELETED_FLAG); holder.unreadMessageCount = folder.getUnreadMessageCount(); folder.close(false); } @@ -1230,11 +1253,11 @@ public class FolderMessageList extends ExpandableListActivity { holder.folderStatus.setVisibility(View.VISIBLE); } - if (!folder.outbox && folder.unreadMessageCount != 0) { + if ((folder.inbox || !folder.special) && folder.unreadMessageCount != 0) { holder.newMessageCount.setText(Integer.toString(folder.unreadMessageCount)); holder.newMessageCount.setVisibility(View.VISIBLE); } - else if (folder.outbox && folder.messageCount > 0) { + else if ((folder.outbox || folder.drafts) && folder.messageCount > 0) { holder.newMessageCount.setText(Integer.toString(folder.messageCount)); holder.newMessageCount.setVisibility(View.VISIBLE); } @@ -1393,9 +1416,14 @@ public class FolderMessageList extends ExpandableListActivity { public boolean lastCheckFailed; /** - * Outbox is handled differently from any other folder. + * Special folder indicator. */ + public boolean special; // One of the following five folders. + public boolean inbox; + public boolean drafts; public boolean outbox; + public boolean sent; + public boolean trash; public int compareTo(FolderInfoHolder o) { String s1 = this.name; @@ -1445,7 +1473,7 @@ public class FolderMessageList extends ExpandableListActivity { } this.hasAttachments = message.getAttachmentCount() > 0; this.read = message.isSet(Flag.SEEN); - if (folder.outbox) { + if (folder.outbox || folder.drafts || folder.sent) { this.sender = Address.toFriendly( message.getRecipients(RecipientType.TO)); } diff --git a/src/com/android/email/mail/store/LocalStore.java b/src/com/android/email/mail/store/LocalStore.java index 026e0cf4e..be2651835 100644 --- a/src/com/android/email/mail/store/LocalStore.java +++ b/src/com/android/email/mail/store/LocalStore.java @@ -651,10 +651,28 @@ public class LocalStore extends Store implements PersistentDataCallbacks { @Override public int getMessageCount() throws MessagingException { + return getMessageCount(null, null); + } + + /** + * Return number of messages based on the state of the flags. + * + * @param setFlags The flags that should be set for a message to be selected (null ok) + * @param clearFlags The flags that should be clear for a message to be selected (null ok) + * @return The number of messages matching the desired flag states. + * @throws MessagingException + */ + public int getMessageCount(Flag[] setFlags, Flag[] clearFlags) throws MessagingException { + // Generate WHERE clause based on flags observed + StringBuilder sql = new StringBuilder("SELECT COUNT(*) FROM messages WHERE "); + buildFlagPredicates(sql, setFlags, clearFlags); + sql.append("messages.folder_id = ?"); + open(OpenMode.READ_WRITE); Cursor cursor = null; try { - cursor = mDb.rawQuery("SELECT COUNT(*) FROM messages WHERE messages.folder_id = ?", + cursor = mDb.rawQuery( + sql.toString(), new String[] { Long.toString(mFolderId) }); @@ -1007,6 +1025,39 @@ public class LocalStore extends Store implements PersistentDataCallbacks { "SELECT " + POPULATE_MESSAGE_SELECT_COLUMNS + " FROM messages" + " WHERE "); + buildFlagPredicates(sql, setFlags, clearFlags); + sql.append("folder_id = ?"); + + open(OpenMode.READ_WRITE); + ArrayList messages = new ArrayList(); + + Cursor cursor = null; + try { + cursor = mDb.rawQuery( + sql.toString(), + new String[] { + Long.toString(mFolderId) + }); + + while (cursor.moveToNext()) { + LocalMessage message = new LocalMessage(null, this); + populateMessageFromGetMessageCursor(message, cursor); + messages.add(message); + } + } finally { + if (cursor != null) { + cursor.close(); + } + } + + return messages.toArray(new Message[] {}); + } + + /* + * Build SQL where predicates expression from set and clear flag arrays. + */ + private void buildFlagPredicates(StringBuilder sql, Flag[] setFlags, Flag[] clearFlags) + throws MessagingException { if (setFlags != null) { for (Flag flag : setFlags) { if (flag == Flag.X_STORE_1) { @@ -1041,32 +1092,6 @@ public class LocalStore extends Store implements PersistentDataCallbacks { } } } - sql.append("folder_id = ?"); - - open(OpenMode.READ_WRITE); - ArrayList messages = new ArrayList(); - - Cursor cursor = null; - try { - cursor = mDb.rawQuery( - sql.toString(), - new String[] { - Long.toString(mFolderId) - }); - - while (cursor.moveToNext()) { - LocalMessage message = new LocalMessage(null, this); - populateMessageFromGetMessageCursor(message, cursor); - messages.add(message); - } - } - finally { - if (cursor != null) { - cursor.close(); - } - } - - return messages.toArray(new Message[] {}); } @Override diff --git a/tests/src/com/android/email/mail/store/LocalStoreUnitTests.java b/tests/src/com/android/email/mail/store/LocalStoreUnitTests.java index 2dc1b9a5c..2db359805 100644 --- a/tests/src/com/android/email/mail/store/LocalStoreUnitTests.java +++ b/tests/src/com/android/email/mail/store/LocalStoreUnitTests.java @@ -689,6 +689,70 @@ public class LocalStoreUnitTests extends AndroidTestCase { assertEquals(failMessage, expectedUids, actualUids); } + /** + * Test for getMessageCount + */ + public void testMessageCount() throws MessagingException { + + final MimeMessage message1 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message1.setFlag(Flag.X_STORE_1, false); + message1.setFlag(Flag.X_STORE_2, false); + message1.setFlag(Flag.X_DOWNLOADED_FULL, true); + + final MimeMessage message2 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message2.setFlag(Flag.X_STORE_1, true); + message2.setFlag(Flag.X_STORE_2, false); + + final MimeMessage message3 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message3.setFlag(Flag.X_STORE_1, false); + message3.setFlag(Flag.X_STORE_2, true); + message3.setFlag(Flag.X_DOWNLOADED_FULL, true); + + final MimeMessage message4 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message4.setFlag(Flag.X_STORE_1, true); + message4.setFlag(Flag.X_STORE_2, true); + message4.setFlag(Flag.X_DOWNLOADED_FULL, true); + + final MimeMessage message5 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message5.setFlag(Flag.X_DOWNLOADED_FULL, true); + + final MimeMessage message6 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message6.setFlag(Flag.X_DOWNLOADED_PARTIAL, true); + + final MimeMessage message7 = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY); + message7.setFlag(Flag.DELETED, true); + + Message[] allOriginals = new Message[] { + message1, message2, message3, message4, message5, message6, message7 }; + + mFolder.open(OpenMode.READ_WRITE, null); + mFolder.appendMessages(allOriginals); + mFolder.close(false); + + // Null lists are the same as empty lists - return all messages + mFolder.open(OpenMode.READ_WRITE, null); + + int allMessages = mFolder.getMessageCount(); + assertEquals("all messages", 7, allMessages); + + int storeFlag1 = mFolder.getMessageCount(new Flag[] { Flag.X_STORE_1 }, null); + assertEquals("store flag 1", 2, storeFlag1); + + int storeFlag1NotFlag2 = mFolder.getMessageCount( + new Flag[] { Flag.X_STORE_1 }, new Flag[] { Flag.X_STORE_2 }); + assertEquals("store flag 1, not 2", 1, storeFlag1NotFlag2); + + int downloadedFull = mFolder.getMessageCount(new Flag[] { Flag.X_DOWNLOADED_FULL }, null); + assertEquals("downloaded full", 4, downloadedFull); + + int storeFlag2Full = mFolder.getMessageCount( + new Flag[] { Flag.X_STORE_2, Flag.X_DOWNLOADED_FULL }, null); + assertEquals("store flag 2, full", 2, storeFlag2Full); + + int notDeleted = mFolder.getMessageCount(null, new Flag[] { Flag.DELETED }); + assertEquals("not deleted", 6, notDeleted); + } + /** * Test unread messages count */