From 937ca7e17a7dd5f2cab7118e671cd5d34567abd8 Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 4 Feb 2015 21:07:54 +0100 Subject: [PATCH] Move inner class DownloadImageTask to upper level --- .../k9/ui/messageview/DownloadImageTask.java | 150 ++++++++++++++++++ .../ui/messageview/MessageContainerView.java | 135 +--------------- 2 files changed, 151 insertions(+), 134 deletions(-) create mode 100644 k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java new file mode 100644 index 000000000..148eab1e6 --- /dev/null +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/DownloadImageTask.java @@ -0,0 +1,150 @@ +package com.fsck.k9.ui.messageview; + + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.os.AsyncTask; +import android.widget.Toast; + +import com.fsck.k9.K9; +import com.fsck.k9.R; +import com.fsck.k9.helper.FileHelper; +import com.fsck.k9.helper.UrlEncodingHelper; +import com.fsck.k9.mail.internet.MimeUtility; +import com.fsck.k9.provider.AttachmentProvider.AttachmentProviderColumns; +import org.apache.commons.io.IOUtils; + + +class DownloadImageTask extends AsyncTask { + private static final String[] ATTACHMENT_PROJECTION = new String[] { + AttachmentProviderColumns._ID, + AttachmentProviderColumns.DISPLAY_NAME + }; + private static final int DISPLAY_NAME_INDEX = 1; + + + private final Context context; + + public DownloadImageTask(Context context) { + this.context = context.getApplicationContext(); + } + + @Override + protected String doInBackground(String... params) { + String urlString = params[0]; + try { + boolean externalImage = urlString.startsWith("http"); + + String filename = null; + String mimeType = null; + InputStream in = null; + + try { + if (externalImage) { + URL url = new URL(urlString); + URLConnection conn = url.openConnection(); + in = conn.getInputStream(); + + String path = url.getPath(); + + // Try to get the filename from the URL + int start = path.lastIndexOf("/"); + if (start != -1 && start + 1 < path.length()) { + filename = UrlEncodingHelper.decodeUtf8(path.substring(start + 1)); + } else { + // Use a dummy filename if necessary + filename = "saved_image"; + } + + // Get the MIME type if we couldn't find a file extension + if (filename.indexOf('.') == -1) { + mimeType = conn.getContentType(); + } + } else { + ContentResolver contentResolver = context.getContentResolver(); + Uri uri = Uri.parse(urlString); + + // Get the filename from AttachmentProvider + Cursor cursor = contentResolver.query(uri, ATTACHMENT_PROJECTION, null, null, null); + if (cursor != null) { + try { + if (cursor.moveToNext()) { + filename = cursor.getString(DISPLAY_NAME_INDEX); + } + } finally { + cursor.close(); + } + } + + // Use a dummy filename if necessary + if (filename == null) { + filename = "saved_image"; + } + + // Get the MIME type if we couldn't find a file extension + if (filename.indexOf('.') == -1) { + mimeType = contentResolver.getType(uri); + } + + in = contentResolver.openInputStream(uri); + } + + // Do we still need an extension? + if (filename.indexOf('.') == -1) { + // Use JPEG as fallback + String extension = "jpeg"; + if (mimeType != null) { + // Try to find an extension for the given MIME type + String ext = MimeUtility.getExtensionByMimeType(mimeType); + if (ext != null) { + extension = ext; + } + } + filename += "." + extension; + } + + String sanitized = FileHelper.sanitizeFilename(filename); + + File directory = new File(K9.getAttachmentDefaultPath()); + File file = FileHelper.createUniqueFile(directory, sanitized); + FileOutputStream out = new FileOutputStream(file); + try { + IOUtils.copy(in, out); + out.flush(); + } finally { + out.close(); + } + + return file.getName(); + + } finally { + if (in != null) { + in.close(); + } + } + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + + @Override + protected void onPostExecute(String filename) { + String text; + if (filename == null) { + text = context.getString(R.string.image_saving_failed); + } else { + text = context.getString(R.string.image_saved_as, filename); + } + + Toast.makeText(context, text, Toast.LENGTH_LONG).show(); + } +} diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageContainerView.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageContainerView.java index ec2f52771..02dff2e3c 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageContainerView.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageContainerView.java @@ -1,22 +1,14 @@ package com.fsck.k9.ui.messageview; -import java.io.File; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import android.app.Activity; import android.app.Fragment; import android.content.ActivityNotFoundException; -import android.content.ContentResolver; import android.content.Context; import android.content.Intent; -import android.database.Cursor; import android.net.Uri; -import android.os.AsyncTask; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; @@ -38,22 +30,16 @@ import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast; -import com.fsck.k9.K9; import com.fsck.k9.R; import com.fsck.k9.helper.ClipboardManager; import com.fsck.k9.helper.Contacts; -import com.fsck.k9.helper.FileHelper; -import com.fsck.k9.helper.UrlEncodingHelper; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.MessagingException; -import com.fsck.k9.mail.internet.MimeUtility; import com.fsck.k9.mailstore.AttachmentViewInfo; import com.fsck.k9.mailstore.MessageViewInfo.MessageViewContainer; -import com.fsck.k9.provider.AttachmentProvider.AttachmentProviderColumns; import com.fsck.k9.view.MessageHeader.OnLayoutChangedListener; import com.fsck.k9.view.MessageWebView; -import org.apache.commons.io.IOUtils; import org.openintents.openpgp.OpenPgpError; @@ -75,12 +61,6 @@ public class MessageContainerView extends LinearLayout implements OnClickListene private static final int MENU_ITEM_EMAIL_SAVE = Menu.FIRST + 1; private static final int MENU_ITEM_EMAIL_COPY = Menu.FIRST + 2; - private static final String[] ATTACHMENT_PROJECTION = new String[] { - AttachmentProviderColumns._ID, - AttachmentProviderColumns.DISPLAY_NAME - }; - private static final int DISPLAY_NAME_INDEX = 1; - private ViewStub mOpenPgpHeaderStub; private View mSidebar; private MessageWebView mMessageContentView; @@ -240,7 +220,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene break; } case MENU_ITEM_IMAGE_SAVE: { - new DownloadImageTask().execute(url); + new DownloadImageTask(getContext()).execute(url); break; } case MENU_ITEM_IMAGE_COPY: { @@ -659,117 +639,4 @@ public class MessageContainerView extends LinearLayout implements OnClickListene out.writeInt((this.showPictures) ? 1 : 0); } } - - class DownloadImageTask extends AsyncTask { - @Override - protected String doInBackground(String... params) { - String urlString = params[0]; - try { - boolean externalImage = urlString.startsWith("http"); - - String filename = null; - String mimeType = null; - InputStream in = null; - - try { - if (externalImage) { - URL url = new URL(urlString); - URLConnection conn = url.openConnection(); - in = conn.getInputStream(); - - String path = url.getPath(); - - // Try to get the filename from the URL - int start = path.lastIndexOf("/"); - if (start != -1 && start + 1 < path.length()) { - filename = UrlEncodingHelper.decodeUtf8(path.substring(start + 1)); - } else { - // Use a dummy filename if necessary - filename = "saved_image"; - } - - // Get the MIME type if we couldn't find a file extension - if (filename.indexOf('.') == -1) { - mimeType = conn.getContentType(); - } - } else { - ContentResolver contentResolver = getContext().getContentResolver(); - Uri uri = Uri.parse(urlString); - - // Get the filename from AttachmentProvider - Cursor cursor = contentResolver.query(uri, ATTACHMENT_PROJECTION, null, null, null); - if (cursor != null) { - try { - if (cursor.moveToNext()) { - filename = cursor.getString(DISPLAY_NAME_INDEX); - } - } finally { - cursor.close(); - } - } - - // Use a dummy filename if necessary - if (filename == null) { - filename = "saved_image"; - } - - // Get the MIME type if we couldn't find a file extension - if (filename.indexOf('.') == -1) { - mimeType = contentResolver.getType(uri); - } - - in = contentResolver.openInputStream(uri); - } - - // Do we still need an extension? - if (filename.indexOf('.') == -1) { - // Use JPEG as fallback - String extension = "jpeg"; - if (mimeType != null) { - // Try to find an extension for the given MIME type - String ext = MimeUtility.getExtensionByMimeType(mimeType); - if (ext != null) { - extension = ext; - } - } - filename += "." + extension; - } - - String sanitized = FileHelper.sanitizeFilename(filename); - - File directory = new File(K9.getAttachmentDefaultPath()); - File file = FileHelper.createUniqueFile(directory, sanitized); - FileOutputStream out = new FileOutputStream(file); - try { - IOUtils.copy(in, out); - out.flush(); - } finally { - out.close(); - } - - return file.getName(); - - } finally { - if (in != null) { - in.close(); - } - } - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - - @Override - protected void onPostExecute(String filename) { - String text; - if (filename == null) { - text = getContext().getString(R.string.image_saving_failed); - } else { - text = getContext().getString(R.string.image_saved_as, filename); - } - - Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show(); - } - } }