diff options
author | kaiyiz <kaiyiz@codeaurora.org> | 2014-03-21 11:15:55 +0800 |
---|---|---|
committer | Adnan <adnan@cyngn.com> | 2014-09-03 15:41:22 -0700 |
commit | 5446d6bfbb2efce3bf1f3f912adb8980559b191d (patch) | |
tree | f5fb3a3e59ef8ddd2ce7884919fbba74cfa5d27c | |
parent | dae8c5ed80cc3f2ee243fb9cf22b25cd20a3a4f1 (diff) | |
download | android_packages_apps_UnifiedEmail-5446d6bfbb2efce3bf1f3f912adb8980559b191d.tar.gz android_packages_apps_UnifiedEmail-5446d6bfbb2efce3bf1f3f912adb8980559b191d.tar.bz2 android_packages_apps_UnifiedEmail-5446d6bfbb2efce3bf1f3f912adb8980559b191d.zip |
UnifiedEmail: Use the cached attachment to save first
Now try to use the cached attachment to save first. If the attachment
is not cached, send the command to download it.
CRs-Fixed: 601682
Change-Id: I16917c192c76a077dcf478001e276c3b6a7f7e93
-rw-r--r-- | src/com/android/mail/browse/AttachmentActionHandler.java | 76 | ||||
-rw-r--r-- | src/com/android/mail/browse/MessageAttachmentBar.java | 1 | ||||
-rw-r--r-- | src/com/android/mail/providers/Attachment.java | 2 |
3 files changed, 77 insertions, 2 deletions
diff --git a/src/com/android/mail/browse/AttachmentActionHandler.java b/src/com/android/mail/browse/AttachmentActionHandler.java index 9c1fa0064..4527b4c49 100644 --- a/src/com/android/mail/browse/AttachmentActionHandler.java +++ b/src/com/android/mail/browse/AttachmentActionHandler.java @@ -19,6 +19,7 @@ package com.android.mail.browse; import android.app.DialogFragment; +import android.app.DownloadManager; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; @@ -26,7 +27,9 @@ import android.content.ActivityNotFoundException; import android.content.ContentValues; import android.content.Context; import android.content.Intent; +import android.media.MediaScannerConnection; import android.net.Uri; +import android.os.Environment; import android.os.Handler; import android.os.Parcelable; @@ -40,6 +43,13 @@ import com.android.mail.utils.LogTag; import com.android.mail.utils.LogUtils; import com.android.mail.utils.Utils; +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.ArrayList; public class AttachmentActionHandler { @@ -110,6 +120,15 @@ public class AttachmentActionHandler { private void startDownloadingAttachment( Attachment attachment, int destination, int rendition, int additionalPriority, boolean delayDownload) { + if (attachment.state == AttachmentState.SAVED + && destination == AttachmentDestination.EXTERNAL) { + File savedFile = performAttachmentSave(attachment); + if (savedFile != null) { + // The attachment is saved successfully from cache. + return; + } + } + final ContentValues params = new ContentValues(5); params.put(AttachmentColumns.STATE, AttachmentState.DOWNLOADING); params.put(AttachmentColumns.DESTINATION, destination); @@ -236,4 +255,61 @@ public class AttachmentActionHandler { LogUtils.e(LOG_TAG, "Couldn't find Activity for intent", e); } } + + private File createUniqueFile(File directory, String filename) throws IOException { + File file = new File(directory, filename); + if (file.createNewFile()) { + return file; + } + // Get the extension of the file, if any. + int index = filename.lastIndexOf('.'); + String format; + if (index != -1) { + String name = filename.substring(0, index); + String extension = filename.substring(index); + format = name + "-%d" + extension; + } else { + format = filename + "-%d"; + } + + for (int i = 2; i < Integer.MAX_VALUE; i++) { + file = new File(directory, String.format(format, i)); + if (file.createNewFile()) { + return file; + } + } + return null; + } + + private File performAttachmentSave(final Attachment attachment) { + Uri attachmentUri = attachment.uri; + try { + File downloads = Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_DOWNLOADS); + downloads.mkdirs(); + File file = createUniqueFile(downloads, attachment.getName()); + Uri contentUri = attachment.contentUri; + InputStream in = mContext.getContentResolver().openInputStream(contentUri); + OutputStream out = new FileOutputStream(file); + IOUtils.copy(in, out); + out.flush(); + out.close(); + in.close(); + String absolutePath = file.getAbsolutePath(); + MediaScannerConnection.scanFile(mContext, new String[] {absolutePath}, + null, null); + DownloadManager dm = + (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); + dm.addCompletedDownload(attachment.getName(), attachment.getName(), + false /* do not use media scanner */, + attachment.getContentType(), absolutePath, attachment.downloadedSize, + true /* show notification */); + + return file; + } catch (IOException ioe) { + // Ignore. Callers will handle it from the return code. + } + + return null; + } } diff --git a/src/com/android/mail/browse/MessageAttachmentBar.java b/src/com/android/mail/browse/MessageAttachmentBar.java index cfcbb34b7..c4c74d093 100644 --- a/src/com/android/mail/browse/MessageAttachmentBar.java +++ b/src/com/android/mail/browse/MessageAttachmentBar.java @@ -175,7 +175,6 @@ public class MessageAttachmentBar extends FrameLayout implements OnClickListener } else if (res == R.id.save_attachment) { if (mAttachment.canSave()) { mActionHandler.startDownloadingAttachment(AttachmentDestination.EXTERNAL); - mSaveClicked = true; Analytics.getInstance().sendEvent( "save_attachment", Utils.normalizeMimeType(mAttachment.getContentType()), diff --git a/src/com/android/mail/providers/Attachment.java b/src/com/android/mail/providers/Attachment.java index 369ca4bf0..3fba99328 100644 --- a/src/com/android/mail/providers/Attachment.java +++ b/src/com/android/mail/providers/Attachment.java @@ -412,7 +412,7 @@ public class Attachment implements Parcelable { } public boolean canSave() { - return !isSavedToExternal() && !isInstallable() && !MimeType.isBlocked(getContentType()); + return !isInstallable() && !MimeType.isBlocked(getContentType()); } public boolean canShare() { |