2009-03-04 03:32:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.provider;
|
|
|
|
|
2009-10-17 15:37:39 +00:00
|
|
|
import com.android.email.Email;
|
2009-06-08 22:27:17 +00:00
|
|
|
import com.android.email.mail.internet.MimeUtility;
|
2009-07-27 22:52:42 +00:00
|
|
|
import com.android.email.provider.EmailContent.Attachment;
|
|
|
|
import com.android.email.provider.EmailContent.AttachmentColumns;
|
2009-10-01 06:32:30 +00:00
|
|
|
import com.android.email.provider.EmailContent.Message;
|
|
|
|
import com.android.email.provider.EmailContent.MessageColumns;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
import android.content.ContentProvider;
|
2009-06-03 13:44:47 +00:00
|
|
|
import android.content.ContentResolver;
|
2009-07-27 22:52:42 +00:00
|
|
|
import android.content.ContentUris;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.content.ContentValues;
|
2009-07-28 02:52:21 +00:00
|
|
|
import android.content.Context;
|
2009-03-04 03:32:22 +00:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.MatrixCursor;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.ParcelFileDescriptor;
|
2010-04-30 22:01:10 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.webkit.MimeTypeMap;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
2009-06-08 22:27:17 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.List;
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A simple ContentProvider that allows file access to Email's attachments.
|
2009-07-27 22:52:42 +00:00
|
|
|
*
|
|
|
|
* The URI scheme is as follows. For raw file access:
|
|
|
|
* content://com.android.email.attachmentprovider/acct#/attach#/RAW
|
|
|
|
*
|
|
|
|
* And for access to thumbnails:
|
|
|
|
* content://com.android.email.attachmentprovider/acct#/attach#/THUMBNAIL/width#/height#
|
2009-07-28 02:52:21 +00:00
|
|
|
*
|
|
|
|
* The on-disk (storage) schema is as follows.
|
|
|
|
*
|
|
|
|
* Attachments are stored at: <database-path>/account#.db_att/item#
|
|
|
|
* Thumbnails are stored at: <cache-path>/thmb_account#_item#
|
|
|
|
*
|
|
|
|
* Using the standard application context, account #10 and attachment # 20, this would be:
|
|
|
|
* /data/data/com.android.email/databases/10.db_att/20
|
|
|
|
* /data/data/com.android.email/cache/thmb_10_20
|
2009-03-04 03:32:22 +00:00
|
|
|
*/
|
|
|
|
public class AttachmentProvider extends ContentProvider {
|
2009-07-27 18:20:15 +00:00
|
|
|
|
|
|
|
public static final String AUTHORITY = "com.android.email.attachmentprovider";
|
|
|
|
public static final Uri CONTENT_URI = Uri.parse( "content://" + AUTHORITY);
|
2009-03-04 03:32:22 +00:00
|
|
|
|
|
|
|
private static final String FORMAT_RAW = "RAW";
|
|
|
|
private static final String FORMAT_THUMBNAIL = "THUMBNAIL";
|
|
|
|
|
|
|
|
public static class AttachmentProviderColumns {
|
|
|
|
public static final String _ID = "_id";
|
|
|
|
public static final String DATA = "_data";
|
|
|
|
public static final String DISPLAY_NAME = "_display_name";
|
|
|
|
public static final String SIZE = "_size";
|
|
|
|
}
|
|
|
|
|
2010-04-30 22:01:10 +00:00
|
|
|
private static final String[] MIME_TYPE_PROJECTION = new String[] {
|
|
|
|
AttachmentColumns.MIME_TYPE, AttachmentColumns.FILENAME };
|
|
|
|
private static final int MIME_TYPE_COLUMN_MIME_TYPE = 0;
|
|
|
|
private static final int MIME_TYPE_COLUMN_FILENAME = 1;
|
|
|
|
|
|
|
|
private static final String[] PROJECTION_QUERY = new String[] { AttachmentColumns.FILENAME,
|
2009-07-27 22:52:42 +00:00
|
|
|
AttachmentColumns.SIZE, AttachmentColumns.CONTENT_URI };
|
|
|
|
|
|
|
|
public static Uri getAttachmentUri(long accountId, long id) {
|
2009-03-04 03:32:22 +00:00
|
|
|
return CONTENT_URI.buildUpon()
|
2009-07-27 22:52:42 +00:00
|
|
|
.appendPath(Long.toString(accountId))
|
2009-03-04 03:32:22 +00:00
|
|
|
.appendPath(Long.toString(id))
|
|
|
|
.appendPath(FORMAT_RAW)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2009-07-28 02:52:21 +00:00
|
|
|
public static Uri getAttachmentThumbnailUri(long accountId, long id,
|
2009-06-08 22:27:17 +00:00
|
|
|
int width, int height) {
|
2009-03-04 03:32:22 +00:00
|
|
|
return CONTENT_URI.buildUpon()
|
2009-07-28 02:52:21 +00:00
|
|
|
.appendPath(Long.toString(accountId))
|
2009-03-04 03:32:22 +00:00
|
|
|
.appendPath(Long.toString(id))
|
|
|
|
.appendPath(FORMAT_THUMBNAIL)
|
|
|
|
.appendPath(Integer.toString(width))
|
|
|
|
.appendPath(Integer.toString(height))
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2009-07-28 02:52:21 +00:00
|
|
|
/**
|
|
|
|
* Return the filename for a given attachment. This should be used by any code that is
|
|
|
|
* going to *write* attachments.
|
|
|
|
*
|
|
|
|
* This does not create or write the file, or even the directories. It simply builds
|
|
|
|
* the filename that should be used.
|
|
|
|
*/
|
|
|
|
public static File getAttachmentFilename(Context context, long accountId, long attachmentId) {
|
2009-08-11 22:02:57 +00:00
|
|
|
return new File(getAttachmentDirectory(context, accountId), Long.toString(attachmentId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the directory for a given attachment. This should be used by any code that is
|
|
|
|
* going to *write* attachments.
|
|
|
|
*
|
|
|
|
* This does not create or write the directory. It simply builds the pathname that should be
|
|
|
|
* used.
|
|
|
|
*/
|
|
|
|
public static File getAttachmentDirectory(Context context, long accountId) {
|
|
|
|
return context.getDatabasePath(accountId + ".db_att");
|
2009-07-28 02:52:21 +00:00
|
|
|
}
|
|
|
|
|
2009-03-04 03:32:22 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
/*
|
|
|
|
* We use the cache dir as a temporary directory (since Android doesn't give us one) so
|
|
|
|
* on startup we'll clean up any .tmp files from the last run.
|
|
|
|
*/
|
|
|
|
File[] files = getContext().getCacheDir().listFiles();
|
|
|
|
for (File file : files) {
|
2009-06-16 01:35:38 +00:00
|
|
|
String filename = file.getName();
|
|
|
|
if (filename.endsWith(".tmp") || filename.startsWith("thmb_")) {
|
2009-03-04 03:32:22 +00:00
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-27 18:20:15 +00:00
|
|
|
/**
|
|
|
|
* Returns the mime type for a given attachment. There are three possible results:
|
|
|
|
* - If thumbnail Uri, always returns "image/png" (even if there's no attachment)
|
|
|
|
* - If the attachment does not exist, returns null
|
|
|
|
* - Returns the mime type of the attachment
|
|
|
|
*/
|
2009-03-04 03:32:22 +00:00
|
|
|
@Override
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
List<String> segments = uri.getPathSegments();
|
2009-07-27 22:52:42 +00:00
|
|
|
String accountId = segments.get(0);
|
2009-03-04 03:32:22 +00:00
|
|
|
String id = segments.get(1);
|
|
|
|
String format = segments.get(2);
|
|
|
|
if (FORMAT_THUMBNAIL.equals(format)) {
|
|
|
|
return "image/png";
|
2009-07-27 22:52:42 +00:00
|
|
|
} else {
|
|
|
|
uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, Long.parseLong(id));
|
2010-04-30 22:01:10 +00:00
|
|
|
Cursor c = getContext().getContentResolver().query(uri, MIME_TYPE_PROJECTION,
|
2009-07-27 22:52:42 +00:00
|
|
|
null, null, null);
|
2009-03-04 03:32:22 +00:00
|
|
|
try {
|
2009-07-27 22:52:42 +00:00
|
|
|
if (c.moveToFirst()) {
|
2010-04-30 22:01:10 +00:00
|
|
|
String mimeType = c.getString(MIME_TYPE_COLUMN_MIME_TYPE);
|
|
|
|
String fileName = c.getString(MIME_TYPE_COLUMN_FILENAME);
|
|
|
|
mimeType = inferMimeType(fileName, mimeType);
|
|
|
|
return mimeType;
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
2009-07-27 22:52:42 +00:00
|
|
|
} finally {
|
|
|
|
c.close();
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
2009-07-27 22:52:42 +00:00
|
|
|
return null;
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-30 22:01:10 +00:00
|
|
|
/**
|
|
|
|
* Helper to convert unknown or unmapped attachments to something useful based on filename
|
|
|
|
* extensions. Imperfect, but helps.
|
|
|
|
*
|
|
|
|
* If the given mime type is non-empty and anything other than "application/octet-stream",
|
|
|
|
* just return it. (This is the most common case.)
|
|
|
|
* If the filename has a recognizable extension and it converts to a mime type, return that.
|
|
|
|
* If the filename has an unrecognized extension, return "application/extension"
|
|
|
|
* Otherwise return "application/octet-stream".
|
|
|
|
*
|
|
|
|
* @param fileName The given filename
|
|
|
|
* @param mimeType The given mime type
|
|
|
|
* @return A likely mime type for the attachment
|
|
|
|
*/
|
|
|
|
public static String inferMimeType(String fileName, String mimeType) {
|
|
|
|
// If the given mime type appears to be non-empty and non-generic - return it
|
|
|
|
if (!TextUtils.isEmpty(mimeType) &&
|
|
|
|
!"application/octet-stream".equalsIgnoreCase(mimeType)) {
|
|
|
|
return mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find an extension in the filename
|
|
|
|
if (!TextUtils.isEmpty(fileName)) {
|
|
|
|
int lastDot = fileName.lastIndexOf('.');
|
|
|
|
String extension = null;
|
|
|
|
if ((lastDot > 0) && (lastDot < fileName.length() - 1)) {
|
|
|
|
extension = fileName.substring(lastDot + 1).toLowerCase();
|
|
|
|
}
|
|
|
|
if (!TextUtils.isEmpty(extension)) {
|
|
|
|
// Extension found. Look up mime type, or synthesize if none found.
|
|
|
|
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
|
|
|
if (mimeType == null) {
|
|
|
|
mimeType = "application/" + extension;
|
|
|
|
}
|
|
|
|
return mimeType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fallback case - no good guess could be made.
|
|
|
|
return "application/octet-stream";
|
|
|
|
}
|
|
|
|
|
2009-07-27 18:20:15 +00:00
|
|
|
/**
|
|
|
|
* Open an attachment file. There are two "modes" - "raw", which returns an actual file,
|
|
|
|
* and "thumbnail", which attempts to generate a thumbnail image.
|
|
|
|
*
|
|
|
|
* Thumbnails are cached for easy space recovery and cleanup.
|
|
|
|
*
|
|
|
|
* TODO: The thumbnail mode returns null for its failure cases, instead of throwing
|
|
|
|
* FileNotFoundException, and should be fixed for consistency.
|
|
|
|
*
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
*/
|
2009-03-04 03:32:22 +00:00
|
|
|
@Override
|
|
|
|
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
|
|
|
|
List<String> segments = uri.getPathSegments();
|
2009-07-27 22:52:42 +00:00
|
|
|
String accountId = segments.get(0);
|
2009-03-04 03:32:22 +00:00
|
|
|
String id = segments.get(1);
|
|
|
|
String format = segments.get(2);
|
|
|
|
if (FORMAT_THUMBNAIL.equals(format)) {
|
|
|
|
int width = Integer.parseInt(segments.get(3));
|
|
|
|
int height = Integer.parseInt(segments.get(4));
|
2009-07-27 22:52:42 +00:00
|
|
|
String filename = "thmb_" + accountId + "_" + id;
|
2009-03-04 03:32:22 +00:00
|
|
|
File dir = getContext().getCacheDir();
|
|
|
|
File file = new File(dir, filename);
|
|
|
|
if (!file.exists()) {
|
2009-07-27 22:52:42 +00:00
|
|
|
Uri attachmentUri = getAttachmentUri(Long.parseLong(accountId), Long.parseLong(id));
|
2009-06-01 18:28:33 +00:00
|
|
|
Cursor c = query(attachmentUri,
|
|
|
|
new String[] { AttachmentProviderColumns.DATA }, null, null, null);
|
|
|
|
if (c != null) {
|
|
|
|
try {
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
attachmentUri = Uri.parse(c.getString(0));
|
2009-07-27 22:52:42 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
2009-06-01 18:28:33 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String type = getContext().getContentResolver().getType(attachmentUri);
|
2009-03-04 03:32:22 +00:00
|
|
|
try {
|
2009-06-01 18:28:33 +00:00
|
|
|
InputStream in =
|
|
|
|
getContext().getContentResolver().openInputStream(attachmentUri);
|
2009-03-04 03:32:22 +00:00
|
|
|
Bitmap thumbnail = createThumbnail(type, in);
|
2009-06-01 18:28:33 +00:00
|
|
|
thumbnail = Bitmap.createScaledBitmap(thumbnail, width, height, true);
|
2009-03-04 03:32:22 +00:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
thumbnail.compress(Bitmap.CompressFormat.PNG, 100, out);
|
|
|
|
out.close();
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
catch (IOException ioe) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return ParcelFileDescriptor.open(
|
2009-07-27 22:52:42 +00:00
|
|
|
new File(getContext().getDatabasePath(accountId + ".db_att"), id),
|
2009-03-04 03:32:22 +00:00
|
|
|
ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String arg1, String[] arg2) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2009-07-27 18:20:15 +00:00
|
|
|
/**
|
|
|
|
* Returns a cursor based on the data in the attachments table, or null if the attachment
|
|
|
|
* is not recorded in the table.
|
|
|
|
*
|
|
|
|
* Supports REST Uri only, for a single row - selection, selection args, and sortOrder are
|
|
|
|
* ignored (non-null values should probably throw an exception....)
|
|
|
|
*/
|
2009-03-04 03:32:22 +00:00
|
|
|
@Override
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
String sortOrder) {
|
|
|
|
if (projection == null) {
|
|
|
|
projection =
|
|
|
|
new String[] {
|
|
|
|
AttachmentProviderColumns._ID,
|
|
|
|
AttachmentProviderColumns.DATA,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> segments = uri.getPathSegments();
|
2009-07-27 22:52:42 +00:00
|
|
|
String accountId = segments.get(0);
|
2009-03-04 03:32:22 +00:00
|
|
|
String id = segments.get(1);
|
|
|
|
String format = segments.get(2);
|
|
|
|
String name = null;
|
|
|
|
int size = -1;
|
2009-06-01 18:28:33 +00:00
|
|
|
String contentUri = null;
|
2009-07-27 22:52:42 +00:00
|
|
|
|
|
|
|
uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, Long.parseLong(id));
|
|
|
|
Cursor c = getContext().getContentResolver().query(uri, PROJECTION_QUERY,
|
|
|
|
null, null, null);
|
2009-03-04 03:32:22 +00:00
|
|
|
try {
|
2009-07-27 22:52:42 +00:00
|
|
|
if (c.moveToFirst()) {
|
|
|
|
name = c.getString(0);
|
|
|
|
size = c.getInt(1);
|
|
|
|
contentUri = c.getString(2);
|
|
|
|
} else {
|
2009-03-04 03:32:22 +00:00
|
|
|
return null;
|
|
|
|
}
|
2009-07-27 22:52:42 +00:00
|
|
|
} finally {
|
|
|
|
c.close();
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MatrixCursor ret = new MatrixCursor(projection);
|
|
|
|
Object[] values = new Object[projection.length];
|
|
|
|
for (int i = 0, count = projection.length; i < count; i++) {
|
|
|
|
String column = projection[i];
|
|
|
|
if (AttachmentProviderColumns._ID.equals(column)) {
|
|
|
|
values[i] = id;
|
|
|
|
}
|
|
|
|
else if (AttachmentProviderColumns.DATA.equals(column)) {
|
2009-06-01 18:28:33 +00:00
|
|
|
values[i] = contentUri;
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|
|
|
|
else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) {
|
|
|
|
values[i] = name;
|
|
|
|
}
|
|
|
|
else if (AttachmentProviderColumns.SIZE.equals(column)) {
|
|
|
|
values[i] = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.addRow(values);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap createThumbnail(String type, InputStream data) {
|
|
|
|
if(MimeUtility.mimeTypeMatches(type, "image/*")) {
|
|
|
|
return createImageThumbnail(data);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap createImageThumbnail(InputStream data) {
|
|
|
|
try {
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeStream(data);
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
catch (OutOfMemoryError oome) {
|
|
|
|
/*
|
|
|
|
* Improperly downloaded images, corrupt bitmaps and the like can commonly
|
|
|
|
* cause OOME due to invalid allocation sizes. We're happy with a null bitmap in
|
|
|
|
* that case. If the system is really out of memory we'll know about it soon
|
|
|
|
* enough.
|
|
|
|
*/
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2009-06-03 13:44:47 +00:00
|
|
|
/**
|
2009-07-27 18:20:15 +00:00
|
|
|
* Resolve attachment id to content URI. Returns the resolved content URI (from the attachment
|
|
|
|
* DB) or, if not found, simply returns the incoming value.
|
2009-06-03 13:44:47 +00:00
|
|
|
*
|
|
|
|
* @param attachmentUri
|
|
|
|
* @return resolved content URI
|
2009-07-27 18:20:15 +00:00
|
|
|
*
|
|
|
|
* TODO: Throws an SQLite exception on a missing DB file (e.g. unknown URI) instead of just
|
|
|
|
* returning the incoming uri, as it should.
|
2009-06-03 13:44:47 +00:00
|
|
|
*/
|
|
|
|
public static Uri resolveAttachmentIdToContentUri(ContentResolver resolver, Uri attachmentUri) {
|
|
|
|
Cursor c = resolver.query(attachmentUri,
|
|
|
|
new String[] { AttachmentProvider.AttachmentProviderColumns.DATA },
|
|
|
|
null, null, null);
|
|
|
|
if (c != null) {
|
|
|
|
try {
|
|
|
|
if (c.moveToFirst()) {
|
2009-10-17 15:37:39 +00:00
|
|
|
final String strUri = c.getString(0);
|
|
|
|
if (strUri != null) {
|
|
|
|
return Uri.parse(strUri);
|
|
|
|
} else {
|
|
|
|
Email.log("AttachmentProvider: attachment with null contentUri");
|
|
|
|
}
|
2009-06-03 13:44:47 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return attachmentUri;
|
|
|
|
}
|
2009-08-20 18:09:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* In support of deleting a message, find all attachments and delete associated attachment
|
|
|
|
* files.
|
|
|
|
* @param context
|
|
|
|
* @param accountId the account for the message
|
|
|
|
* @param messageId the message
|
|
|
|
*/
|
|
|
|
public static void deleteAllAttachmentFiles(Context context, long accountId, long messageId) {
|
|
|
|
Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, messageId);
|
|
|
|
Cursor c = context.getContentResolver().query(uri, Attachment.ID_PROJECTION,
|
|
|
|
null, null, null);
|
|
|
|
try {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
long attachmentId = c.getLong(Attachment.ID_PROJECTION_COLUMN);
|
|
|
|
File attachmentFile = getAttachmentFilename(context, accountId, attachmentId);
|
|
|
|
// Note, delete() throws no exceptions for basic FS errors (e.g. file not found)
|
|
|
|
// it just returns false, which we ignore, and proceed to the next file.
|
|
|
|
// This entire loop is best-effort only.
|
|
|
|
attachmentFile.delete();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
2009-10-01 06:32:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* In support of deleting a mailbox, find all messages and delete their attachments.
|
|
|
|
*
|
|
|
|
* @param context
|
|
|
|
* @param accountId the account for the mailbox
|
|
|
|
* @param mailboxId the mailbox for the messages
|
|
|
|
*/
|
|
|
|
public static void deleteAllMailboxAttachmentFiles(Context context, long accountId,
|
|
|
|
long mailboxId) {
|
|
|
|
Cursor c = context.getContentResolver().query(Message.CONTENT_URI,
|
|
|
|
Message.ID_COLUMN_PROJECTION, MessageColumns.MAILBOX_KEY + "=?",
|
|
|
|
new String[] { Long.toString(mailboxId) }, null);
|
|
|
|
try {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
long messageId = c.getLong(Message.ID_PROJECTION_COLUMN);
|
|
|
|
deleteAllAttachmentFiles(context, accountId, messageId);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 03:32:22 +00:00
|
|
|
}
|