Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d096261c authored by cketti's avatar cketti Committed by Vincent Breitmoser
Browse files

Make use of Globals to simplify code

parent f6fe28d3
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -6,7 +6,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
@@ -42,14 +41,14 @@ public class AttachmentResolver {
    }

    @WorkerThread
    public static AttachmentResolver createFromPart(Context context, Part part) {
    public static AttachmentResolver createFromPart(Part part) {
        AttachmentInfoExtractor attachmentInfoExtractor = AttachmentInfoExtractor.getInstance();
        Map<String, Uri> contentIdToAttachmentUriMap = buildCidToAttachmentUriMap(context, attachmentInfoExtractor, part);
        Map<String, Uri> contentIdToAttachmentUriMap = buildCidToAttachmentUriMap(attachmentInfoExtractor, part);
        return new AttachmentResolver(contentIdToAttachmentUriMap);
    }

    @VisibleForTesting
    static Map<String,Uri> buildCidToAttachmentUriMap(Context context, AttachmentInfoExtractor attachmentInfoExtractor,
    static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor,
            Part rootPart) {
        HashMap<String,Uri> result = new HashMap<>();

@@ -69,7 +68,7 @@ public class AttachmentResolver {
                try {
                    String contentId = part.getContentId();
                    if (contentId != null) {
                        AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(context, part);
                        AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part);
                        result.put(contentId, attachmentInfo.uri);
                    }
                } catch (MessagingException e) {
+2 −2
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ public class MessageViewInfoExtractor {
            extraViewableText = extraViewable.text;
        }

        AttachmentResolver attachmentResolver = AttachmentResolver.createFromPart(context, rootPart);
        AttachmentResolver attachmentResolver = AttachmentResolver.createFromPart(rootPart);

        return MessageViewInfo.createWithExtractedContent(message, rootPart, viewable.html,
                attachmentInfos, cryptoResultAnnotation, extraViewableText, extraAttachmentInfos, attachmentResolver);
@@ -89,7 +89,7 @@ public class MessageViewInfoExtractor {
            MessageExtractor.findViewablesAndAttachments(part, viewableParts, attachments);
        }

        attachmentInfos.addAll(attachmentInfoExtractor.extractAttachmentInfos(context, attachments));
        attachmentInfos.addAll(attachmentInfoExtractor.extractAttachmentInfos(attachments));
        return MessageViewInfoExtractor.extractTextFromViewables(context, viewableParts);
    }

+15 −9
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.util.Log;

import com.fsck.k9.Globals;
import com.fsck.k9.K9;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.MessagingException;
@@ -26,25 +27,30 @@ import com.fsck.k9.provider.DecryptedFileProvider;


public class AttachmentInfoExtractor {
    @VisibleForTesting
    AttachmentInfoExtractor() { }
    private final Context context;


    public static AttachmentInfoExtractor getInstance() {
        return new AttachmentInfoExtractor();
        Context context = Globals.getContext();
        return new AttachmentInfoExtractor(context);
    }

    @VisibleForTesting
    AttachmentInfoExtractor(Context context) {
        this.context = context;
    }

    public List<AttachmentViewInfo> extractAttachmentInfos(Context context, List<Part> attachmentParts)
            throws MessagingException {
    public List<AttachmentViewInfo> extractAttachmentInfos(List<Part> attachmentParts) throws MessagingException {

        List<AttachmentViewInfo> attachments = new ArrayList<>();
        for (Part part : attachmentParts) {
            attachments.add(extractAttachmentInfo(context, part));
            attachments.add(extractAttachmentInfo(part));
        }

        return attachments;
    }

    public AttachmentViewInfo extractAttachmentInfo(Context context, Part part) throws MessagingException {
    public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException {
        Uri uri;
        long size;
        if (part instanceof LocalPart) {
@@ -58,7 +64,7 @@ public class AttachmentInfoExtractor {
            if (body instanceof DeferredFileBody) {
                DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body;
                size = decryptedTempFileBody.getSize();
                uri = getDecryptedFileProviderUri(context, decryptedTempFileBody, part.getMimeType());
                uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType());
                return extractAttachmentInfo(part, uri, size);
            } else {
                throw new IllegalArgumentException("Unsupported part type provided");
@@ -70,7 +76,7 @@ public class AttachmentInfoExtractor {

    @Nullable
    @VisibleForTesting
    protected Uri getDecryptedFileProviderUri(Context context, DeferredFileBody decryptedTempFileBody, String mimeType) {
    protected Uri getDecryptedFileProviderUri(DeferredFileBody decryptedTempFileBody, String mimeType) {
        Uri uri;
        try {
            File file = decryptedTempFileBody.getFile();
+3 −3
Original line number Diff line number Diff line
@@ -133,7 +133,7 @@ public class QuotedMessagePresenter {

            // Load the message with the reply header. TODO replace with MessageViewInfo data
            view.setQuotedHtml(quotedHtmlContent.getQuotedContent(),
                    AttachmentResolver.createFromPart(messageCompose, sourceMessage));
                    AttachmentResolver.createFromPart(sourceMessage));

            // TODO: Also strip the signature from the text/plain part
            view.setQuotedText(QuotedMessageHelper.quoteOriginalTextMessage(resources, sourceMessage,
@@ -298,7 +298,7 @@ public class QuotedMessagePresenter {
                    }
                    // TODO replace with MessageViewInfo data
                    view.setQuotedHtml(quotedHtmlContent.getQuotedContent(),
                            AttachmentResolver.createFromPart(messageCompose, message));
                            AttachmentResolver.createFromPart(message));
                }
            }
            if (bodyPlainOffset != null && bodyPlainLength != null) {
+7 −18
Original line number Diff line number Diff line
package com.fsck.k9.mailstore;


import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import android.net.Uri;
@@ -17,7 +15,6 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;

import static org.junit.Assert.*;
@@ -46,8 +43,7 @@ public class AttachmentResolverTest {
    public void buildCidMap__onPartWithNoBody__shouldReturnEmptyMap() throws Exception {
        Part part = new MimeBodyPart();

        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(
                RuntimeEnvironment.application, attachmentInfoExtractor, part);
        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, part);

        assertTrue(result.isEmpty());
    }
@@ -57,8 +53,7 @@ public class AttachmentResolverTest {
        Multipart multipartBody = new MimeMultipart();
        Part multipartPart = new MimeBodyPart(multipartBody);

        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(
                RuntimeEnvironment.application, attachmentInfoExtractor, multipartPart);
        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart);

        assertTrue(result.isEmpty());
    }
@@ -70,10 +65,7 @@ public class AttachmentResolverTest {
        Part multipartPart = new MimeBodyPart(multipartBody);
        multipartBody.addBodyPart(bodyPart);


        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(
                RuntimeEnvironment.application, attachmentInfoExtractor, multipartPart);

        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart);

        verify(bodyPart).getContentId();
        assertTrue(result.isEmpty());
@@ -92,19 +84,16 @@ public class AttachmentResolverTest {
        when(subPart1.getContentId()).thenReturn("cid-1");
        when(subPart2.getContentId()).thenReturn("cid-2");

        when(attachmentInfoExtractor.extractAttachmentInfo(RuntimeEnvironment.application, subPart1)).thenReturn(
        when(attachmentInfoExtractor.extractAttachmentInfo(subPart1)).thenReturn(
                new AttachmentViewInfo(null, null, AttachmentViewInfo.UNKNOWN_SIZE, ATTACHMENT_TEST_URI_1, true, subPart1));
        when(attachmentInfoExtractor.extractAttachmentInfo(RuntimeEnvironment.application, subPart2)).thenReturn(
        when(attachmentInfoExtractor.extractAttachmentInfo(subPart2)).thenReturn(
                new AttachmentViewInfo(null, null, AttachmentViewInfo.UNKNOWN_SIZE, ATTACHMENT_TEST_URI_2, true, subPart2));


        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(RuntimeEnvironment.application,
                attachmentInfoExtractor, multipartPart);

        Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart);

        assertEquals(2, result.size());
        assertEquals(ATTACHMENT_TEST_URI_1, result.get("cid-1"));
        assertEquals(ATTACHMENT_TEST_URI_2, result.get("cid-2"));
    }

}
Loading