From c5326e781c7285b3f653153f571961a9320d7256 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 8 Jun 2020 20:16:23 +0300 Subject: [PATCH] Glide crash fix --- .../activities/profile/ProfileActivity.java | 19 +++++++++++++------ .../mthmmy/activities/topic/TopicAdapter.java | 15 +++++++++------ .../gr/thmmy/mthmmy/utils/ui/GlideUtils.java | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/utils/ui/GlideUtils.java diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index 81f70fae..e88a22de 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java @@ -1,5 +1,7 @@ package gr.thmmy.mthmmy.activities.profile; +import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.graphics.Typeface; @@ -57,6 +59,7 @@ import timber.log.Timber; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; import static gr.thmmy.mthmmy.utils.parsing.ParseHelpers.emojiTagToHtml; +import static gr.thmmy.mthmmy.utils.ui.GlideUtils.isValidContextForGlide; import static gr.thmmy.mthmmy.utils.ui.PhotoViewUtils.displayPhotoViewImage; /** @@ -222,12 +225,16 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment avatarView.setOnClickListener(v -> displayPhotoViewImage(ProfileActivity.this, avatarUrl)); } - Glide.with(this) - .load(avatarUri) - .circleCrop() - .error(R.drawable.ic_default_user_avatar) - .placeholder(R.drawable.ic_default_user_avatar) - .into(avatarView); + if(isValidContextForGlide(this)){ + Glide.with(this) + .load(avatarUri) + .circleCrop() + .error(R.drawable.ic_default_user_avatar) + .placeholder(R.drawable.ic_default_user_avatar) + .into(avatarView); + } + else + Timber.d("Will not load Glide image (invalid context)"); } /** diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 213de341..e0f41e8f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -88,6 +88,7 @@ import static gr.thmmy.mthmmy.activities.topic.TopicParser.USER_COLOR_WHITE; import static gr.thmmy.mthmmy.activities.topic.TopicParser.USER_COLOR_YELLOW; import static gr.thmmy.mthmmy.base.BaseActivity.getSessionManager; import static gr.thmmy.mthmmy.utils.FileUtils.faIconFromFilename; +import static gr.thmmy.mthmmy.utils.ui.GlideUtils.isValidContextForGlide; /** * Custom {@link RecyclerView.Adapter} used for topics. @@ -810,12 +811,14 @@ class TopicAdapter extends RecyclerView.Adapter { if(imageUrl!=null) imageUrl = imageUrl.trim(); - Glide.with(context) - .load(imageUrl) - .circleCrop() - .error(R.drawable.ic_default_user_avatar_darker) - .placeholder(R.drawable.ic_default_user_avatar_darker) - .into(imageView); + if(isValidContextForGlide(context)) { + Glide.with(context) + .load(imageUrl) + .circleCrop() + .error(R.drawable.ic_default_user_avatar_darker) + .placeholder(R.drawable.ic_default_user_avatar_darker) + .into(imageView); + } } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/ui/GlideUtils.java b/app/src/main/java/gr/thmmy/mthmmy/utils/ui/GlideUtils.java new file mode 100644 index 00000000..1f3f57c7 --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/ui/GlideUtils.java @@ -0,0 +1,17 @@ +package gr.thmmy.mthmmy.utils.ui; + +import android.app.Activity; +import android.content.Context; + +public class GlideUtils { + public static boolean isValidContextForGlide(final Context context) { + if (context == null) + return false; + + if (context instanceof Activity) { + final Activity activity = (Activity) context; + return !activity.isDestroyed() && !activity.isFinishing(); + } + return true; + } +}