mirror of
https://github.com/status-im/react-native-fast-image.git
synced 2025-02-23 11:48:16 +00:00
fix: Fix IllegalArgumentException crash (Android). (#511)
Fix IllegalArgumentException crash when context was instance of ContextWrapper and activity was destroyed.
This commit is contained in:
parent
a5e3b9e64b
commit
b6c467780b
@ -2,6 +2,7 @@ package com.dylanvann.fastimage;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.os.Build;
|
||||
|
||||
@ -182,32 +183,43 @@ class FastImageViewManager extends SimpleViewManager<FastImageViewWithUrl> imple
|
||||
|
||||
|
||||
private static boolean isValidContextForGlide(final Context context) {
|
||||
if (context == null) {
|
||||
Activity activity = getActivityFromContext(context);
|
||||
|
||||
if (activity == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !isActivityDestroyed(activity);
|
||||
}
|
||||
|
||||
private static Activity getActivityFromContext(final Context context) {
|
||||
if (context instanceof Activity) {
|
||||
final Activity activity = (Activity) context;
|
||||
if (isActivityDestroyed(activity)) {
|
||||
return false;
|
||||
}
|
||||
return (Activity) context;
|
||||
}
|
||||
|
||||
if (context instanceof ThemedReactContext) {
|
||||
final Context baseContext = ((ThemedReactContext) context).getBaseContext();
|
||||
if (baseContext instanceof Activity) {
|
||||
final Activity baseActivity = (Activity) baseContext;
|
||||
return !isActivityDestroyed(baseActivity);
|
||||
return (Activity) baseContext;
|
||||
}
|
||||
|
||||
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) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
|
||||
return activity.isDestroyed() || activity.isFinishing();
|
||||
} else {
|
||||
return activity.isFinishing() || activity.isChangingConfigurations();
|
||||
return activity.isDestroyed() || activity.isFinishing() || activity.isChangingConfigurations();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user