From b6c467780b72e6b2440b6296f3bc520102734dae Mon Sep 17 00:00:00 2001 From: Leandro Mazzuquini Date: Fri, 5 Jul 2019 05:05:12 +0100 Subject: [PATCH] fix: Fix IllegalArgumentException crash (Android). (#511) Fix IllegalArgumentException crash when context was instance of ContextWrapper and activity was destroyed. --- .../fastimage/FastImageViewManager.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java index 61f1971..f97ec71 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java @@ -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 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(); } }