From 2accaae9015ad8b6591ec01afb4342edd96e6c8e Mon Sep 17 00:00:00 2001 From: harjot-oberai Date: Thu, 2 Mar 2017 04:48:28 +0530 Subject: [PATCH 1/5] Add percentage progress bar for attachment downloads --- .../k9/mail/AttachmentProgressCallback.java | 5 ++ .../main/java/com/fsck/k9/mail/Folder.java | 3 +- .../fsck/k9/mail/internet/MimeMessage.java | 2 +- .../fsck/k9/mail/internet/MimeUtility.java | 25 +++++- .../k9/mail/store/imap/FetchPartCallback.java | 13 ++- .../fsck/k9/mail/store/imap/ImapFolder.java | 7 +- .../k9/mail/store/imap/ImapFolderTest.java | 6 +- .../com/fsck/k9/activity/MessageCompose.java | 9 +- .../k9/controller/MessagingController.java | 21 ++++- .../fsck/k9/controller/MessagingListener.java | 2 + .../controller/SimpleMessagingListener.java | 5 ++ .../AttachmentDownloadDialogFragment.java | 83 +++++++++++++++++++ .../ui/messageview/MessageViewFragment.java | 6 +- 13 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java create mode 100644 k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java b/k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java new file mode 100644 index 000000000..e85f6e448 --- /dev/null +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java @@ -0,0 +1,5 @@ +package com.fsck.k9.mail; + +public interface AttachmentProgressCallback { + void onUpdate(int progress); +} diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java index 6545a7547..2edf6fe81 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java @@ -128,7 +128,8 @@ public abstract class Folder { MessageRetrievalListener listener) throws MessagingException; public void fetchPart(Message message, Part part, - MessageRetrievalListener listener) throws MessagingException { + MessageRetrievalListener listener, + AttachmentProgressCallback progressCallback) throws MessagingException { // This is causing trouble. Disabled for now. See issue 1733 //throw new RuntimeException("fetchPart() not implemented."); diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java index 557927734..98a78e183 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java @@ -576,7 +576,7 @@ public class MimeMessage extends Message { @Override public void body(BodyDescriptor bd, InputStream in) throws IOException, MimeException { expect(Part.class); - Body body = MimeUtility.createBody(in, bd.getTransferEncoding(), bd.getMimeType()); + Body body = MimeUtility.createBody(in, bd.getTransferEncoding(), bd.getMimeType(), null); ((Part)stack.peek()).setBody(body); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java index 13ee30a27..98431c945 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java @@ -6,17 +6,22 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Locale; +import java.util.Timer; +import java.util.TimerTask; import java.util.regex.Pattern; import android.support.annotation.NonNull; +import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; + import org.apache.commons.io.IOUtils; +import org.apache.commons.io.output.CountingOutputStream; import org.apache.james.mime4j.codec.Base64InputStream; import org.apache.james.mime4j.codec.QuotedPrintableInputStream; import org.apache.james.mime4j.util.MimeUtil; @@ -985,14 +990,14 @@ public class MimeUtility { return isSameMimeType(mimeType, DEFAULT_ATTACHMENT_MIME_TYPE); } - public static Body createBody(InputStream in, String contentTransferEncoding, String contentType) + public static Body createBody(InputStream in, String contentTransferEncoding, String contentType, final AttachmentProgressCallback progressCallback) throws IOException { if (contentTransferEncoding != null) { contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null); } - BinaryTempFileBody tempBody; + final BinaryTempFileBody tempBody; if (MimeUtil.isMessage(contentType)) { tempBody = new BinaryTempFileMessageBody(contentTransferEncoding); } else { @@ -1000,9 +1005,23 @@ public class MimeUtility { } OutputStream out = tempBody.getOutputStream(); + final CountingOutputStream countingOutputStream = new CountingOutputStream(out); + Timer timer = null; try { - IOUtils.copy(in, out); + if (progressCallback != null) { + timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + progressCallback.onUpdate((int) countingOutputStream.getCount()); + } + }, 0, 50); + } + IOUtils.copy(in, countingOutputStream); } finally { + if (timer != null) { + timer.cancel(); + } out.close(); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java index 2ecd2ef50..abed997b2 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java @@ -3,6 +3,7 @@ package com.fsck.k9.mail.store.imap; import java.io.IOException; +import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.Part; import com.fsck.k9.mail.filter.FixedLengthInputStream; import com.fsck.k9.mail.internet.MimeHeader; @@ -11,11 +12,17 @@ import com.fsck.k9.mail.internet.MimeUtility; class FetchPartCallback implements ImapResponseCallback { private Part mPart; + private AttachmentProgressCallback attachmentProgressCallback; FetchPartCallback(Part part) { mPart = part; } + FetchPartCallback(Part part, AttachmentProgressCallback attachmentProgressCallback) { + mPart = part; + this.attachmentProgressCallback = attachmentProgressCallback; + } + @Override public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws IOException { if (response.getTag() == null && @@ -27,7 +34,11 @@ class FetchPartCallback implements ImapResponseCallback { String contentType = mPart .getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0]; - return MimeUtility.createBody(literal, contentTransferEncoding, contentType); + if (attachmentProgressCallback != null) { + return MimeUtility.createBody(literal, contentTransferEncoding, contentType, attachmentProgressCallback); + } + + return MimeUtility.createBody(literal, contentTransferEncoding, contentType, null); } return null; } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java index 44bbb8347..5002db525 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java @@ -18,6 +18,7 @@ import java.util.concurrent.ConcurrentHashMap; import android.text.TextUtils; +import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.FetchProfile; import com.fsck.k9.mail.Flag; @@ -779,7 +780,7 @@ class ImapFolder extends Folder { } @Override - public void fetchPart(Message message, Part part, MessageRetrievalListener listener) + public void fetchPart(Message message, Part part, MessageRetrievalListener listener, AttachmentProgressCallback progressCallback) throws MessagingException { checkOpen(); @@ -800,7 +801,7 @@ class ImapFolder extends Folder { ImapResponse response; int messageNumber = 0; - ImapResponseCallback callback = new FetchPartCallback(part); + ImapResponseCallback callback = new FetchPartCallback(part, progressCallback); do { response = connection.readResponse(callback); @@ -838,7 +839,7 @@ class ImapFolder extends Folder { part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0]; String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0]; MimeMessageHelper.setBody(part, MimeUtility.createBody(bodyStream, contentTransferEncoding, - contentType)); + contentType, progressCallback)); } else { // This shouldn't happen throw new MessagingException("Got FETCH response with bogus parameters"); diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java index 25bfcdabd..ce10c829a 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java @@ -934,7 +934,7 @@ public class ImapFolderTest { Part part = createPart("TEXT"); when(imapConnection.readResponse(any(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); - folder.fetchPart(message, part, null); + folder.fetchPart(message, part, null, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[TEXT]<0.4096>)", false); } @@ -948,7 +948,7 @@ public class ImapFolderTest { Part part = createPart("1.1"); when(imapConnection.readResponse(any(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); - folder.fetchPart(message, part, null); + folder.fetchPart(message, part, null, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[1.1])", false); } @@ -962,7 +962,7 @@ public class ImapFolderTest { Part part = createPlainTextPart("1.1"); setupSingleFetchResponseToCallback(); - folder.fetchPart(message, part, null); + folder.fetchPart(message, part, null, null); ArgumentCaptor bodyArgumentCaptor = ArgumentCaptor.forClass(Body.class); verify(part).setBody(bodyArgumentCaptor.capture()); diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java index 5f254d64b..822a3290c 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -69,8 +69,11 @@ import com.fsck.k9.activity.misc.Attachment; import com.fsck.k9.controller.MessagingController; import com.fsck.k9.controller.MessagingListener; import com.fsck.k9.controller.SimpleMessagingListener; +import com.fsck.k9.fragment.AttachmentDownloadDialogFragment; import com.fsck.k9.fragment.ProgressDialogFragment; import com.fsck.k9.fragment.ProgressDialogFragment.CancelListener; +import com.fsck.k9.fragment.AttachmentDownloadDialogFragment; +import com.fsck.k9.fragment.AttachmentDownloadDialogFragment.AttachmentDownloadCancelListener; import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.IdentityHelper; import com.fsck.k9.helper.MailTo; @@ -101,7 +104,7 @@ import com.fsck.k9.ui.compose.QuotedMessagePresenter; @SuppressWarnings("deprecation") // TODO get rid of activity dialogs and indeterminate progress bars public class MessageCompose extends K9Activity implements OnClickListener, - CancelListener, OnFocusChangeListener, OnCryptoModeChangedListener, + CancelListener, AttachmentDownloadCancelListener, OnFocusChangeListener, OnCryptoModeChangedListener, OnOpenPgpInlineChangeListener, OnOpenPgpSignOnlyChangeListener, MessageBuilder.Callback, AttachmentPresenter.AttachmentsChangedListener, RecipientPresenter.RecipientsChangedListener { @@ -1053,6 +1056,10 @@ public class MessageCompose extends K9Activity implements OnClickListener, return false; } + @Override + public void onProgressCancel(AttachmentDownloadDialogFragment fragment) { + attachmentPresenter.attachmentProgressDialogCancelled(); + } public void onProgressCancel(ProgressDialogFragment fragment) { attachmentPresenter.attachmentProgressDialogCancelled(); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 01facaee7..63ef13053 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -18,6 +18,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; @@ -63,8 +65,10 @@ import com.fsck.k9.controller.MessagingControllerCommands.PendingExpunge; import com.fsck.k9.controller.MessagingControllerCommands.PendingMarkAllAsRead; import com.fsck.k9.controller.MessagingControllerCommands.PendingMoveOrCopy; import com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag; +import com.fsck.k9.fragment.AttachmentDownloadDialogFragment; import com.fsck.k9.helper.Contacts; import com.fsck.k9.mail.Address; +import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.AuthenticationFailedException; import com.fsck.k9.mail.CertificateValidationException; import com.fsck.k9.mail.FetchProfile; @@ -150,6 +154,8 @@ public class MessagingController { private MessagingListener checkMailListener = null; private volatile boolean stopped = false; + private MessagingListener attachmentDownloadProgressListener = null; + public static synchronized MessagingController getInstance(Context context) { if (inst == null) { @@ -259,6 +265,10 @@ public class MessagingController { throw new Error(e); } + public void addDownloadProgressListener(MessagingListener messagingListener){ + attachmentDownloadProgressListener = messagingListener; + } + public void addListener(MessagingListener listener) { listeners.add(listener); refreshListener(listener); @@ -1483,7 +1493,7 @@ public class MessagingController { * Now download the parts we're interested in storing. */ for (Part part : viewables) { - remoteFolder.fetchPart(message, part, null); + remoteFolder.fetchPart(message, part, null, null); } // Store the updated message locally localFolder.appendMessages(Collections.singletonList(message)); @@ -2551,8 +2561,15 @@ public class MessagingController { remoteFolder = remoteStore.getFolder(folderName); remoteFolder.open(Folder.OPEN_MODE_RW); + AttachmentProgressCallback attachmentProgressCallback = new AttachmentProgressCallback() { + @Override + public void onUpdate(int progress) { + attachmentDownloadProgressListener.updateProgress(progress); + } + }; + Message remoteMessage = remoteFolder.getMessage(message.getUid()); - remoteFolder.fetchPart(remoteMessage, part, null); + remoteFolder.fetchPart(remoteMessage, part, null, attachmentProgressCallback); localFolder.addPartToMessage(message, part); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingListener.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingListener.java index 8cc15e70a..6a3e3f97a 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingListener.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingListener.java @@ -73,4 +73,6 @@ public interface MessagingListener { void remoteSearchFailed(String folder, String err); void enableProgressIndicator(boolean enable); + + void updateProgress(int progress); } diff --git a/k9mail/src/main/java/com/fsck/k9/controller/SimpleMessagingListener.java b/k9mail/src/main/java/com/fsck/k9/controller/SimpleMessagingListener.java index 785191b84..3998981ed 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/SimpleMessagingListener.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/SimpleMessagingListener.java @@ -181,4 +181,9 @@ public abstract class SimpleMessagingListener implements MessagingListener { @Override public void enableProgressIndicator(boolean enable) { } + + @Override + public void updateProgress(int progress) { + + } } diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java new file mode 100644 index 000000000..ce5426f1e --- /dev/null +++ b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java @@ -0,0 +1,83 @@ +package com.fsck.k9.fragment; + +import android.app.Activity; +import android.app.Dialog; +import android.app.DialogFragment; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.Bundle; + +import com.fsck.k9.controller.MessagingController; +import com.fsck.k9.controller.MessagingListener; +import com.fsck.k9.controller.SimpleMessagingListener; + +public class AttachmentDownloadDialogFragment extends DialogFragment { + + ProgressDialog dialog; + + int progress = 0; + + MessagingListener messagingListener; + + protected static final String ARG_SIZE = "size"; + protected static final String ARG_MESSAGE = "message"; + + public static AttachmentDownloadDialogFragment newInstance(int size, String message) { + AttachmentDownloadDialogFragment attachmentDownloadDialogFragment = new AttachmentDownloadDialogFragment(); + + Bundle args = new Bundle(); + args.putInt(ARG_SIZE, size); + args.putString(ARG_MESSAGE, message); + attachmentDownloadDialogFragment.setArguments(args); + + return attachmentDownloadDialogFragment; + } + + public void updateDownloadProgress(int newProgress) { + progress = newProgress; + dialog.setProgress(newProgress); + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Bundle args = getArguments(); + int size = args.getInt(ARG_SIZE); + String message = args.getString(ARG_MESSAGE); + + progress = 0; + + messagingListener = new SimpleMessagingListener() { + @Override + public void updateProgress(int progress) { + updateDownloadProgress(progress); + } + }; + + MessagingController.getInstance(getActivity()).addDownloadProgressListener(messagingListener); + + dialog = new ProgressDialog(getActivity()); + dialog.setMessage(message); + dialog.setMax(size); + dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + dialog.setProgress(0); + dialog.show(); + + return dialog; + } + + @Override + public void onCancel(DialogInterface dialog) { + Activity activity = getActivity(); + if (activity != null && activity instanceof AttachmentDownloadCancelListener) { + AttachmentDownloadCancelListener listener = (AttachmentDownloadCancelListener) activity; + listener.onProgressCancel(this); + } + + super.onCancel(dialog); + } + + public interface AttachmentDownloadCancelListener { + void onProgressCancel(AttachmentDownloadDialogFragment fragment); + } + +} diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index 35b7bf4cb..bbba057bf 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -37,6 +37,7 @@ import com.fsck.k9.activity.MessageLoaderHelper; import com.fsck.k9.activity.MessageLoaderHelper.MessageLoaderCallbacks; import com.fsck.k9.activity.MessageReference; import com.fsck.k9.controller.MessagingController; +import com.fsck.k9.fragment.AttachmentDownloadDialogFragment; import com.fsck.k9.fragment.ConfirmationDialogFragment; import com.fsck.k9.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener; import com.fsck.k9.fragment.ProgressDialogFragment; @@ -534,7 +535,8 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF } case R.id.dialog_attachment_progress: { String message = getString(R.string.dialog_attachment_progress_title); - fragment = ProgressDialogFragment.newInstance(null, message); + int size = (int) currentAttachmentViewInfo.size; + fragment = AttachmentDownloadDialogFragment.newInstance(size, message); break; } default: { @@ -796,7 +798,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF @Override public void onViewAttachment(AttachmentViewInfo attachment) { //TODO: check if we have to download the attachment first - + currentAttachmentViewInfo = attachment; getAttachmentController(attachment).viewAttachment(); } From bb16b1da3beca2609d3104162f020c34a04a1394 Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 26 Mar 2017 08:33:28 +0200 Subject: [PATCH 2/5] Change the way we're tracking attachment download progress --- .../k9/mail/AttachmentProgressCallback.java | 5 --- .../java/com/fsck/k9/mail/BodyFactory.java | 10 +++++ .../com/fsck/k9/mail/DefaultBodyFactory.java | 43 ++++++++++++++++++ .../main/java/com/fsck/k9/mail/Folder.java | 5 +-- .../fsck/k9/mail/internet/MimeMessage.java | 10 +++-- .../fsck/k9/mail/internet/MimeUtility.java | 44 ------------------- .../k9/mail/store/imap/FetchPartCallback.java | 31 +++++-------- .../fsck/k9/mail/store/imap/ImapFolder.java | 14 +++--- .../k9/mail/store/imap/ImapFolderTest.java | 3 +- .../k9/controller/MessagingController.java | 28 +++++------- .../k9/controller/ProgressBodyFactory.java | 44 +++++++++++++++++++ .../AttachmentDownloadDialogFragment.java | 10 ++++- 12 files changed, 146 insertions(+), 101 deletions(-) delete mode 100644 k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java create mode 100644 k9mail-library/src/main/java/com/fsck/k9/mail/BodyFactory.java create mode 100644 k9mail-library/src/main/java/com/fsck/k9/mail/DefaultBodyFactory.java create mode 100644 k9mail/src/main/java/com/fsck/k9/controller/ProgressBodyFactory.java diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java b/k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java deleted file mode 100644 index e85f6e448..000000000 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/AttachmentProgressCallback.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.fsck.k9.mail; - -public interface AttachmentProgressCallback { - void onUpdate(int progress); -} diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/BodyFactory.java b/k9mail-library/src/main/java/com/fsck/k9/mail/BodyFactory.java new file mode 100644 index 000000000..c11a74e0a --- /dev/null +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/BodyFactory.java @@ -0,0 +1,10 @@ +package com.fsck.k9.mail; + + +import java.io.IOException; +import java.io.InputStream; + + +public interface BodyFactory { + Body createBody(String contentTransferEncoding, String contentType, InputStream inputStream) throws IOException; +} diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/DefaultBodyFactory.java b/k9mail-library/src/main/java/com/fsck/k9/mail/DefaultBodyFactory.java new file mode 100644 index 000000000..1cbffbe35 --- /dev/null +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/DefaultBodyFactory.java @@ -0,0 +1,43 @@ +package com.fsck.k9.mail; + + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import com.fsck.k9.mail.internet.BinaryTempFileBody; +import com.fsck.k9.mail.internet.BinaryTempFileMessageBody; +import com.fsck.k9.mail.internet.MimeUtility; +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.util.MimeUtil; + + +public class DefaultBodyFactory implements BodyFactory { + public Body createBody(String contentTransferEncoding, String contentType, InputStream inputStream) + throws IOException { + + if (contentTransferEncoding != null) { + contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null); + } + + final BinaryTempFileBody tempBody; + if (MimeUtil.isMessage(contentType)) { + tempBody = new BinaryTempFileMessageBody(contentTransferEncoding); + } else { + tempBody = new BinaryTempFileBody(contentTransferEncoding); + } + + OutputStream outputStream = tempBody.getOutputStream(); + try { + copyData(inputStream, outputStream); + } finally { + outputStream.close(); + } + + return tempBody; + } + + protected void copyData(InputStream inputStream, OutputStream outputStream) throws IOException { + IOUtils.copy(inputStream, outputStream); + } +} diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java index 2edf6fe81..f15830120 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/Folder.java @@ -127,9 +127,8 @@ public abstract class Folder { public abstract void fetch(List messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException; - public void fetchPart(Message message, Part part, - MessageRetrievalListener listener, - AttachmentProgressCallback progressCallback) throws MessagingException { + public void fetchPart(Message message, Part part, MessageRetrievalListener listener, + BodyFactory bodyFactory) throws MessagingException { // This is causing trouble. Disabled for now. See issue 1733 //throw new RuntimeException("fetchPart() not implemented."); diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java index 98a78e183..01d6a293c 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeMessage.java @@ -19,7 +19,9 @@ import android.support.annotation.NonNull; import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.BodyFactory; import com.fsck.k9.mail.BodyPart; +import com.fsck.k9.mail.DefaultBodyFactory; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Multipart; @@ -106,7 +108,7 @@ public class MimeMessage extends Message { // REALLY long References: headers parserConfig.setMaxHeaderCount(-1); // Disable the check for header count. MimeStreamParser parser = new MimeStreamParser(parserConfig); - parser.setContentHandler(new MimeMessageBuilder()); + parser.setContentHandler(new MimeMessageBuilder(new DefaultBodyFactory())); if (recurse) { parser.setRecurse(); } @@ -520,8 +522,10 @@ public class MimeMessage extends Message { private class MimeMessageBuilder implements ContentHandler { private final LinkedList stack = new LinkedList<>(); + private final BodyFactory bodyFactory; - public MimeMessageBuilder() { + public MimeMessageBuilder(BodyFactory bodyFactory) { + this.bodyFactory = bodyFactory; } private void expect(Class c) { @@ -576,7 +580,7 @@ public class MimeMessage extends Message { @Override public void body(BodyDescriptor bd, InputStream in) throws IOException, MimeException { expect(Part.class); - Body body = MimeUtility.createBody(in, bd.getTransferEncoding(), bd.getMimeType(), null); + Body body = bodyFactory.createBody(bd.getTransferEncoding(), bd.getMimeType(), in); ((Part)stack.peek()).setBody(body); } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java index 98431c945..66ab042dc 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/internet/MimeUtility.java @@ -4,15 +4,11 @@ package com.fsck.k9.mail.internet; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.util.Locale; -import java.util.Timer; -import java.util.TimerTask; import java.util.regex.Pattern; import android.support.annotation.NonNull; -import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.Body; import com.fsck.k9.mail.BodyPart; import com.fsck.k9.mail.Message; @@ -20,8 +16,6 @@ import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.Multipart; import com.fsck.k9.mail.Part; -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.output.CountingOutputStream; import org.apache.james.mime4j.codec.Base64InputStream; import org.apache.james.mime4j.codec.QuotedPrintableInputStream; import org.apache.james.mime4j.util.MimeUtil; @@ -990,44 +984,6 @@ public class MimeUtility { return isSameMimeType(mimeType, DEFAULT_ATTACHMENT_MIME_TYPE); } - public static Body createBody(InputStream in, String contentTransferEncoding, String contentType, final AttachmentProgressCallback progressCallback) - throws IOException { - - if (contentTransferEncoding != null) { - contentTransferEncoding = MimeUtility.getHeaderParameter(contentTransferEncoding, null); - } - - final BinaryTempFileBody tempBody; - if (MimeUtil.isMessage(contentType)) { - tempBody = new BinaryTempFileMessageBody(contentTransferEncoding); - } else { - tempBody = new BinaryTempFileBody(contentTransferEncoding); - } - - OutputStream out = tempBody.getOutputStream(); - final CountingOutputStream countingOutputStream = new CountingOutputStream(out); - Timer timer = null; - try { - if (progressCallback != null) { - timer = new Timer(); - timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - progressCallback.onUpdate((int) countingOutputStream.getCount()); - } - }, 0, 50); - } - IOUtils.copy(in, countingOutputStream); - } finally { - if (timer != null) { - timer.cancel(); - } - out.close(); - } - - return tempBody; - } - /** * Get decoded contents of a body. *

diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java index abed997b2..3b38ee579 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/FetchPartCallback.java @@ -3,42 +3,31 @@ package com.fsck.k9.mail.store.imap; import java.io.IOException; -import com.fsck.k9.mail.AttachmentProgressCallback; +import com.fsck.k9.mail.BodyFactory; import com.fsck.k9.mail.Part; import com.fsck.k9.mail.filter.FixedLengthInputStream; import com.fsck.k9.mail.internet.MimeHeader; -import com.fsck.k9.mail.internet.MimeUtility; class FetchPartCallback implements ImapResponseCallback { - private Part mPart; - private AttachmentProgressCallback attachmentProgressCallback; + private final Part part; + private final BodyFactory bodyFactory; - FetchPartCallback(Part part) { - mPart = part; - } - FetchPartCallback(Part part, AttachmentProgressCallback attachmentProgressCallback) { - mPart = part; - this.attachmentProgressCallback = attachmentProgressCallback; + FetchPartCallback(Part part, BodyFactory bodyFactory) { + this.part = part; + this.bodyFactory = bodyFactory; } @Override public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws IOException { - if (response.getTag() == null && - ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) { + if (response.getTag() == null && ImapResponseParser.equalsIgnoreCase(response.get(1), "FETCH")) { //TODO: check for correct UID - String contentTransferEncoding = mPart - .getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0]; - String contentType = mPart - .getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0]; + String contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0]; + String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0]; - if (attachmentProgressCallback != null) { - return MimeUtility.createBody(literal, contentTransferEncoding, contentType, attachmentProgressCallback); - } - - return MimeUtility.createBody(literal, contentTransferEncoding, contentType, null); + return bodyFactory.createBody(contentTransferEncoding, contentType, literal); } return null; } diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java index 5002db525..6736747db 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolder.java @@ -18,8 +18,8 @@ import java.util.concurrent.ConcurrentHashMap; import android.text.TextUtils; -import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.BodyFactory; import com.fsck.k9.mail.FetchProfile; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Folder; @@ -780,8 +780,8 @@ class ImapFolder extends Folder { } @Override - public void fetchPart(Message message, Part part, MessageRetrievalListener listener, AttachmentProgressCallback progressCallback) - throws MessagingException { + public void fetchPart(Message message, Part part, MessageRetrievalListener listener, + BodyFactory bodyFactory) throws MessagingException { checkOpen(); String partId = part.getServerExtra(); @@ -801,7 +801,7 @@ class ImapFolder extends Folder { ImapResponse response; int messageNumber = 0; - ImapResponseCallback callback = new FetchPartCallback(part, progressCallback); + ImapResponseCallback callback = new FetchPartCallback(part, bodyFactory); do { response = connection.readResponse(callback); @@ -829,7 +829,7 @@ class ImapFolder extends Folder { if (literal != null) { if (literal instanceof Body) { - // Most of the work was done in FetchAttchmentCallback.foundLiteral() + // Most of the work was done in FetchAttachmentCallback.foundLiteral() MimeMessageHelper.setBody(part, (Body) literal); } else if (literal instanceof String) { String bodyString = (String) literal; @@ -838,8 +838,8 @@ class ImapFolder extends Folder { String contentTransferEncoding = part.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)[0]; String contentType = part.getHeader(MimeHeader.HEADER_CONTENT_TYPE)[0]; - MimeMessageHelper.setBody(part, MimeUtility.createBody(bodyStream, contentTransferEncoding, - contentType, progressCallback)); + Body body = bodyFactory.createBody(contentTransferEncoding, contentType, bodyStream); + MimeMessageHelper.setBody(part, body); } else { // This shouldn't happen throw new MessagingException("Got FETCH response with bogus parameters"); diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java index ce10c829a..11d50e859 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/imap/ImapFolderTest.java @@ -12,6 +12,7 @@ import java.util.Set; import java.util.TimeZone; import com.fsck.k9.mail.Body; +import com.fsck.k9.mail.DefaultBodyFactory; import com.fsck.k9.mail.FetchProfile; import com.fsck.k9.mail.FetchProfile.Item; import com.fsck.k9.mail.Flag; @@ -962,7 +963,7 @@ public class ImapFolderTest { Part part = createPlainTextPart("1.1"); setupSingleFetchResponseToCallback(); - folder.fetchPart(message, part, null, null); + folder.fetchPart(message, part, null, new DefaultBodyFactory()); ArgumentCaptor bodyArgumentCaptor = ArgumentCaptor.forClass(Body.class); verify(part).setBody(bodyArgumentCaptor.capture()); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 63ef13053..83802b033 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -18,8 +18,6 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import java.util.Timer; -import java.util.TimerTask; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; @@ -65,12 +63,13 @@ import com.fsck.k9.controller.MessagingControllerCommands.PendingExpunge; import com.fsck.k9.controller.MessagingControllerCommands.PendingMarkAllAsRead; import com.fsck.k9.controller.MessagingControllerCommands.PendingMoveOrCopy; import com.fsck.k9.controller.MessagingControllerCommands.PendingSetFlag; -import com.fsck.k9.fragment.AttachmentDownloadDialogFragment; +import com.fsck.k9.controller.ProgressBodyFactory.ProgressListener; import com.fsck.k9.helper.Contacts; import com.fsck.k9.mail.Address; -import com.fsck.k9.mail.AttachmentProgressCallback; import com.fsck.k9.mail.AuthenticationFailedException; +import com.fsck.k9.mail.BodyFactory; import com.fsck.k9.mail.CertificateValidationException; +import com.fsck.k9.mail.DefaultBodyFactory; import com.fsck.k9.mail.FetchProfile; import com.fsck.k9.mail.FetchProfile.Item; import com.fsck.k9.mail.Flag; @@ -154,8 +153,6 @@ public class MessagingController { private MessagingListener checkMailListener = null; private volatile boolean stopped = false; - private MessagingListener attachmentDownloadProgressListener = null; - public static synchronized MessagingController getInstance(Context context) { if (inst == null) { @@ -265,10 +262,6 @@ public class MessagingController { throw new Error(e); } - public void addDownloadProgressListener(MessagingListener messagingListener){ - attachmentDownloadProgressListener = messagingListener; - } - public void addListener(MessagingListener listener) { listeners.add(listener); refreshListener(listener); @@ -1492,8 +1485,9 @@ public class MessagingController { /* * Now download the parts we're interested in storing. */ + BodyFactory bodyFactory = new DefaultBodyFactory(); for (Part part : viewables) { - remoteFolder.fetchPart(message, part, null, null); + remoteFolder.fetchPart(message, part, null, bodyFactory); } // Store the updated message locally localFolder.appendMessages(Collections.singletonList(message)); @@ -2561,15 +2555,17 @@ public class MessagingController { remoteFolder = remoteStore.getFolder(folderName); remoteFolder.open(Folder.OPEN_MODE_RW); - AttachmentProgressCallback attachmentProgressCallback = new AttachmentProgressCallback() { + ProgressBodyFactory bodyFactory = new ProgressBodyFactory(new ProgressListener() { @Override - public void onUpdate(int progress) { - attachmentDownloadProgressListener.updateProgress(progress); + public void updateProgress(int progress) { + for (MessagingListener listener : getListeners()) { + listener.updateProgress(progress); + } } - }; + }); Message remoteMessage = remoteFolder.getMessage(message.getUid()); - remoteFolder.fetchPart(remoteMessage, part, null, attachmentProgressCallback); + remoteFolder.fetchPart(remoteMessage, part, null, bodyFactory); localFolder.addPartToMessage(message, part); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/ProgressBodyFactory.java b/k9mail/src/main/java/com/fsck/k9/controller/ProgressBodyFactory.java new file mode 100644 index 000000000..606541f27 --- /dev/null +++ b/k9mail/src/main/java/com/fsck/k9/controller/ProgressBodyFactory.java @@ -0,0 +1,44 @@ +package com.fsck.k9.controller; + + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Timer; +import java.util.TimerTask; + +import com.fsck.k9.mail.DefaultBodyFactory; +import org.apache.commons.io.output.CountingOutputStream; + + +class ProgressBodyFactory extends DefaultBodyFactory { + private final ProgressListener progressListener; + + + ProgressBodyFactory(ProgressListener progressListener) { + this.progressListener = progressListener; + } + + @Override + protected void copyData(InputStream inputStream, OutputStream outputStream) throws IOException { + final CountingOutputStream countingOutputStream = new CountingOutputStream(outputStream); + + Timer timer = new Timer(); + try { + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + progressListener.updateProgress(countingOutputStream.getCount()); + } + }, 0, 50); + + super.copyData(inputStream, countingOutputStream); + } finally { + timer.cancel(); + } + } + + interface ProgressListener { + void updateProgress(int progress); + } +} diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java index ce5426f1e..fc0b04c73 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java @@ -21,6 +21,7 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { protected static final String ARG_SIZE = "size"; protected static final String ARG_MESSAGE = "message"; + private MessagingController messagingController; public static AttachmentDownloadDialogFragment newInstance(int size, String message) { AttachmentDownloadDialogFragment attachmentDownloadDialogFragment = new AttachmentDownloadDialogFragment(); @@ -53,7 +54,8 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { } }; - MessagingController.getInstance(getActivity()).addDownloadProgressListener(messagingListener); + messagingController = MessagingController.getInstance(getActivity()); + messagingController.addListener(messagingListener); dialog = new ProgressDialog(getActivity()); dialog.setMessage(message); @@ -65,6 +67,12 @@ public class AttachmentDownloadDialogFragment extends DialogFragment { return dialog; } + @Override + public void onDestroyView() { + messagingController.removeListener(messagingListener); + super.onDestroyView(); + } + @Override public void onCancel(DialogInterface dialog) { Activity activity = getActivity(); From 336da9438609ba66f78a0fe3d814946d69e83d77 Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 13 Apr 2017 19:15:33 +0200 Subject: [PATCH 3/5] Fix crash when clicking "Save" on attachment that needs to be downloaded --- .../java/com/fsck/k9/ui/messageview/MessageViewFragment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index bbba057bf..620bc0dd7 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -805,7 +805,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF @Override public void onSaveAttachment(AttachmentViewInfo attachment) { //TODO: check if we have to download the attachment first - + currentAttachmentViewInfo = attachment; getAttachmentController(attachment).saveAttachment(); } From f69ef280badb856fe11b957b2a0d4876c9fb0e7d Mon Sep 17 00:00:00 2001 From: cketti Date: Thu, 13 Apr 2017 19:56:29 +0200 Subject: [PATCH 4/5] Avoid downloading attachments multiple times Mark an attachment as downloaded so repeated clicks on "Open" or "Save" won't download the attachment again. --- .../k9/activity/compose/AttachmentPresenter.java | 2 +- .../com/fsck/k9/mailstore/AttachmentViewInfo.java | 14 +++++++++++--- .../k9/ui/messageview/AttachmentController.java | 5 +++-- .../extractors/AttachmentInfoExtractorTest.java | 6 +++--- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/compose/AttachmentPresenter.java b/k9mail/src/main/java/com/fsck/k9/activity/compose/AttachmentPresenter.java index 1ea3481e0..44db23a5a 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/compose/AttachmentPresenter.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/compose/AttachmentPresenter.java @@ -162,7 +162,7 @@ public class AttachmentPresenter { if (attachmentViewInfo.inlineAttachment) { continue; } - if (!attachmentViewInfo.isContentAvailable) { + if (!attachmentViewInfo.isContentAvailable()) { allPartsAvailable = false; continue; } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java b/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java index 189225e73..31b04d405 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/AttachmentViewInfo.java @@ -21,16 +21,24 @@ public class AttachmentViewInfo { public final Uri internalUri; public final boolean inlineAttachment; public final Part part; - public final boolean isContentAvailable; + private boolean contentAvailable; public AttachmentViewInfo(String mimeType, String displayName, long size, Uri internalUri, boolean inlineAttachment, - Part part, boolean isContentAvailable) { + Part part, boolean contentAvailable) { this.mimeType = mimeType; this.displayName = displayName; this.size = size; this.internalUri = internalUri; this.inlineAttachment = inlineAttachment; this.part = part; - this.isContentAvailable = isContentAvailable; + this.contentAvailable = contentAvailable; + } + + public boolean isContentAvailable() { + return contentAvailable; + } + + public void setContentAvailable() { + this.contentAvailable = true; } } diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java index 5164e691f..e4f5f2bc9 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/AttachmentController.java @@ -57,7 +57,7 @@ public class AttachmentController { } public void viewAttachment() { - if (!attachment.isContentAvailable) { + if (!attachment.isContentAvailable()) { downloadAndViewAttachment((LocalPart) attachment.part); } else { viewLocalAttachment(); @@ -100,6 +100,7 @@ public class AttachmentController { controller.loadAttachment(account, message, attachment.part, new SimpleMessagingListener() { @Override public void loadAttachmentFinished(Account account, Message message, Part part) { + attachment.setContentAvailable(); messageViewFragment.hideAttachmentLoadingDialogOnMainThread(); messageViewFragment.runOnMainThread(attachmentDownloadedCallback); } @@ -129,7 +130,7 @@ public class AttachmentController { return; } - if (!attachment.isContentAvailable) { + if (!attachment.isContentAvailable()) { downloadAndSaveAttachmentTo((LocalPart) attachment.part, directory); } else { saveLocalAttachmentTo(directory); diff --git a/k9mail/src/test/java/com/fsck/k9/message/extractors/AttachmentInfoExtractorTest.java b/k9mail/src/test/java/com/fsck/k9/message/extractors/AttachmentInfoExtractorTest.java index aa5b07b16..c5921c7ed 100644 --- a/k9mail/src/test/java/com/fsck/k9/message/extractors/AttachmentInfoExtractorTest.java +++ b/k9mail/src/test/java/com/fsck/k9/message/extractors/AttachmentInfoExtractorTest.java @@ -168,7 +168,7 @@ public class AttachmentInfoExtractorTest { AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); - assertFalse(attachmentViewInfo.isContentAvailable); + assertFalse(attachmentViewInfo.isContentAvailable()); } @Test @@ -178,7 +178,7 @@ public class AttachmentInfoExtractorTest { AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); - assertTrue(attachmentViewInfo.isContentAvailable); + assertTrue(attachmentViewInfo.isContentAvailable()); } @Test @@ -206,6 +206,6 @@ public class AttachmentInfoExtractorTest { assertEquals(TEST_SIZE, attachmentViewInfo.size); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); assertFalse(attachmentViewInfo.inlineAttachment); - assertTrue(attachmentViewInfo.isContentAvailable); + assertTrue(attachmentViewInfo.isContentAvailable()); } } From a50045844a2e2ce880e868ba6785095858813377 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 14 Apr 2017 04:43:26 +0200 Subject: [PATCH 5/5] Cleanup --- .../AttachmentDownloadDialogFragment.java | 175 +++++++++--------- .../ui/messageview/MessageViewFragment.java | 9 +- 2 files changed, 86 insertions(+), 98 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java index fc0b04c73..7d2abe721 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/AttachmentDownloadDialogFragment.java @@ -1,91 +1,84 @@ -package com.fsck.k9.fragment; - -import android.app.Activity; -import android.app.Dialog; -import android.app.DialogFragment; -import android.app.ProgressDialog; -import android.content.DialogInterface; -import android.os.Bundle; - -import com.fsck.k9.controller.MessagingController; -import com.fsck.k9.controller.MessagingListener; -import com.fsck.k9.controller.SimpleMessagingListener; - -public class AttachmentDownloadDialogFragment extends DialogFragment { - - ProgressDialog dialog; - - int progress = 0; - - MessagingListener messagingListener; - - protected static final String ARG_SIZE = "size"; - protected static final String ARG_MESSAGE = "message"; - private MessagingController messagingController; - - public static AttachmentDownloadDialogFragment newInstance(int size, String message) { - AttachmentDownloadDialogFragment attachmentDownloadDialogFragment = new AttachmentDownloadDialogFragment(); - - Bundle args = new Bundle(); - args.putInt(ARG_SIZE, size); - args.putString(ARG_MESSAGE, message); - attachmentDownloadDialogFragment.setArguments(args); - - return attachmentDownloadDialogFragment; - } - - public void updateDownloadProgress(int newProgress) { - progress = newProgress; - dialog.setProgress(newProgress); - } - - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - Bundle args = getArguments(); - int size = args.getInt(ARG_SIZE); - String message = args.getString(ARG_MESSAGE); - - progress = 0; - - messagingListener = new SimpleMessagingListener() { - @Override - public void updateProgress(int progress) { - updateDownloadProgress(progress); - } - }; - - messagingController = MessagingController.getInstance(getActivity()); - messagingController.addListener(messagingListener); - - dialog = new ProgressDialog(getActivity()); - dialog.setMessage(message); - dialog.setMax(size); - dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); - dialog.setProgress(0); - dialog.show(); - - return dialog; - } - - @Override - public void onDestroyView() { - messagingController.removeListener(messagingListener); - super.onDestroyView(); - } - - @Override - public void onCancel(DialogInterface dialog) { - Activity activity = getActivity(); - if (activity != null && activity instanceof AttachmentDownloadCancelListener) { - AttachmentDownloadCancelListener listener = (AttachmentDownloadCancelListener) activity; - listener.onProgressCancel(this); - } - - super.onCancel(dialog); - } - - public interface AttachmentDownloadCancelListener { - void onProgressCancel(AttachmentDownloadDialogFragment fragment); - } - -} +package com.fsck.k9.fragment; + + +import android.app.Activity; +import android.app.Dialog; +import android.app.DialogFragment; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.Bundle; + +import com.fsck.k9.controller.MessagingController; +import com.fsck.k9.controller.MessagingListener; +import com.fsck.k9.controller.SimpleMessagingListener; + + +public class AttachmentDownloadDialogFragment extends DialogFragment { + private static final String ARG_SIZE = "size"; + private static final String ARG_MESSAGE = "message"; + + + private ProgressDialog dialog; + private MessagingListener messagingListener; + private MessagingController messagingController; + + + public static AttachmentDownloadDialogFragment newInstance(int size, String message) { + AttachmentDownloadDialogFragment fragment = new AttachmentDownloadDialogFragment(); + + Bundle args = new Bundle(); + args.putInt(ARG_SIZE, size); + args.putString(ARG_MESSAGE, message); + fragment.setArguments(args); + + return fragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Bundle args = getArguments(); + int size = args.getInt(ARG_SIZE); + String message = args.getString(ARG_MESSAGE); + + messagingListener = new SimpleMessagingListener() { + @Override + public void updateProgress(int progress) { + dialog.setProgress(progress); + } + }; + + messagingController = MessagingController.getInstance(getActivity()); + messagingController.addListener(messagingListener); + + dialog = new ProgressDialog(getActivity()); + dialog.setMessage(message); + dialog.setMax(size); + dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + dialog.setProgress(0); + dialog.show(); + + return dialog; + } + + @Override + public void onDestroyView() { + messagingController.removeListener(messagingListener); + super.onDestroyView(); + } + + @Override + public void onCancel(DialogInterface dialog) { + Activity activity = getActivity(); + if (activity != null && activity instanceof AttachmentDownloadCancelListener) { + AttachmentDownloadCancelListener listener = (AttachmentDownloadCancelListener) activity; + listener.onProgressCancel(this); + } + + super.onCancel(dialog); + } + + + public interface AttachmentDownloadCancelListener { + void onProgressCancel(AttachmentDownloadDialogFragment fragment); + } +} diff --git a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java index 620bc0dd7..fd88083e7 100644 --- a/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java @@ -18,7 +18,6 @@ import android.os.Bundle; import android.os.Handler; import android.os.Parcelable; import android.text.TextUtils; -import timber.log.Timber; import android.view.ContextThemeWrapper; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -36,22 +35,22 @@ import com.fsck.k9.activity.ChooseFolder; import com.fsck.k9.activity.MessageLoaderHelper; import com.fsck.k9.activity.MessageLoaderHelper.MessageLoaderCallbacks; import com.fsck.k9.activity.MessageReference; +import com.fsck.k9.activity.setup.OpenPgpAppSelectDialog; import com.fsck.k9.controller.MessagingController; import com.fsck.k9.fragment.AttachmentDownloadDialogFragment; import com.fsck.k9.fragment.ConfirmationDialogFragment; import com.fsck.k9.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener; -import com.fsck.k9.fragment.ProgressDialogFragment; import com.fsck.k9.helper.FileBrowserHelper; import com.fsck.k9.helper.FileBrowserHelper.FileBrowserFailOverCallback; import com.fsck.k9.mail.Flag; import com.fsck.k9.mailstore.AttachmentViewInfo; import com.fsck.k9.mailstore.LocalMessage; import com.fsck.k9.mailstore.MessageViewInfo; -import com.fsck.k9.activity.setup.OpenPgpAppSelectDialog; import com.fsck.k9.ui.messageview.CryptoInfoDialog.OnClickShowCryptoKeyListener; import com.fsck.k9.ui.messageview.MessageCryptoPresenter.MessageCryptoMvpView; import com.fsck.k9.view.MessageCryptoDisplayStatus; import com.fsck.k9.view.MessageHeader; +import timber.log.Timber; public class MessageViewFragment extends Fragment implements ConfirmationDialogFragmentListener, @@ -797,22 +796,18 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF @Override public void onViewAttachment(AttachmentViewInfo attachment) { - //TODO: check if we have to download the attachment first currentAttachmentViewInfo = attachment; getAttachmentController(attachment).viewAttachment(); } @Override public void onSaveAttachment(AttachmentViewInfo attachment) { - //TODO: check if we have to download the attachment first currentAttachmentViewInfo = attachment; getAttachmentController(attachment).saveAttachment(); } @Override public void onSaveAttachmentToUserProvidedDirectory(final AttachmentViewInfo attachment) { - //TODO: check if we have to download the attachment first - currentAttachmentViewInfo = attachment; FileBrowserHelper.getInstance().showFileBrowserActivity(MessageViewFragment.this, null, ACTIVITY_CHOOSE_DIRECTORY, new FileBrowserFailOverCallback() {