Browse Source

Glide crash fix

pull/70/head
Ezerous 5 years ago
parent
commit
c5326e781c
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 7
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  2. 3
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 17
      app/src/main/java/gr/thmmy/mthmmy/utils/ui/GlideUtils.java

7
app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java

@ -1,5 +1,7 @@
package gr.thmmy.mthmmy.activities.profile; package gr.thmmy.mthmmy.activities.profile;
import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; 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_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; 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.parsing.ParseHelpers.emojiTagToHtml;
import static gr.thmmy.mthmmy.utils.ui.GlideUtils.isValidContextForGlide;
import static gr.thmmy.mthmmy.utils.ui.PhotoViewUtils.displayPhotoViewImage; import static gr.thmmy.mthmmy.utils.ui.PhotoViewUtils.displayPhotoViewImage;
/** /**
@ -222,6 +225,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
avatarView.setOnClickListener(v -> displayPhotoViewImage(ProfileActivity.this, avatarUrl)); avatarView.setOnClickListener(v -> displayPhotoViewImage(ProfileActivity.this, avatarUrl));
} }
if(isValidContextForGlide(this)){
Glide.with(this) Glide.with(this)
.load(avatarUri) .load(avatarUri)
.circleCrop() .circleCrop()
@ -229,6 +233,9 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment
.placeholder(R.drawable.ic_default_user_avatar) .placeholder(R.drawable.ic_default_user_avatar)
.into(avatarView); .into(avatarView);
} }
else
Timber.d("Will not load Glide image (invalid context)");
}
/** /**
* An {@link AsyncTask} that handles asynchronous fetching of a profile page and parsing this * An {@link AsyncTask} that handles asynchronous fetching of a profile page and parsing this

3
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.activities.topic.TopicParser.USER_COLOR_YELLOW;
import static gr.thmmy.mthmmy.base.BaseActivity.getSessionManager; import static gr.thmmy.mthmmy.base.BaseActivity.getSessionManager;
import static gr.thmmy.mthmmy.utils.FileUtils.faIconFromFilename; import static gr.thmmy.mthmmy.utils.FileUtils.faIconFromFilename;
import static gr.thmmy.mthmmy.utils.ui.GlideUtils.isValidContextForGlide;
/** /**
* Custom {@link RecyclerView.Adapter} used for topics. * Custom {@link RecyclerView.Adapter} used for topics.
@ -810,6 +811,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if(imageUrl!=null) if(imageUrl!=null)
imageUrl = imageUrl.trim(); imageUrl = imageUrl.trim();
if(isValidContextForGlide(context)) {
Glide.with(context) Glide.with(context)
.load(imageUrl) .load(imageUrl)
.circleCrop() .circleCrop()
@ -817,6 +819,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
.placeholder(R.drawable.ic_default_user_avatar_darker) .placeholder(R.drawable.ic_default_user_avatar_darker)
.into(imageView); .into(imageView);
} }
}
@Override @Override
public int getItemCount() { public int getItemCount() {

17
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;
}
}
Loading…
Cancel
Save