Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed IllegalArgumentException crash when context was instance of Con… #511

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.PorterDuff;
import android.os.Build;

Expand Down Expand Up @@ -182,32 +183,43 @@ private boolean isNullOrEmpty(final String url) {


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();
}

}
Expand Down