Rework of attachment loading/saving
* Respect the destination for attachment downloading * Rework attachment load/save for IMAP/POP Change-Id: I94fdcea5ec6d397aba554fec3753b2de9cb5f1dd
This commit is contained in:
parent
b6d43d73c1
commit
733aef1204
|
@ -16,21 +16,32 @@
|
|||
|
||||
package com.android.emailcommon.utility;
|
||||
|
||||
import com.android.emailcommon.Logging;
|
||||
import com.android.emailcommon.provider.EmailContent.Attachment;
|
||||
import com.android.emailcommon.provider.EmailContent.Message;
|
||||
import com.android.emailcommon.provider.EmailContent.MessageColumns;
|
||||
|
||||
import android.app.DownloadManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.ContentUris;
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.android.emailcommon.Logging;
|
||||
import com.android.emailcommon.provider.EmailContent.Attachment;
|
||||
import com.android.emailcommon.provider.EmailContent.AttachmentColumns;
|
||||
import com.android.emailcommon.provider.EmailContent.Message;
|
||||
import com.android.emailcommon.provider.EmailContent.MessageColumns;
|
||||
import com.android.mail.providers.UIProvider;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AttachmentUtilities {
|
||||
public static final String AUTHORITY = "com.android.email.attachmentprovider";
|
||||
|
@ -349,4 +360,72 @@ public class AttachmentUtilities {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static long copyFile(InputStream in, File file) throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(file);
|
||||
long size = IOUtils.copy(in, out);
|
||||
in.close();
|
||||
out.flush();
|
||||
out.close();
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the attachment to its final resting place (cache or sd card)
|
||||
*/
|
||||
public static void saveAttachment(Context context, InputStream in, Attachment attachment) {
|
||||
Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachment.mId);
|
||||
ContentValues cv = new ContentValues();
|
||||
long attachmentId = attachment.mId;
|
||||
long accountId = attachment.mAccountKey;
|
||||
String contentUri;
|
||||
long size;
|
||||
try {
|
||||
if (attachment.mUiDestination == UIProvider.AttachmentDestination.CACHE) {
|
||||
File saveIn = getAttachmentDirectory(context, accountId);
|
||||
if (!saveIn.exists()) {
|
||||
saveIn.mkdirs();
|
||||
}
|
||||
File file = getAttachmentFilename(context, accountId, attachmentId);
|
||||
file.createNewFile();
|
||||
size = copyFile(in, file);
|
||||
contentUri = getAttachmentUri(accountId, attachmentId).toString();
|
||||
} else if (Utility.isExternalStorageMounted()) {
|
||||
File downloads = Environment.getExternalStoragePublicDirectory(
|
||||
Environment.DIRECTORY_DOWNLOADS);
|
||||
downloads.mkdirs();
|
||||
File file = Utility.createUniqueFile(downloads, attachment.mFileName);
|
||||
size = copyFile(in, file);
|
||||
String absolutePath = file.getAbsolutePath();
|
||||
|
||||
// Although the download manager can scan media files, scanning only happens
|
||||
// after the user clicks on the item in the Downloads app. So, we run the
|
||||
// attachment through the media scanner ourselves so it gets added to
|
||||
// gallery / music immediately.
|
||||
MediaScannerConnection.scanFile(context, new String[] {absolutePath},
|
||||
null, null);
|
||||
|
||||
DownloadManager dm =
|
||||
(DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
|
||||
long id = dm.addCompletedDownload(attachment.mFileName, attachment.mFileName,
|
||||
false /* do not use media scanner */,
|
||||
attachment.mMimeType, absolutePath, size,
|
||||
true /* show notification */);
|
||||
contentUri = dm.getUriForDownloadedFile(id).toString();
|
||||
|
||||
} else {
|
||||
throw new IOException();
|
||||
}
|
||||
|
||||
// Update the attachment
|
||||
cv.put(AttachmentColumns.SIZE, size);
|
||||
cv.put(AttachmentColumns.CONTENT_URI, contentUri);
|
||||
cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED);
|
||||
} catch (IOException e) {
|
||||
// Handle failures here...
|
||||
cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.FAILED);
|
||||
}
|
||||
context.getContentResolver().update(uri, cv, null, null);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,6 @@ import android.os.RemoteException;
|
|||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.email.LegacyConversions;
|
||||
import com.android.email.NotificationController;
|
||||
import com.android.email.mail.Sender;
|
||||
import com.android.email.mail.Store;
|
||||
|
@ -65,7 +64,6 @@ import com.android.emailcommon.utility.AttachmentUtilities;
|
|||
import com.android.emailcommon.utility.Utility;
|
||||
import com.android.mail.providers.UIProvider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
|
@ -285,9 +283,9 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
|
|||
throw new MessagingException("Attachment not loaded.");
|
||||
}
|
||||
|
||||
// 5. Save the downloaded file and update the attachment as necessary
|
||||
LegacyConversions.saveAttachmentBody(mContext, storePart, attachment,
|
||||
message.mAccountKey);
|
||||
// Save the attachment to wherever it's going
|
||||
AttachmentUtilities.saveAttachment(mContext, storePart.getBody().getInputStream(),
|
||||
attachment);
|
||||
|
||||
// 6. Report success
|
||||
mCallback.loadAttachmentStatus(messageId, attachmentId, EmailServiceStatus.SUCCESS, 0);
|
||||
|
@ -302,8 +300,6 @@ public abstract class EmailServiceStub extends IEmailService.Stub implements IEm
|
|||
mContext.getContentResolver().update(uri, cv, null, null);
|
||||
|
||||
mCallback.loadAttachmentStatus(0, attachmentId, EmailServiceStatus.CONNECTION_ERROR, 0);
|
||||
} catch (IOException ioe) {
|
||||
Log.e(Logging.LOG_TAG, "Error while storing attachment." + ioe.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue