fix: Fix IllegalArgumentException crash (Android). (#511)

Fix IllegalArgumentException crash when context was instance of ContextWrapper and activity was destroyed.
This commit is contained in:
Leandro Mazzuquini 2019-07-05 05:05:12 +01:00 committed by Dylan Vann
parent a5e3b9e64b
commit b6c467780b
1 changed files with 21 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package com.dylanvann.fastimage;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.os.Build; import android.os.Build;
@ -182,32 +183,43 @@ class FastImageViewManager extends SimpleViewManager<FastImageViewWithUrl> imple
private static boolean isValidContextForGlide(final Context context) { private static boolean isValidContextForGlide(final Context context) {
if (context == null) { Activity activity = getActivityFromContext(context);
if (activity == null) {
return false; return false;
} }
return !isActivityDestroyed(activity);
}
private static Activity getActivityFromContext(final Context context) {
if (context instanceof Activity) { if (context instanceof Activity) {
final Activity activity = (Activity) context; return (Activity) context;
if (isActivityDestroyed(activity)) {
return false;
}
} }
if (context instanceof ThemedReactContext) { if (context instanceof ThemedReactContext) {
final Context baseContext = ((ThemedReactContext) context).getBaseContext(); final Context baseContext = ((ThemedReactContext) context).getBaseContext();
if (baseContext instanceof Activity) { if (baseContext instanceof Activity) {
final Activity baseActivity = (Activity) baseContext; return (Activity) baseContext;
return !isActivityDestroyed(baseActivity); }
if (baseContext instanceof ContextWrapper) {
final ContextWrapper contextWrapper = (ContextWrapper) baseContext;
final Context wrapperBaseContext = contextWrapper.getBaseContext();
if (wrapperBaseContext instanceof Activity) {
return (Activity) wrapperBaseContext;
}
} }
} }
return true; return null;
} }
private static boolean isActivityDestroyed(Activity activity) { private static boolean isActivityDestroyed(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return activity.isDestroyed() || activity.isFinishing(); return activity.isDestroyed() || activity.isFinishing();
} else { } else {
return activity.isFinishing() || activity.isChangingConfigurations(); return activity.isDestroyed() || activity.isFinishing() || activity.isChangingConfigurations();
} }
} }