Search in sources :

Example 26 with AttachmentViewInfo

use of com.fsck.k9.mailstore.AttachmentViewInfo in project k-9 by k9mail.

the class AttachmentInfoExtractorTest method extractInfoForDb__withDispositionInlineAndContentIdAndImageMimeType__shouldReturnInlineAttachment.

@Test
public void extractInfoForDb__withDispositionInlineAndContentIdAndImageMimeType__shouldReturnInlineAttachment() throws Exception {
    Part part = new MimeBodyPart();
    part.addRawHeader(MimeHeader.HEADER_CONTENT_TYPE, MimeHeader.HEADER_CONTENT_TYPE + ": image/png");
    part.addRawHeader(MimeHeader.HEADER_CONTENT_ID, MimeHeader.HEADER_CONTENT_ID + ": " + TEST_CONTENT_ID);
    part.addRawHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, MimeHeader.HEADER_CONTENT_DISPOSITION + ": " + "inline" + ";\n  filename=\"filename.ext\";\n  meaningless=\"dummy\"");
    AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part);
    assertTrue(attachmentViewInfo.inlineAttachment);
}
Also used : Part(com.fsck.k9.mail.Part) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) LocalBodyPart(com.fsck.k9.mailstore.LocalBodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) AttachmentViewInfo(com.fsck.k9.mailstore.AttachmentViewInfo) RobolectricTest(com.fsck.k9.RobolectricTest) Test(org.junit.Test)

Example 27 with AttachmentViewInfo

use of com.fsck.k9.mailstore.AttachmentViewInfo in project k-9 by k9mail.

