Create Attachment interface for MessageBuilder

This commit is contained in:
cketti 2018-07-01 02:44:58 +02:00
parent 4f4918c39b
commit f16b65edd6
9 changed files with 101 additions and 25 deletions

View file

@ -677,7 +677,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
.setIdentity(identity)
.setMessageFormat(currentMessageFormat)
.setText(messageContentView.getCharacters())
.setAttachments(attachmentPresenter.createAttachmentList())
.setAttachments(attachmentPresenter.getAttachments())
.setSignature(signatureView.getCharacters())
.setSignatureBeforeQuotedText(account.isSignatureBeforeQuotedText())
.setIdentityChanged(identityChanged)

View file

@ -3,6 +3,7 @@ package com.fsck.k9.activity.compose;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
@ -21,11 +22,11 @@ import com.fsck.k9.activity.compose.ComposeCryptoStatus.AttachErrorState;
import com.fsck.k9.activity.loader.AttachmentContentLoader;
import com.fsck.k9.activity.loader.AttachmentInfoLoader;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.activity.misc.Attachment.LoadingState;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mailstore.AttachmentViewInfo;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mailstore.MessageViewInfo;
import com.fsck.k9.message.Attachment.LoadingState;
import com.fsck.k9.provider.RawMessageProvider;
@ -119,6 +120,10 @@ public class AttachmentPresenter {
return result;
}
public List<com.fsck.k9.message.Attachment> getAttachments() {
return new ArrayList<com.fsck.k9.message.Attachment>(attachments.values());
}
public void onClickAddAttachment(RecipientPresenter recipientPresenter) {
ComposeCryptoStatus currentCachedCryptoStatus = recipientPresenter.getCurrentCachedCryptoStatus();
if (currentCachedCryptoStatus == null) {

View file

@ -10,7 +10,7 @@ import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.activity.misc.Attachment.LoadingState;
import com.fsck.k9.message.Attachment.LoadingState;
import de.cketti.safecontentresolver.SafeContentResolver;
import de.cketti.safecontentresolver.SafeContentResolverCompat;
import org.apache.commons.io.IOUtils;

View file

@ -9,10 +9,10 @@ import android.net.Uri;
import android.provider.OpenableColumns;
import android.support.v4.content.AsyncTaskLoader;
import com.fsck.k9.message.Attachment.LoadingState;
import timber.log.Timber;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.activity.misc.Attachment.LoadingState;
import com.fsck.k9.mail.internet.MimeUtility;
/**

View file

@ -4,12 +4,16 @@ import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Container class for information about an attachment.
*
* This is used by {@link com.fsck.k9.activity.MessageCompose} to fetch and manage attachments.
*/
public class Attachment implements Parcelable {
public class Attachment implements Parcelable, com.fsck.k9.message.Attachment {
/**
* The URI pointing to the source of the attachment.
*
@ -60,11 +64,34 @@ public class Attachment implements Parcelable {
*/
public final String filename;
public enum LoadingState {
URI_ONLY,
METADATA,
COMPLETE,
CANCELLED
@NotNull
@Override
public LoadingState getState() {
return state;
}
@Nullable
@Override
public String getFileName() {
return filename;
}
@Nullable
@Override
public String getContentType() {
return contentType;
}
@Nullable
@Override
public String getName() {
return name;
}
@Nullable
@Override
public Long getSize() {
return size;
}
private Attachment(Uri uri, LoadingState state, int loaderId, String contentType, boolean allowMessageType,

View file

@ -0,0 +1,17 @@
package com.fsck.k9.message
interface Attachment {
val state: LoadingState
val fileName: String?
val contentType: String?
val name: String?
val size: Long?
enum class LoadingState {
URI_ONLY,
METADATA,
COMPLETE,
CANCELLED
}
}

View file

@ -18,7 +18,6 @@ import com.fsck.k9.Identity;
import com.fsck.k9.K9;
import com.fsck.k9.core.R;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BoundaryGenerator;
@ -219,15 +218,15 @@ public abstract class MessageBuilder {
*/
private void addAttachmentsToMessage(final MimeMultipart mp) throws MessagingException {
for (Attachment attachment : attachments) {
if (attachment.state != Attachment.LoadingState.COMPLETE) {
if (attachment.getState() != Attachment.LoadingState.COMPLETE) {
continue;
}
Body body = new TempFileBody(attachment.filename);
Body body = new TempFileBody(attachment.getFileName());
MimeBodyPart bp = new MimeBodyPart(body);
addContentType(bp, attachment.contentType, attachment.name);
addContentDisposition(bp, attachment.name, attachment.size);
addContentType(bp, attachment.getContentType(), attachment.getName());
addContentDisposition(bp, attachment.getName(), attachment.getSize());
mp.addBodyPart(bp);
}

View file

@ -16,7 +16,6 @@ import android.app.Application;
import com.fsck.k9.Account.QuoteStyle;
import com.fsck.k9.Identity;
import com.fsck.k9.RobolectricTest;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.BoundaryGenerator;
@ -352,17 +351,40 @@ public class MessageBuilderTest extends RobolectricTest {
return outputStream.toString();
}
private Attachment createAttachmentWithContent(String mimeType, String filename, String content) throws Exception {
byte[] bytes = content.getBytes();
File tempFile = File.createTempFile("pre", ".tmp");
private Attachment createAttachmentWithContent(final String mimeType, final String filename, String content) throws Exception {
final byte[] bytes = content.getBytes();
final File tempFile = File.createTempFile("pre", ".tmp");
tempFile.deleteOnExit();
FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
fileOutputStream.write(bytes);
fileOutputStream.close();
return Attachment.createAttachment(null, 0, mimeType, true)
.deriveWithMetadataLoaded(mimeType, filename, bytes.length)
.deriveWithLoadComplete(tempFile.getAbsolutePath());
return new Attachment() {
@Override
public Long getSize() {
return (long) bytes.length;
}
@Override
public String getName() {
return filename;
}
@Override
public String getContentType() {
return mimeType;
}
@Override
public String getFileName() {
return tempFile.getAbsolutePath();
}
@Override
public LoadingState getState() {
return LoadingState.COMPLETE;
}
};
}
private MessageBuilder createSimpleMessageBuilder() {

View file

@ -9,6 +9,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.app.PendingIntent;
@ -18,7 +19,6 @@ import android.os.Bundle;
import com.fsck.k9.Account.QuoteStyle;
import com.fsck.k9.Identity;
import com.fsck.k9.K9RobolectricTest;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.autocrypt.AutocryptOpenPgpApiInteractor;
import com.fsck.k9.autocrypt.AutocryptOperations;
import com.fsck.k9.mail.Address;
@ -441,7 +441,7 @@ public class PgpMessageBuilderTest extends K9RobolectricTest {
public void buildSignWithAttach__withInlineEnabled__shouldThrow() {
configureSignOnly();
configurePgpInline();
pgpMessageBuilder.setAttachments(Collections.singletonList(Attachment.createAttachment(null, 0, null, true)));
pgpMessageBuilder.setAttachments(createAttachmentList());
Callback mockCallback = mock(Callback.class);
pgpMessageBuilder.buildAsync(mockCallback);
@ -455,7 +455,7 @@ public class PgpMessageBuilderTest extends K9RobolectricTest {
public void buildEncryptWithAttach__withInlineEnabled__shouldThrow() {
configureEncryptAndSign();
configurePgpInline();
pgpMessageBuilder.setAttachments(Collections.singletonList(Attachment.createAttachment(null, 0, null, true)));
pgpMessageBuilder.setAttachments(createAttachmentList());
Callback mockCallback = mock(Callback.class);
pgpMessageBuilder.buildAsync(mockCallback);
@ -563,6 +563,12 @@ public class PgpMessageBuilderTest extends K9RobolectricTest {
return builder;
}
private static List<Attachment> createAttachmentList() {
Attachment attachment = mock(Attachment.class);
when(attachment.getState()).thenReturn(Attachment.LoadingState.URI_ONLY);
return Collections.singletonList(attachment);
}
private static void assertContentOfBodyPartEquals(String reason, BodyPart signatureBodyPart, byte[] expected) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();