Repository: thundernest/k-9
Issue: AccountImageLoader/Glide crash

From the Google Play developer console.
App version: 5.800

```
java.lang.IllegalArgumentException: 
  at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed (RequestManagerRetriever.java:348)
  at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java:148)
  at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java:128)
  at com.bumptech.glide.Glide.with (Glide.java:801)
  at com.fsck.k9.ui.account.AccountImageLoader.cancel (AccountImageLoader.kt:21)
  at com.fsck.k9.ui.K9Drawer$initializeImageLoader$1.cancel (K9Drawer.kt:145)
  at com.mikepenz.materialdrawer.util.DrawerImageLoader.cancelImage (DrawerImageLoader.kt:56)
  at com.mikepenz.materialdrawer.model.BaseDescribeableDrawerItem.unbindView (BaseDescribeableDrawerItem.kt:95)
  at com.mikepenz.materialdrawer.model.BaseDescribeableDrawerItem.unbindView (BaseDescribeableDrawerItem.kt:20)
  at com.mikepenz.fastadapter.listeners.OnBindViewHolderListenerImpl.unBindViewHolder (OnBindViewHolderListenerImpl.kt:36)
  at com.mikepenz.fastadapter.FastAdapter.onViewRecycled (FastAdapter.kt:410)
  at androidx.recyclerview.widget.RecyclerView$Recycler.dispatchViewRecycled (RecyclerView.java:6957)
  at androidx.recyclerview.widget.RecyclerView$Recycler.addViewHolderToRecycledViewPool (RecyclerView.java:6719)
  at androidx.recyclerview.widget.RecyclerView$Recycler.recycleViewHolderInternal (RecyclerView.java:6671)
  at androidx.recyclerview.widget.RecyclerView.removeAnimatingView (RecyclerView.java:1525)
  at androidx.recyclerview.widget.RecyclerView$ItemAnimatorRestoreListener.onAnimationFinished (RecyclerView.java:13109)
  at androidx.recyclerview.widget.RecyclerView$ItemAnimator.dispatchAnimationFinished (RecyclerView.java:13611)
  at androidx.recyclerview.widget.SimpleItemAnimator.dispatchRemoveFinished (SimpleItemAnimator.java:277)
  at androidx.recyclerview.widget.DefaultItemAnimator.endAnimations (DefaultItemAnimator.java:569)
  at androidx.recyclerview.widget.RecyclerView.onDetachedFromWindow (RecyclerView.java:3130)
  at android.view.View.dispatchDetachedFromWindow (View.java:20534)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3942)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewGroup.dispatchDetachedFromWindow (ViewGroup.java:3934)
  at android.view.ViewRootImpl.dispatchDetachedFromWindow (ViewRootImpl.java:4652)
  at android.view.ViewRootImpl.doDie (ViewRootImpl.java:7697)
  at android.view.ViewRootImpl.die (ViewRootImpl.java:7674)
  at android.view.WindowManagerGlobal.removeViewLocked (WindowManagerGlobal.java:509)
  at android.view.WindowManagerGlobal.removeView (WindowManagerGlobal.java:450)
  at android.view.WindowManagerImpl.removeViewImmediate (WindowManagerImpl.java:141)
  at android.app.ActivityThread.handleDestroyActivity (ActivityThread.java:5161)
  at android.app.ActivityThread.handleRelaunchActivityInner (ActivityThread.java:5431)
  at android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:5361)
  at android.app.servertransaction.ActivityRelaunchItem.execute (ActivityRelaunchItem.java:69)
  at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
  at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
  at android.app.ClientTransactionHandler.executeTransaction (ClientTransactionHandler.java:58)
  at android.app.ActivityThread.handleRelaunchActivityLocally (ActivityThread.java:5414)
  at android.app.ActivityThread.access$3300 (ActivityThread.java:237)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2076)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:223)
  at android.app.ActivityThread.main (ActivityThread.java:7664)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:592)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947)
```

Changed files:
app/ui/legacy/src/main/java/com/fsck/k9/ui/account/AccountImageLoader.kt

diff --git a/app/ui/legacy/src/main/java/com/fsck/k9/ui/account/AccountImageLoader.kt b/app/ui/legacy/src/main/java/com/fsck/k9/ui/account/AccountImageLoader.kt
index 3be463587..1db03ec74 100644
--- a/app/ui/legacy/src/main/java/com/fsck/k9/ui/account/AccountImageLoader.kt
+++ b/app/ui/legacy/src/main/java/com/fsck/k9/ui/account/AccountImageLoader.kt
@@ -1,5 +1,7 @@
 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
@@ -9,15 +11,27 @@ import com.bumptech.glide.load.engine.DiskCacheStrategy
  */
 class AccountImageLoader(private val accountFallbackImageProvider: AccountFallbackImageProvider) {
     fun setAccountImage(imageView: ImageView, email: String, color: Int) {
-        Glide.with(imageView.context)
-            .load(AccountImage(email, color))
-            .placeholder(accountFallbackImageProvider.getDrawable(color))
-            .diskCacheStrategy(DiskCacheStrategy.NONE)
-            .dontAnimate()
-            .into(imageView)
+        imageView.context.ifNotDestroyed { context ->
+            Glide.with(context)
+                .load(AccountImage(email, color))
+                .placeholder(accountFallbackImageProvider.getDrawable(color))
+                .diskCacheStrategy(DiskCacheStrategy.NONE)
+                .dontAnimate()
+                .into(imageView)
+        }
     }
 
     fun cancel(imageView: ImageView) {
-        Glide.with(imageView.context).clear(imageView)
+        imageView.context.ifNotDestroyed { context ->
+            Glide.with(context).clear(imageView)
+        }
+    }
+
+    private inline fun Context.ifNotDestroyed(block: (Context) -> Unit) {
+        if ((this as? Activity)?.isDestroyed == true) {
+            // Do nothing because Glide would throw an exception
+        } else {
+            block(this)
+        }
     }
 }