From 8de5bda81594182757c1aa94a65cfb8c7b360b34 Mon Sep 17 00:00:00 2001 From: Makoto Onuki Date: Wed, 29 Jun 2011 18:46:23 -0700 Subject: [PATCH] Close cursor properly. Also, don't issue separate query()s for each recent mailbox. Bug 4977956 Change-Id: I3ccd437a7efd5c3599c4a1952ba091a7b3b815bf --- .../android/emailcommon/utility/Utility.java | 23 ++++++++++++++ .../activity/AccountSelectorAdapter.java | 30 +++++++++++-------- .../email/activity/RecentMailboxManager.java | 4 ++- .../emailcommon/utility/UtilityUnitTests.java | 16 ++++++++++ 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/emailcommon/src/com/android/emailcommon/utility/Utility.java b/emailcommon/src/com/android/emailcommon/utility/Utility.java index b65b1e66f..f1c3906ef 100644 --- a/emailcommon/src/com/android/emailcommon/utility/Utility.java +++ b/emailcommon/src/com/android/emailcommon/utility/Utility.java @@ -1124,4 +1124,27 @@ public class Utility { f.dump("", new FileDescriptor(), w, new String[0]); return sw.toString(); } + + /** + * Builds an "in" expression for SQLite. + * + * e.g. "ID" + 1,2,3 -> "ID in (1,2,3)". If {@code values} is empty or null, it returns an + * empty string. + */ + public static String buildInSelection(String columnName, Collection values) { + if ((values == null) || (values.size() == 0)) { + return ""; + } + StringBuilder sb = new StringBuilder(); + sb.append(columnName); + sb.append(" in ("); + String sep = ""; + for (Number n : values) { + sb.append(sep); + sb.append(n.toString()); + sep = ","; + } + sb.append(')'); + return sb.toString(); + } } diff --git a/src/com/android/email/activity/AccountSelectorAdapter.java b/src/com/android/email/activity/AccountSelectorAdapter.java index 047a3fb53..ea5a5ff5e 100644 --- a/src/com/android/email/activity/AccountSelectorAdapter.java +++ b/src/com/android/email/activity/AccountSelectorAdapter.java @@ -31,6 +31,7 @@ import com.android.emailcommon.provider.Mailbox; import com.android.emailcommon.utility.Utility; import java.util.ArrayList; +import java.util.Collection; import android.content.ContentResolver; import android.content.ContentUris; @@ -385,7 +386,7 @@ public class AccountSelectorAdapter extends CursorAdapter { // Do not display recent mailboxes in the account spinner for the two pane view recentMailboxes = mailboxManager.getMostRecent(mAccountId, mUseTwoPane); } - int recentCount = (recentMailboxes == null) ? 0 : recentMailboxes.size(); + final int recentCount = (recentMailboxes == null) ? 0 : recentMailboxes.size(); matrixCursor.mRecentCount = recentCount; if (!mUseTwoPane) { @@ -395,9 +396,7 @@ public class AccountSelectorAdapter extends CursorAdapter { } if (recentCount > 0) { - for (long mailboxId : recentMailboxes) { - addMailboxRow(matrixCursor, accountPosition, mailboxId); - } + addMailboxRows(matrixCursor, accountPosition, recentMailboxes); } if (!mUseTwoPane) { @@ -418,16 +417,23 @@ public class AccountSelectorAdapter extends CursorAdapter { MailboxColumns.UNREAD_COUNT, MailboxColumns.MESSAGE_COUNT }; - private void addMailboxRow(MatrixCursor matrixCursor, int accountPosition, long mailboxId) { + private void addMailboxRows(MatrixCursor matrixCursor, int accountPosition, + Collection mailboxIds) { Cursor c = mContext.getContentResolver().query( - ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId), - RECENT_MAILBOX_INFO_PROJECTION, null, null, null); - if (!c.moveToFirst()) { - return; + Mailbox.CONTENT_URI, RECENT_MAILBOX_INFO_PROJECTION, + Utility.buildInSelection(MailboxColumns.ID, mailboxIds), null, + RecentMailboxManager.RECENT_MAILBOXES_SORT_ORDER); + try { + c.moveToPosition(-1); + while (c.moveToNext()) { + addRow(matrixCursor, ROW_TYPE_MAILBOX, + c.getLong(c.getColumnIndex(MailboxColumns.ID)), + mFolderProperties.getDisplayName(c), null, + mFolderProperties.getMessageCount(c), accountPosition, mAccountId); + } + } finally { + c.close(); } - addRow(matrixCursor, ROW_TYPE_MAILBOX, mailboxId, - mFolderProperties.getDisplayName(c), null, - mFolderProperties.getMessageCount(c), accountPosition, mAccountId); } private void addHeaderRow(MatrixCursor cursor, String name) { diff --git a/src/com/android/email/activity/RecentMailboxManager.java b/src/com/android/email/activity/RecentMailboxManager.java index 110aaf4e0..dabfac50f 100644 --- a/src/com/android/email/activity/RecentMailboxManager.java +++ b/src/com/android/email/activity/RecentMailboxManager.java @@ -39,6 +39,8 @@ public class RecentMailboxManager { @VisibleForTesting static RecentMailboxManager sInstance; + public static String RECENT_MAILBOXES_SORT_ORDER = MailboxColumns.DISPLAY_NAME; + /** The maximum number of results to retrieve */ private static final int LIMIT_RESULTS = 5; /** Query to find the top most recent mailboxes */ @@ -101,7 +103,7 @@ public class RecentMailboxManager { EmailContent.ID_PROJECTION, selection, new String[] { Long.toString(accountId), Integer.toString(LIMIT_RESULTS) }, - MailboxColumns.DISPLAY_NAME); + RECENT_MAILBOXES_SORT_ORDER); try { while (cursor.moveToNext()) { returnList.add(cursor.getLong(EmailContent.ID_PROJECTION_COLUMN)); diff --git a/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java b/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java index fa3110086..171ace1a9 100644 --- a/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java +++ b/tests/src/com/android/emailcommon/utility/UtilityUnitTests.java @@ -525,4 +525,20 @@ public class UtilityUnitTests extends AndroidTestCase { assertFalse(Utility.isServerNameValid("$")); assertFalse(Utility.isServerNameValid(" ")); } + + private static Collection toColleciton(long... values) { + ArrayList ret = new ArrayList(); + for (long v : values) { + ret.add(v); + } + return ret; + } + + public void testBuildInSelection() { + assertEquals("", Utility.buildInSelection("c", null)); + assertEquals("", Utility.buildInSelection("c", toColleciton())); + assertEquals("c in (1)", Utility.buildInSelection("c", toColleciton(1))); + assertEquals("c in (1,2)", Utility.buildInSelection("c", toColleciton(1, 2))); + assertEquals("c in (1,2,-500)", Utility.buildInSelection("c", toColleciton(1, 2, -500))); + } }