Skip to content

Commit

Permalink
fix: Fix IllegalArgumentException crash (Android). (#511)
Browse files Browse the repository at this point in the history
Fix IllegalArgumentException crash when context was instance of ContextWrapper and activity was destroyed.
  • Loading branch information
leanmazzu authored and DylanVann committed Jul 5, 2019
1 parent a5e3b9e commit b6c4677
Showing 1 changed file with 21 additions and 9 deletions.
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

0 comments on commit b6c4677

Please sign in to comment.