the class MessageContainerView method onCreateContextMenu.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu);
    WebView webview = (WebView) v;
    WebView.HitTestResult result = webview.getHitTestResult();
    if (result == null) {
        return;
    }
    int type = result.getType();
    Context context = getContext();
    switch(type) {
        case HitTestResult.SRC_ANCHOR_TYPE:
            {
                final String url = result.getExtra();
                OnMenuItemClickListener listener = new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch(item.getItemId()) {
                            case MENU_ITEM_LINK_VIEW:
                                {
                                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                    startActivityIfAvailable(getContext(), intent);
                                    break;
                                }
                            case MENU_ITEM_LINK_SHARE:
                                {
                                    new ShareCompat.IntentBuilder(getContext()).setType("text/plain").setText(url).startChooser();
                                    break;
                                }
                            case MENU_ITEM_LINK_COPY:
                                {
                                    String label = getContext().getString(R.string.webview_contextmenu_link_clipboard_label);
                                    clipboardManager.setText(label, url);
                                    break;
                                }
                            case MENU_ITEM_LINK_TEXT_COPY:
                                {
                                    LinkTextHandler linkTextHandler = DI.get(LinkTextHandler.class);
                                    Message message = linkTextHandler.obtainMessage();
                                    webview.requestFocusNodeHref(message);
                                    break;
                                }
                        }
                        return true;
                    }
                };
                menu.setHeaderTitle(url);
                menu.add(Menu.NONE, MENU_ITEM_LINK_VIEW, 0, context.getString(R.string.webview_contextmenu_link_view_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_LINK_SHARE, 1, context.getString(R.string.webview_contextmenu_link_share_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_LINK_COPY, 2, context.getString(R.string.webview_contextmenu_link_copy_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_LINK_TEXT_COPY, 3, context.getString(R.string.webview_contextmenu_link_text_copy_action)).setOnMenuItemClickListener(listener);
                break;
            }
        case HitTestResult.IMAGE_TYPE:
        case HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
            {
                final Uri uri = Uri.parse(result.getExtra());
                if (uri == null) {
                    return;
                }
                final AttachmentViewInfo attachmentViewInfo = getAttachmentViewInfoIfCidUri(uri);
                final boolean inlineImage = attachmentViewInfo != null;
                OnMenuItemClickListener listener = new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch(item.getItemId()) {
                            case MENU_ITEM_IMAGE_VIEW:
                                {
                                    if (inlineImage) {
                                        attachmentCallback.onViewAttachment(attachmentViewInfo);
                                    } else {
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        startActivityIfAvailable(getContext(), intent);
                                    }
                                    break;
                                }
                            case MENU_ITEM_IMAGE_SAVE:
                                {
                                    if (inlineImage) {
                                        attachmentCallback.onSaveAttachment(attachmentViewInfo);
                                    } else {
                                        downloadImage(uri);
                                    }
                                    break;
                                }
                            case MENU_ITEM_IMAGE_COPY:
                                {
                                    String label = getContext().getString(R.string.webview_contextmenu_image_clipboard_label);
                                    clipboardManager.setText(label, uri.toString());
                                    break;
                                }
                        }
                        return true;
                    }
                };
                menu.setHeaderTitle(inlineImage ? context.getString(R.string.webview_contextmenu_image_title) : uri.toString());
                menu.add(Menu.NONE, MENU_ITEM_IMAGE_VIEW, 0, context.getString(R.string.webview_contextmenu_image_view_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_IMAGE_SAVE, 1, inlineImage ? context.getString(R.string.webview_contextmenu_image_save_action) : context.getString(R.string.webview_contextmenu_image_download_action)).setOnMenuItemClickListener(listener);
                if (!inlineImage) {
                    menu.add(Menu.NONE, MENU_ITEM_IMAGE_COPY, 2, context.getString(R.string.webview_contextmenu_image_copy_action)).setOnMenuItemClickListener(listener);
                }
                break;
            }
        case HitTestResult.PHONE_TYPE:
            {
                final String phoneNumber = result.getExtra();
                OnMenuItemClickListener listener = new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch(item.getItemId()) {
                            case MENU_ITEM_PHONE_CALL:
                                {
                                    Uri uri = Uri.parse(WebView.SCHEME_TEL + phoneNumber);
                                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                    startActivityIfAvailable(getContext(), intent);
                                    break;
                                }
                            case MENU_ITEM_PHONE_SAVE:
                                {
                                    Contacts contacts = Contacts.getInstance(getContext());
                                    contacts.addPhoneContact(phoneNumber);
                                    break;
                                }
                            case MENU_ITEM_PHONE_COPY:
                                {
                                    String label = getContext().getString(R.string.webview_contextmenu_phone_clipboard_label);
                                    clipboardManager.setText(label, phoneNumber);
                                    break;
                                }
                        }
                        return true;
                    }
                };
                menu.setHeaderTitle(phoneNumber);
                menu.add(Menu.NONE, MENU_ITEM_PHONE_CALL, 0, context.getString(R.string.webview_contextmenu_phone_call_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_PHONE_SAVE, 1, context.getString(R.string.webview_contextmenu_phone_save_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_PHONE_COPY, 2, context.getString(R.string.webview_contextmenu_phone_copy_action)).setOnMenuItemClickListener(listener);
                break;
            }
        case WebView.HitTestResult.EMAIL_TYPE:
            {
                final String email = result.getExtra();
                OnMenuItemClickListener listener = new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        switch(item.getItemId()) {
                            case MENU_ITEM_EMAIL_SEND:
                                {
                                    Uri uri = Uri.parse(WebView.SCHEME_MAILTO + email);
                                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                    startActivityIfAvailable(getContext(), intent);
                                    break;
                                }
                            case MENU_ITEM_EMAIL_SAVE:
                                {
                                    Contacts contacts = Contacts.getInstance(getContext());
                                    contacts.createContact(new Address(email));
                                    break;
                                }
                            case MENU_ITEM_EMAIL_COPY:
                                {
                                    String label = getContext().getString(R.string.webview_contextmenu_email_clipboard_label);
                                    clipboardManager.setText(label, email);
                                    break;
                                }
                        }
                        return true;
                    }
                };
                menu.setHeaderTitle(email);
                menu.add(Menu.NONE, MENU_ITEM_EMAIL_SEND, 0, context.getString(R.string.webview_contextmenu_email_send_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_EMAIL_SAVE, 1, context.getString(R.string.webview_contextmenu_email_save_action)).setOnMenuItemClickListener(listener);
                menu.add(Menu.NONE, MENU_ITEM_EMAIL_COPY, 2, context.getString(R.string.webview_contextmenu_email_copy_action)).setOnMenuItemClickListener(listener);
                break;
            }
    }
}
Also used : Context(android.content.Context) Message(android.os.Message) Address(com.fsck.k9.mail.Address) OnMenuItemClickListener(android.view.MenuItem.OnMenuItemClickListener) ShareCompat(androidx.core.app.ShareCompat) MenuItem(android.view.MenuItem) Intent(android.content.Intent) Uri(android.net.Uri) HitTestResult(android.webkit.WebView.HitTestResult) Contacts(com.fsck.k9.helper.Contacts) MessageWebView(com.fsck.k9.view.MessageWebView) WebView(android.webkit.WebView) AttachmentViewInfo(com.fsck.k9.mailstore.AttachmentViewInfo)

Example 28 with AttachmentViewInfo

use of com.fsck.k9.mailstore.AttachmentViewInfo in project k-9 by k9mail.

the class MessageContainerView method renderAttachments.

