From e292027fa7ec3f7d12aa7228d2c62b7c863581be Mon Sep 17 00:00:00 2001 From: Tony Mantler Date: Mon, 30 Jun 2014 10:21:25 -0700 Subject: [PATCH] Pre-cache attachment content URIs Saves us a DB read on the UI thread. b/15939030 Change-Id: I1b8174880ccc9dc06df6b57df2bd8d44a2fe05d4 --- .../android/email/provider/EmailProvider.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index e8d87f61a..78e9ae8a3 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -4130,23 +4130,32 @@ public class EmailProvider extends ContentProvider private final int mContentUriIndex; private final int mUriIndex; private final Context mContext; + private final String[] mContentUriStrings; public AttachmentsCursor(Context context, Cursor cursor) { super(cursor); mContentUriIndex = cursor.getColumnIndex(UIProvider.AttachmentColumns.CONTENT_URI); mUriIndex = cursor.getColumnIndex(UIProvider.AttachmentColumns.URI); mContext = context; - } - - @Override - public String getString(int column) { - if (column == mContentUriIndex) { + mContentUriStrings = new String[cursor.getCount()]; + if (mContentUriIndex == -1) { + // Nothing to do here, move along + return; + } + while (cursor.moveToNext()) { + final int index = cursor.getPosition(); final Uri uri = Uri.parse(getString(mUriIndex)); final long id = Long.parseLong(uri.getLastPathSegment()); final Attachment att = Attachment.restoreAttachmentWithId(mContext, id); - if (att == null) return ""; + + if (att == null) { + mContentUriStrings[index] = ""; + continue; + } + if (!TextUtils.isEmpty(att.getCachedFileUri())) { - return att.getCachedFileUri(); + mContentUriStrings[index] = att.getCachedFileUri(); + continue; } final String contentUri; @@ -4172,8 +4181,16 @@ public class EmailProvider extends ContentProvider .toString(); } } - return contentUri; + mContentUriStrings[index] = contentUri; + } + cursor.moveToPosition(-1); + } + + @Override + public String getString(int column) { + if (column == mContentUriIndex) { + return mContentUriStrings[getPosition()]; } else { return super.getString(column); }