Merge pull request #5783 from k9mail/avoid_glide_crashes

Avoid Glide crash
This commit is contained in:
cketti 2021-11-22 20:09:36 +01:00 committed by GitHub
commit c0c0e05a29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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;
}