From 8332342d7564ecd40d9f26eee751373519637292 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Tue, 5 May 2015 15:14:41 -0700 Subject: [PATCH] verify glide isn't loading into destroyed activity Fixes #3110 Closes #3114 // FREEBIE --- .../securesms/components/ThumbnailView.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/ThumbnailView.java b/src/org/thoughtcrime/securesms/components/ThumbnailView.java index d8b5557533..2b35aadf30 100644 --- a/src/org/thoughtcrime/securesms/components/ThumbnailView.java +++ b/src/org/thoughtcrime/securesms/components/ThumbnailView.java @@ -1,8 +1,12 @@ package org.thoughtcrime.securesms.components; +import android.annotation.TargetApi; +import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.net.Uri; +import android.os.Build.VERSION; +import android.os.Build.VERSION_CODES; import android.os.Handler; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -44,6 +48,11 @@ public class ThumbnailView extends ForegroundImageView { super(context, attrs, defStyle); } + @Override protected void onDetachedFromWindow() { + Glide.clear(this); + super.onDetachedFromWindow(); + } + public void setImageResource(@NonNull ListenableFutureTask slideDeckFuture, @Nullable MasterSecret masterSecret) { @@ -57,12 +66,15 @@ public class ThumbnailView extends ForegroundImageView { } public void setImageResource(@NonNull Slide slide, @Nullable MasterSecret masterSecret) { - buildGlideRequest(slide, masterSecret).into(ThumbnailView.this); - setOnClickListener(new ThumbnailClickDispatcher(thumbnailClickListener, slide)); + if (isContextValid()) { + buildGlideRequest(slide, masterSecret).into(ThumbnailView.this); + setOnClickListener(new ThumbnailClickDispatcher(thumbnailClickListener, slide)); + } else { + Log.w(TAG, "Not going to load resource, context is invalid"); + } } - public void setImageResource(@NonNull Slide slide) - { + public void setImageResource(@NonNull Slide slide) { setImageResource(slide, null); } @@ -70,6 +82,13 @@ public class ThumbnailView extends ForegroundImageView { this.thumbnailClickListener = listener; } + @TargetApi(VERSION_CODES.JELLY_BEAN_MR1) + private boolean isContextValid() { + return !(getContext() instanceof Activity) || + VERSION.SDK_INT < VERSION_CODES.JELLY_BEAN_MR1 || + !((Activity)getContext()).isDestroyed(); + } + private GenericRequestBuilder buildGlideRequest(@NonNull Slide slide, @Nullable MasterSecret masterSecret) {