Close cursor properly.

Also, don't issue separate query()s for each recent mailbox.

Bug 4977956

Change-Id: I3ccd437a7efd5c3599c4a1952ba091a7b3b815bf
This commit is contained in:
Makoto Onuki 2011-06-29 18:46:23 -07:00
parent 5fbc5025f0
commit 8de5bda815
4 changed files with 60 additions and 13 deletions

View File

@ -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<? extends Number> 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();
}
}

View File

@ -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<Long> 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) {

View File

@ -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));

View File

@ -525,4 +525,20 @@ public class UtilityUnitTests extends AndroidTestCase {
assertFalse(Utility.isServerNameValid("$"));
assertFalse(Utility.isServerNameValid(" "));
}
private static Collection<Long> toColleciton(long... values) {
ArrayList<Long> ret = new ArrayList<Long>();
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)));
}
}