From b572fce6580f8ef41ab5190ec844831ce79ea6da Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Mon, 25 Feb 2019 13:58:10 -0800 Subject: [PATCH] Remove some unnecessary intermediate blob creation. --- .../conversation/ConversationActivity.java | 17 +++++++++-- .../linkpreview/LinkPreviewViewModel.java | 29 ++----------------- 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index d38497a626..2d9140d9e0 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -70,6 +70,7 @@ import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; +import com.annimon.stream.Stream; import com.google.android.gms.location.places.ui.PlacePicker; import org.greenrobot.eventbus.EventBus; @@ -581,7 +582,19 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity } } - sendMediaMessage(transport.isSms(), message, slideDeck, Collections.emptyList(), Collections.emptyList(), expiresIn, subscriptionId, initiating); + final Context context = ConversationActivity.this.getApplicationContext(); + + sendMediaMessage(transport.isSms(), message, slideDeck, Collections.emptyList(), Collections.emptyList(), expiresIn, subscriptionId, initiating).addListener(new AssertedSuccessListener() { + @Override + public void onSuccess(Void result) { + AsyncTask.THREAD_POOL_EXECUTOR.execute(() -> { + Stream.of(slideDeck.getSlides()) + .map(Slide::getUri) + .withoutNulls() + .forEach(uri -> PersistentBlobProvider.getInstance(context).delete(context, uri)); + }); + } + }); break; } @@ -2054,7 +2067,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity throws InvalidMessageException { Log.i(TAG, "Sending media message..."); - sendMediaMessage(forceSms, getMessage(), attachmentManager.buildSlideDeck(), Collections.emptyList(), linkPreviewViewModel.getPersistedLinkPreviews(this), expiresIn, subscriptionId, initiating); + sendMediaMessage(forceSms, getMessage(), attachmentManager.buildSlideDeck(), Collections.emptyList(), linkPreviewViewModel.getActiveLinkPreviews(), expiresIn, subscriptionId, initiating); } private ListenableFuture sendMediaMessage(final boolean forceSms, diff --git a/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewViewModel.java b/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewViewModel.java index 4380fa53b2..fcdd08c5ba 100644 --- a/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewViewModel.java +++ b/src/org/thoughtcrime/securesms/linkpreview/LinkPreviewViewModel.java @@ -51,37 +51,14 @@ public class LinkPreviewViewModel extends ViewModel { return linkPreviewState.getValue() != null && linkPreviewState.getValue().getLinkPreview().isPresent(); } - public @NonNull List getPersistedLinkPreviews(@NonNull Context context) { + public @NonNull List getActiveLinkPreviews() { final LinkPreviewState state = linkPreviewState.getValue(); + if (state == null || !state.getLinkPreview().isPresent()) { return Collections.emptyList(); - } - - if (!state.getLinkPreview().get().getThumbnail().isPresent() || state.getLinkPreview().get().getThumbnail().get().getDataUri() == null) { + } else { return Collections.singletonList(state.getLinkPreview().get()); } - - LinkPreview originalPreview = state.getLinkPreview().get(); - Attachment originalAttachment = originalPreview.getThumbnail().get(); - Uri memoryUri = originalAttachment.getDataUri(); - byte[] imageBlob = MemoryBlobProvider.getInstance().getBlob(memoryUri); - Uri diskUri = PersistentBlobProvider.getInstance(context).create(context, imageBlob, MediaUtil.IMAGE_JPEG, null); - Attachment newAttachment = new UriAttachment(diskUri, - diskUri, - originalAttachment.getContentType(), - originalAttachment.getTransferState(), - originalAttachment.getSize(), - originalAttachment.getWidth(), - originalAttachment.getHeight(), - originalAttachment.getFileName(), - originalAttachment.getFastPreflightId(), - originalAttachment.isVoiceNote(), - originalAttachment.isQuote(), - originalAttachment.getCaption()); - - MemoryBlobProvider.getInstance().delete(memoryUri); - - return Collections.singletonList(new LinkPreview(originalPreview.getUrl(), originalPreview.getTitle(), Optional.of(newAttachment))); } public void onTextChanged(@NonNull Context context, @NonNull String text, int cursorStart, int cursorEnd) {