From adcb29616a18a027badca8ce3d1cb6fc44ca87e6 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Mon, 30 Nov 2015 11:16:30 -0800 Subject: [PATCH] Keep track of attachmentmanager garbage Fixes #4764 // FREEBIE --- .../securesms/mms/AttachmentManager.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/mms/AttachmentManager.java b/src/org/thoughtcrime/securesms/mms/AttachmentManager.java index 42d4cbb313..d451f83d31 100644 --- a/src/org/thoughtcrime/securesms/mms/AttachmentManager.java +++ b/src/org/thoughtcrime/securesms/mms/AttachmentManager.java @@ -45,6 +45,9 @@ import org.thoughtcrime.securesms.util.concurrent.ListenableFuture.Listener; import org.whispersystems.libaxolotl.util.guava.Optional; import java.io.IOException; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import java.util.concurrent.ExecutionException; import ws.com.google.android.mms.ContentType; @@ -60,7 +63,8 @@ public class AttachmentManager { private final @NonNull AudioView audioView; private final @NonNull AttachmentListener attachmentListener; - private @NonNull Optional slide = Optional.absent(); + private @NonNull List garbage = new LinkedList<>(); + private @NonNull Optional slide = Optional.absent(); private @Nullable Uri captureUri; public AttachmentManager(@NonNull Activity activity, @NonNull AttachmentListener listener) { @@ -86,6 +90,8 @@ public class AttachmentManager { @Override public void onFailure(ExecutionException e) {} }); + + markGarbage(getSlideUri()); slide = Optional.absent(); audioView.cleanup(); } @@ -96,6 +102,13 @@ public class AttachmentManager { captureUri = null; slide = Optional.absent(); + + Iterator iterator = garbage.listIterator(); + + while (iterator.hasNext()) { + cleanup(iterator.next()); + iterator.remove(); + } } private void cleanup(final @Nullable Uri uri) { @@ -105,6 +118,13 @@ public class AttachmentManager { } } + private void markGarbage(@Nullable Uri uri) { + if (uri != null && PersistentBlobProvider.isAuthority(context, uri)) { + Log.w(TAG, "Marking garbage that needs cleaning: " + uri); + garbage.add(uri); + } + } + private void setSlide(@NonNull Slide slide) { if (getSlideUri() != null) cleanup(getSlideUri()); if (captureUri != null && slide.getUri() != captureUri) cleanup(captureUri);