From a1cf485ecc71d41e507a6b61d201bbd0feb532bb Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 7 Nov 2019 16:53:42 -0300 Subject: [PATCH] [android] address first round of code review for attachment placeholders --- .../briar/android/attachment/AttachmentItem.java | 2 +- .../briar/android/attachment/AttachmentRetriever.java | 4 ++-- .../briar/android/attachment/AttachmentRetrieverImpl.java | 6 ++++-- .../briar/android/attachment/UnavailableItem.java | 2 ++ .../briar/android/conversation/ConversationActivity.java | 2 +- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java index a19827eab..e0c4b8694 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java @@ -55,7 +55,7 @@ public class AttachmentItem implements Parcelable { } /** - * Use only for {@link MISSING} or {@link LOADING} items. + * Use only for {@link State MISSING} or {@link State LOADING} items. */ AttachmentItem(AttachmentHeader header, int width, int height, State state) { diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java index 9b5d14812..298c8cbbb 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java @@ -27,7 +27,7 @@ public interface AttachmentRetriever { * Retrieves item size and adds the item to the cache, if available. */ @DatabaseExecutor - void cacheAttachmentItem(MessageId conversationMessageId, + void cacheAttachmentItemWithSize(MessageId conversationMessageId, AttachmentHeader h) throws DbException; /** @@ -41,7 +41,7 @@ public interface AttachmentRetriever { * * @return a pair of the {@link MessageId} of the conversation message * and the {@link AttachmentItem} - * or {@code null} when the conversation message did not yet arrive. + * or {@code null} when the private message did not yet arrive. */ @Nullable @DatabaseExecutor diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetrieverImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetrieverImpl.java index f13556b12..8724ff8ee 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetrieverImpl.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetrieverImpl.java @@ -26,6 +26,7 @@ import androidx.annotation.Nullable; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; +import static org.briarproject.bramble.util.IoUtils.tryToClose; import static org.briarproject.briar.android.attachment.AttachmentItem.State.AVAILABLE; import static org.briarproject.briar.android.attachment.AttachmentItem.State.ERROR; import static org.briarproject.briar.android.attachment.AttachmentItem.State.LOADING; @@ -93,7 +94,7 @@ class AttachmentRetrieverImpl implements AttachmentRetriever { @Override @DatabaseExecutor - public void cacheAttachmentItem(MessageId conversationMessageId, + public void cacheAttachmentItemWithSize(MessageId conversationMessageId, AttachmentHeader h) throws DbException { try { Attachment a = messagingManager.getAttachment(h); @@ -135,11 +136,12 @@ class AttachmentRetrieverImpl implements AttachmentRetriever { boolean needsSize) { AttachmentHeader h = a.getHeader(); AttachmentItem item = itemCache.get(h.getMessageId()); - if (item != null && (needsSize && item.hasSize())) return item; + if (item != null && (needsSize == item.hasSize())) return item; if (needsSize) { InputStream is = new BufferedInputStream(a.getStream()); Size size = imageSizeCalculator.getSize(is, h.getContentType()); + tryToClose(is, LOG, WARNING); item = createAttachmentItem(h, size); } else { String extension = diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/UnavailableItem.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/UnavailableItem.java index 9aa648020..72951da3b 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/UnavailableItem.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/UnavailableItem.java @@ -1,11 +1,13 @@ package org.briarproject.briar.android.attachment; +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.briar.api.messaging.AttachmentHeader; import javax.annotation.concurrent.Immutable; @Immutable +@NotNullByDefault class UnavailableItem { private final MessageId conversationMessageId; 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 fccf6ae59..ac03699a6 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 @@ -558,7 +558,7 @@ public class ConversationActivity extends BriarActivity LOG.info("Eagerly loading image size for latest message"); AttachmentHeader header = headers.get(0); // get the item to retrieve its size - attachmentRetriever.cacheAttachmentItem(h.getId(), header); + attachmentRetriever.cacheAttachmentItemWithSize(h.getId(), header); } } catch (DbException e) { logException(LOG, WARNING, e);