Cache column name/indexes so we can use getColumnIndex()

MatrixCursor doesn't do this, so we need to add our own cache.

Depends on Ia5ffd31f54a2a335f495df5e0d1a0886ee08c14b

Change-Id: Ica78e3cec747bf86fcef16508156e359216c64fe
This commit is contained in:
Scott Kennedy 2013-02-06 15:21:15 -08:00
parent 80b5bb3dfc
commit 7fdde9bb4a
9 changed files with 43 additions and 34 deletions

View File

@ -19,12 +19,13 @@ package com.android.email.data;
import android.database.Cursor;
import android.database.MatrixCursor;
import com.android.mail.utils.MatrixCursorWithCachedColumns;
/**
* {@link MatrixCursor} which takes an extra {@link Cursor} to the constructor, and close
* it when self is closed.
*/
public class ClosingMatrixCursor extends MatrixCursor {
public class ClosingMatrixCursor extends MatrixCursorWithCachedColumns {
private final Cursor mInnerCursor;
public ClosingMatrixCursor(String[] columnNames, Cursor innerCursor) {

View File

@ -16,14 +16,6 @@
package com.android.email.provider;
import com.android.emailcommon.Logging;
import com.android.emailcommon.internet.MimeUtility;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.Attachment;
import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
import com.android.emailcommon.utility.AttachmentUtilities;
import com.android.emailcommon.utility.AttachmentUtilities.Columns;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
@ -38,6 +30,15 @@ import android.os.Binder;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import com.android.emailcommon.Logging;
import com.android.emailcommon.internet.MimeUtility;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.Attachment;
import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
import com.android.emailcommon.utility.AttachmentUtilities;
import com.android.emailcommon.utility.AttachmentUtilities.Columns;
import com.android.mail.utils.MatrixCursorWithCachedColumns;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@ -275,7 +276,7 @@ public class AttachmentProvider extends ContentProvider {
c.close();
}
MatrixCursor ret = new MatrixCursor(projection);
MatrixCursor ret = new MatrixCursorWithCachedColumns(projection);
Object[] values = new Object[projection.length];
for (int i = 0, count = projection.length; i < count; i++) {
String column = projection[i];

View File

@ -27,6 +27,7 @@ import android.util.Log;
import android.util.LruCache;
import com.android.email2.ui.MailActivityEmail;
import com.android.mail.utils.MatrixCursorWithCachedColumns;
import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
@ -543,7 +544,7 @@ public final class ContentCache {
Cursor c = get(id);
if (c != null) {
// Make a new MatrixCursor with the requested columns
MatrixCursor mc = new MatrixCursor(projection, 1);
MatrixCursor mc = new MatrixCursorWithCachedColumns(projection, 1);
if (c.getCount() == 0) {
return mc;
}

View File

@ -88,6 +88,7 @@ import com.android.mail.providers.UIProvider.ConversationSendingState;
import com.android.mail.providers.UIProvider.DraftType;
import com.android.mail.providers.UIProvider.Swipe;
import com.android.mail.utils.LogUtils;
import com.android.mail.utils.MatrixCursorWithCachedColumns;
import com.android.mail.utils.MatrixCursorWithExtra;
import com.android.mail.utils.Utils;
import com.android.mail.widget.BaseWidgetProvider;
@ -1254,7 +1255,7 @@ public class EmailProvider extends ContentProvider {
case ACCOUNT_ID:
case HOSTAUTH_ID:
case POLICY_ID:
return new MatrixCursor(projection, 0);
return new MatrixCursorWithCachedColumns(projection, 0);
}
}
throw e;
@ -1344,7 +1345,7 @@ public class EmailProvider extends ContentProvider {
}
}
// Return a cursor with an id projection
MatrixCursor mc = new MatrixCursor(EmailContent.ID_PROJECTION);
MatrixCursor mc = new MatrixCursorWithCachedColumns(EmailContent.ID_PROJECTION);
mc.addRow(new Object[] {accountId});
c = mc;
break;
@ -1355,7 +1356,7 @@ public class EmailProvider extends ContentProvider {
int type = Integer.parseInt(pathSegments.get(2));
long mailboxId = getMailboxIdFromMailboxTypeMap(accountId, type);
// Return a cursor with an id projection
mc = new MatrixCursor(EmailContent.ID_PROJECTION);
mc = new MatrixCursorWithCachedColumns(EmailContent.ID_PROJECTION);
mc.addRow(new Object[] {mailboxId});
c = mc;
break;
@ -1375,7 +1376,7 @@ public class EmailProvider extends ContentProvider {
int accountCount = mMailboxTypeMap.size();
// In the rare case there are MAX_CACHED_ACCOUNTS or more, we can't do this
if (accountCount < MAX_CACHED_ACCOUNTS) {
mc = new MatrixCursor(projection, 1);
mc = new MatrixCursorWithCachedColumns(projection, 1);
mc.addRow(new Object[] {accountCount});
c = mc;
break;
@ -3061,7 +3062,7 @@ outer:
}
private Cursor getVirtualMailboxCursor(long mailboxId) {
MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 1);
MatrixCursor mc = new MatrixCursorWithCachedColumns(UIProvider.FOLDERS_PROJECTION, 1);
mc.addRow(getVirtualMailboxRow(getVirtualMailboxAccountId(mailboxId),
getVirtualMailboxType(mailboxId)));
return mc;
@ -3201,7 +3202,7 @@ outer:
SQLiteDatabase db = getDatabase(context);
String id = uri.getPathSegments().get(1);
if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
MatrixCursor mc = new MatrixCursor(UIProvider.FOLDERS_PROJECTION, 2);
MatrixCursor mc = new MatrixCursorWithCachedColumns(UIProvider.FOLDERS_PROJECTION, 2);
Object[] row = getVirtualMailboxRow(COMBINED_ACCOUNT_ID, Mailbox.TYPE_INBOX);
int numUnread = EmailContent.count(context, Message.CONTENT_URI,
MessageColumns.MAILBOX_KEY + " IN (SELECT " + MailboxColumns.ID +
@ -3228,7 +3229,7 @@ outer:
} else {
// Add starred virtual folder to the cursor
// Show number of messages as unread count (for backward compatibility)
MatrixCursor starCursor = new MatrixCursor(uiProjection, 1);
MatrixCursor starCursor = new MatrixCursorWithCachedColumns(uiProjection, 1);
Object[] row = getVirtualMailboxRow(Long.parseLong(id), Mailbox.TYPE_STARRED);
row[UIProvider.FOLDER_UNREAD_COUNT_COLUMN] = numStarred;
row[UIProvider.FOLDER_ICON_RES_ID_COLUMN] = R.drawable.ic_menu_star_holo_light;
@ -3388,7 +3389,7 @@ outer:
* folder capabilities.
*/
Cursor getFolderListCursor(SQLiteDatabase db, Cursor c, String[] uiProjection) {
final MatrixCursor mc = new MatrixCursor(uiProjection);
final MatrixCursor mc = new MatrixCursorWithCachedColumns(uiProjection);
Object[] values = new Object[uiProjection.length];
String[] args = new String[1];
try {
@ -3483,7 +3484,7 @@ outer:
break;
case UI_ACCOUNT:
if (id.equals(COMBINED_ACCOUNT_ID_STRING)) {
MatrixCursor mc = new MatrixCursor(uiProjection, 1);
MatrixCursor mc = new MatrixCursorWithCachedColumns(uiProjection, 1);
addCombinedAccountRow(mc);
c = mc;
} else {
@ -4173,7 +4174,7 @@ outer:
try {
// TODO Always use this projection? Or what's passed in?
// Not sure if UI wants it, but I'm making a cursor of convo uri's
MatrixCursor c = new MatrixCursor(
MatrixCursor c = new MatrixCursorWithCachedColumns(
new String[] {UIProvider.ConversationColumns.URI},
mLastSequenceOps.size());
for (ContentProviderOperation op: mLastSequenceOps) {
@ -4192,7 +4193,7 @@ outer:
} catch (OperationApplicationException e) {
}
}
return new MatrixCursor(projection, 0);
return new MatrixCursorWithCachedColumns(projection, 0);
}
private void notifyUIConversation(Uri uri) {

View File

@ -31,7 +31,7 @@ import java.util.Set;
public class FolderPropertiesTests extends AndroidTestCase {
private static Cursor buildCursor(String[] columns, Object... values) {
MatrixCursor c = new MatrixCursor(columns, 1);
MatrixCursor c = new MatrixCursorWithCachedColumns(columns, 1);
c.addRow(values);
c.moveToFirst();
return c;

View File

@ -78,7 +78,7 @@ public class AccountSelectorAdapterTest extends LoaderTestCase {
}
private static AccountSelectorAdapter.CursorWithExtras createCursorWithExtras() {
final MatrixCursor m = new MatrixCursor(new String[] {"column"});
final MatrixCursor m = new MatrixCursorWithCachedColumns(new String[] {"column"});
return new AccountSelectorAdapter.CursorWithExtras(m.getColumnNames(), m);
}

View File

@ -17,6 +17,7 @@
package com.android.email.activity;
import com.android.email.activity.ContactStatusLoader.Result;
import com.android.mail.utils.MatrixCursorWithCachedColumns;
import android.content.Context;
import android.content.pm.ProviderInfo;
@ -62,8 +63,8 @@ public class ContactStatusLoaderTest
// Contact doesn't exist
public void testContactNotFound() {
// Insert empty cursor
mProvider.mCursors.offer(
new MatrixCursor(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE));
mProvider.mCursors.offer(new MatrixCursorWithCachedColumns(
ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE));
// Load!
ContactStatusLoader l = new ContactStatusLoader(getMockContext(), EMAIL);
@ -95,12 +96,14 @@ public class ContactStatusLoaderTest
// Contact exists, but no photo
public void testNoPhoto() {
// Result for the first query (the one for photo-id)
MatrixCursor cursor1 = new MatrixCursor(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE);
MatrixCursor cursor1 =
new MatrixCursorWithCachedColumns(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE);
cursor1.addRow(new Object[]{12345, StatusUpdates.AWAY});
mProvider.mCursors.offer(cursor1);
// Empty cursor for the second query
mProvider.mCursors.offer(new MatrixCursor(ContactStatusLoader.PHOTO_PROJECTION));
mProvider.mCursors.offer(
new MatrixCursorWithCachedColumns(ContactStatusLoader.PHOTO_PROJECTION));
// Load!
ContactStatusLoader l = new ContactStatusLoader(getMockContext(), EMAIL);
@ -124,7 +127,8 @@ public class ContactStatusLoaderTest
// Contact exists, but no photo (provider returns null for the second query)
public void testNull2() {
// Result for the first query (the one for photo-id)
MatrixCursor cursor1 = new MatrixCursor(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE);
MatrixCursor cursor1 =
new MatrixCursorWithCachedColumns(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE);
cursor1.addRow(new Object[]{12345, StatusUpdates.AWAY});
mProvider.mCursors.offer(cursor1);
@ -142,7 +146,8 @@ public class ContactStatusLoaderTest
// Contact exists, with a photo
public void testWithPhoto() {
// Result for the first query (the one for photo-id)
MatrixCursor cursor1 = new MatrixCursor(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE);
MatrixCursor cursor1 =
new MatrixCursorWithCachedColumns(ContactStatusLoader.PROJECTION_PHOTO_ID_PRESENCE);
cursor1.addRow(new Object[]{12345, StatusUpdates.AWAY});
mProvider.mCursors.offer(cursor1);
@ -168,7 +173,7 @@ public class ContactStatusLoaderTest
}
// MatrixCursor doesn't support getBlob, so use this...
private static class PhotoCursor extends MatrixCursor {
private static class PhotoCursor extends MatrixCursorWithCachedColumns {
private final byte[] mBlob;
public PhotoCursor(byte[] blob) {

View File

@ -196,7 +196,7 @@ public class ContentCacheTests extends ProviderTestCase2<EmailProvider> {
private static final String[] SIMPLE_PROJECTION = new String[] {"Foo"};
private static final Object[] SIMPLE_ROW = new Object[] {"Bar"};
private Cursor getOneRowCursor() {
MatrixCursor cursor = new MatrixCursor(SIMPLE_PROJECTION, 1);
MatrixCursor cursor = new MatrixCursorWithCachedColumns(SIMPLE_PROJECTION, 1);
cursor.addRow(SIMPLE_ROW);
return cursor;
}

View File

@ -440,7 +440,7 @@ public class UtilityUnitTests extends AndroidTestCase {
public void testDumpCursor() {
// Just make sure the method won't crash and returns non-empty string.
final Cursor c1 = new MatrixCursor(new String[] {"col"});
final Cursor c1 = new MatrixCursorWithCachedColumns(new String[] {"col"});
final Cursor c2 = new CursorWrapper(c1);
// Note it's a subclass of CursorWrapper.
@ -461,7 +461,7 @@ public class UtilityUnitTests extends AndroidTestCase {
}
public void testCloseTraceCursorWrapper() {
final Cursor org = new MatrixCursor(new String[] {"col"});
final Cursor org = new MatrixCursorWithCachedColumns(new String[] {"col"});
final Utility.CloseTraceCursorWrapper c =
Utility.CloseTraceCursorWrapper.alwaysCreateForTest(org);