From dfb71a03a525a9257cad0b4dd928cb1e834cb26b Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Tue, 4 Dec 2018 19:05:16 -0200 Subject: [PATCH] [android] Only retrieve image sizes for single images in messages We need to do this to know the height of messages when binding the view. The size of single images can be different (e.g. due to orientation). For multiple images, we use a fixed size, so no retrieval is required. --- .../conversation/AttachmentController.java | 28 +++++++++++++++---- .../conversation/ConversationActivity.java | 4 +-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/AttachmentController.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/AttachmentController.java index 2d85e2668..c708176b6 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/AttachmentController.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/AttachmentController.java @@ -92,19 +92,32 @@ class AttachmentController { List getAttachmentItems( List> attachments) { + boolean needsSize = attachments.size() == 1; List items = new ArrayList<>(attachments.size()); for (Pair a : attachments) { AttachmentItem item = - getAttachmentItem(a.getFirst(), a.getSecond()); + getAttachmentItem(a.getFirst(), a.getSecond(), needsSize); items.add(item); } return items; } - private AttachmentItem getAttachmentItem(AttachmentHeader h, Attachment a) { + private AttachmentItem getAttachmentItem(AttachmentHeader h, Attachment a, + boolean needsSize) { MessageId messageId = h.getMessageId(); - Size size = new Size(); + if (!needsSize) { + String mimeType = h.getContentType(); + String extension = getExtensionFromMimeType(mimeType); + boolean hasError = false; + if (extension == null) { + extension = ""; + hasError = true; + } + return new AttachmentItem(messageId, 0, 0, mimeType, extension, 0, + 0, hasError); + } + Size size = new Size(); InputStream is = a.getStream(); is.mark(Integer.MAX_VALUE); try { @@ -134,8 +147,7 @@ class AttachmentController { getThumbnailSize(size.width, size.height, size.mimeType); } // get file extension - MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); - String extension = mimeTypeMap.getExtensionFromMimeType(size.mimeType); + String extension = getExtensionFromMimeType(size.mimeType); if (extension == null) { return new AttachmentItem(messageId, 0, 0, "", "", 0, 0, true); } @@ -144,6 +156,12 @@ class AttachmentController { size.error); } + @Nullable + private String getExtensionFromMimeType(String mimeType) { + MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); + return mimeTypeMap.getExtensionFromMimeType(mimeType); + } + /** * Gets the size of a JPEG {@link InputStream} if EXIF info is available. */ diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationActivity.java index 3d3611f90..ae8b22970 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationActivity.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationActivity.java @@ -397,7 +397,7 @@ public class ConversationActivity extends BriarActivity textCache.put(id, text); } } - if (!h.getAttachmentHeaders().isEmpty()) { + if (h.getAttachmentHeaders().size() == 1) { List items = attachmentController.get(id); if (items == null) { @@ -486,7 +486,7 @@ public class ConversationActivity extends BriarActivity try { List> attachments = attachmentController.getMessageAttachments(headers); - // TODO move getting the items off to the IoExecutor + // TODO move getting the items off to IoExecutor, if size == 1 List items = attachmentController.getAttachmentItems(attachments); displayMessageAttachments(messageId, items);