public void renderAttachments(MessageViewInfo messageViewInfo) {
    if (messageViewInfo.attachments != null) {
        for (AttachmentViewInfo attachment : messageViewInfo.attachments) {
            attachments.put(attachment.internalUri, attachment);
            if (attachment.inlineAttachment) {
                continue;
            }
            AttachmentView view = (AttachmentView) mInflater.inflate(R.layout.message_view_attachment, mAttachments, false);
            view.setCallback(attachmentCallback);
            view.setAttachment(attachment);
            attachmentViewMap.put(attachment, view);
            mAttachments.addView(view);
        }
    }
    if (messageViewInfo.extraAttachments != null) {
        for (AttachmentViewInfo attachment : messageViewInfo.extraAttachments) {
            attachments.put(attachment.internalUri, attachment);
            if (attachment.inlineAttachment) {
                continue;
            }
            LockedAttachmentView view = (LockedAttachmentView) mInflater.inflate(R.layout.message_view_attachment_locked, mAttachments, false);
            view.setCallback(attachmentCallback);
            view.setAttachment(attachment);
            // attachments.put(attachment, view);
            mAttachments.addView(view);
        }
    }
}
Also used : AttachmentViewInfo(com.fsck.k9.mailstore.AttachmentViewInfo)

Example 29 with AttachmentViewInfo

use of com.fsck.k9.mailstore.AttachmentViewInfo in project k-9 by k9mail.

the class MessageViewInfoExtractor method extractViewableAndAttachments.

private ViewableExtractedText extractViewableAndAttachments(List<Part> parts, List<AttachmentViewInfo> attachmentInfos) throws MessagingException {
    ArrayList<Viewable> viewableParts = new ArrayList<>();
    ArrayList<Part> attachments = new ArrayList<>();
    for (Part part : parts) {
        MessageExtractor.findViewablesAndAttachments(part, viewableParts, attachments);
    }
    attachmentInfos.addAll(attachmentInfoExtractor.extractAttachmentInfoForView(attachments));
    return extractTextFromViewables(viewableParts);
}
Also used : Part(com.fsck.k9.mail.Part) Viewable(com.fsck.k9.mail.internet.Viewable) ArrayList(java.util.ArrayList)

Example 30 with AttachmentViewInfo

use of com.fsck.k9.mailstore.AttachmentViewInfo in project k-9 by k9mail.

the class AttachmentInfoExtractor method extractAttachmentInfo.

@WorkerThread
public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException {
    Uri uri;
    long size;
    boolean isContentAvailable;
    if (part instanceof LocalPart) {
        LocalPart localPart = (LocalPart) part;
        String accountUuid = localPart.getAccountUuid();
        long messagePartId = localPart.getPartId();
        size = localPart.getSize();
        isContentAvailable = part.getBody() != null;
        uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId);
    } else if (part instanceof LocalMessage) {
        LocalMessage localMessage = (LocalMessage) part;
        String accountUuid = localMessage.getAccount().getUuid();
        long messagePartId = localMessage.getMessagePartId();
        size = localMessage.getSize();
        isContentAvailable = part.getBody() != null;
        uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId);
    } else {
        Body body = part.getBody();
        if (body instanceof DeferredFileBody) {
            DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body;
            size = decryptedTempFileBody.getSize();
            uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType());
            isContentAvailable = true;
        } else {
            throw new IllegalArgumentException("Unsupported part type provided");
        }
    }
    return extractAttachmentInfo(part, uri, size, isContentAvailable);
}
Also used : LocalMessage(com.fsck.k9.mailstore.LocalMessage) LocalPart(com.fsck.k9.mailstore.LocalPart) Uri(android.net.Uri) Body(com.fsck.k9.mail.Body) DeferredFileBody(com.fsck.k9.mailstore.DeferredFileBody) DeferredFileBody(com.fsck.k9.mailstore.DeferredFileBody) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

Test (org.junit.Test)18 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)17 AttachmentViewInfo (com.fsck.k9.mailstore.AttachmentViewInfo)17 RobolectricTest (com.fsck.k9.RobolectricTest)14 Part (com.fsck.k9.mail.Part)9 Uri (android.net.Uri)5 LocalBodyPart (com.fsck.k9.mailstore.LocalBodyPart)5 BodyPart (com.fsck.k9.mail.BodyPart)4 K9RobolectricTest (com.fsck.k9.K9RobolectricTest)3 Attachment (com.fsck.k9.activity.misc.Attachment)3 Message (com.fsck.k9.mail.Message)3 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)3 ArrayList (java.util.ArrayList)3 WorkerThread (androidx.annotation.WorkerThread)2 InlineAttachment (com.fsck.k9.activity.misc.InlineAttachment)2 Body (com.fsck.k9.mail.Body)2 Multipart (com.fsck.k9.mail.Multipart)2 DeferredFileBody (com.fsck.k9.mailstore.DeferredFileBody)2 LocalPart (com.fsck.k9.mailstore.LocalPart)2 Context (android.content.Context)1