Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
EmailProvider now supports query and getType for cached file urls
Browse files Browse the repository at this point in the history
b/17573792
This is needed so that we can send cached files as attachments.
This case comes up if you edit a draft with an attachment,
view the attachment, and then share that with Email.
Also, update the manifest so that EmailProvider grants
uri permission for cachedFiles.

Change-Id: Ib32ae8360b627823af9361cba05e0e5dbd0ae4ca
  • Loading branch information
Martin Hibdon committed Oct 10, 2014
1 parent 1927361 commit 8927278
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
4 changes: 3 additions & 1 deletion AndroidManifest.xml
Expand Up @@ -672,7 +672,9 @@
android:exported="true"
android:permission="com.android.email.permission.ACCESS_PROVIDER"
android:label="@string/app_name"
/>
android:grantUriPermissions="true">
<grant-uri-permission android:pathPrefix="/attachment/cachedFile"/>
</provider>

<!-- Legacy authenticators, etc. can be added below. OEMs may remove these -->

Expand Down
76 changes: 76 additions & 0 deletions provider_src/com/android/email/provider/EmailProvider.java
Expand Up @@ -355,6 +355,12 @@ public class EmailProvider extends ContentProvider

private static final String SYNC_STATUS_CALLBACK_METHOD = "sync_status";

private static final String[] MIME_TYPE_PROJECTION = new String[]{AttachmentColumns.MIME_TYPE};

private static final String[] CACHED_FILE_QUERY_PROJECTION = new String[]
{ AttachmentColumns._ID, AttachmentColumns.FILENAME, AttachmentColumns.SIZE,
AttachmentColumns.CONTENT_URI };

/**
* Wrap the UriMatcher call so we can throw a runtime exception if an unknown Uri is passed in
* @param uri the Uri to match
Expand Down Expand Up @@ -843,6 +849,23 @@ public String getType(Uri uri) {
return "vnd.android.cursor.dir/email-hostauth";
case HOSTAUTH_ID:
return "vnd.android.cursor.item/email-hostauth";
case ATTACHMENTS_CACHED_FILE_ACCESS: {
SQLiteDatabase db = getDatabase(getContext());
Cursor c = db.query(Attachment.TABLE_NAME, MIME_TYPE_PROJECTION,
AttachmentColumns.CACHED_FILE + "=?", new String[]{uri.toString()},
null, null, null, null);
try {
if (c != null && c.moveToFirst()) {
return c.getString(0);
} else {
return null;
}
} finally {
if (c != null) {
c.close();
}
}
}
default:
return null;
}
Expand Down Expand Up @@ -1423,6 +1446,59 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
id = uri.getPathSegments().get(2);
c = uiQuickResponseAccount(projection, id);
break;
case ATTACHMENTS_CACHED_FILE_ACCESS:
if (projection == null) {
projection =
new String[] {
AttachmentUtilities.Columns._ID,
AttachmentUtilities.Columns.DATA,
};
}
// Map the columns of our attachment table to the columns defined in
// AttachmentUtils. These are a superset of OpenableColumns.
// This mirrors similar code in AttachmentProvider.
c = db.query(Attachment.TABLE_NAME,
CACHED_FILE_QUERY_PROJECTION, AttachmentColumns.CACHED_FILE + "=?",
new String[]{uri.toString()}, null, null, null, null);
try {
if (c.getCount() > 1) {
LogUtils.e(TAG, "multiple results querying CACHED_FILE_ACCESS %s", uri);
}
if (c != null && c.moveToFirst()) {
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];
if (AttachmentUtilities.Columns._ID.equals(column)) {
values[i] = c.getLong(
c.getColumnIndexOrThrow(AttachmentColumns._ID));
}
else if (AttachmentUtilities.Columns.DATA.equals(column)) {
values[i] = c.getString(
c.getColumnIndexOrThrow(AttachmentColumns.CONTENT_URI));
}
else if (AttachmentUtilities.Columns.DISPLAY_NAME.equals(column)) {
values[i] = c.getString(
c.getColumnIndexOrThrow(AttachmentColumns.FILENAME));
}
else if (AttachmentUtilities.Columns.SIZE.equals(column)) {
values[i] = c.getInt(
c.getColumnIndexOrThrow(AttachmentColumns.SIZE));
} else {
LogUtils.e(TAG,
"unexpected column %s requested for CACHED_FILE",
column);
}
}
ret.addRow(values);
return ret;
}
} finally {
if (c != null) {
c.close();
}
}
return null;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
Expand Down

0 comments on commit 8927278

Please sign in to comment.