Avoid Glide crash

This hopefully fixes the check that tests whether the Activity has been destroyed already.
This commit is contained in:
cketti 2021-11-20 02:13:36 +01:00
parent e18e617957
commit d99c8580e6
3 changed files with 16 additions and 3 deletions

View file

@ -1,10 +1,10 @@
package com.fsck.k9.ui.account
import android.app.Activity
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.fsck.k9.ui.helper.findActivity
/**
* Load the account image into an [ImageView].
@ -28,7 +28,7 @@ class AccountImageLoader(private val accountFallbackImageProvider: AccountFallba
}
private inline fun Context.ifNotDestroyed(block: (Context) -> Unit) {
if ((this as? Activity)?.isDestroyed == true) {
if (findActivity()?.isDestroyed == true) {
// Do nothing because Glide would throw an exception
} else {
block(this)

View file

@ -0,0 +1,11 @@
@file:JvmName("ContextHelper")
package com.fsck.k9.ui.helper
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
// Source: https://stackoverflow.com/a/58249983
tailrec fun Context.findActivity(): Activity? {
return this as? Activity ?: (this as? ContextWrapper)?.baseContext?.findActivity()
}

View file

@ -14,6 +14,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.fsck.k9.K9;
import com.fsck.k9.ui.R;
import com.fsck.k9.ui.helper.ContextHelper;
import com.fsck.k9.ui.helper.SizeFormatter;
import com.fsck.k9.mailstore.AttachmentViewInfo;
@ -122,7 +123,8 @@ public class AttachmentView extends FrameLayout implements OnClickListener {
public void refreshThumbnail() {
Context context = getContext();
if (context instanceof Activity && ((Activity) context).isDestroyed()) {
Activity activity = ContextHelper.findActivity(context);
if (activity != null && activity.isDestroyed()) {
// Do nothing because Glide would throw an exception
return;
}