From cdf568ecf83cc5356876810284e8e272bf6d2327 Mon Sep 17 00:00:00 2001 From: Alan Hughes Date: Wed, 27 Mar 2024 22:19:14 +0000 Subject: [PATCH] Fix issue on bridgeless when accessing ReactContext --- .../java/com/reactnativecommunity/webview/RNCWebView.java | 5 +++++ .../com/reactnativecommunity/webview/RNCWebViewClient.java | 2 +- .../reactnativecommunity/webview/RNCWebViewManagerImpl.kt | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java index 7b3f3b3c0..6664b6f6e 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebView.java @@ -21,6 +21,7 @@ import com.facebook.react.bridge.CatalystInstance; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.LifecycleEventListener; +import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableNativeArray; import com.facebook.react.bridge.WritableNativeMap; @@ -391,6 +392,10 @@ public ThemedReactContext getThemedReactContext() { return (ThemedReactContext) this.getContext(); } + public ReactApplicationContext getReactApplicationContext() { + return this.getThemedReactContext().getReactApplicationContext(); + } + protected class RNCWebViewBridge { private String TAG = "RNCWebViewBridge"; RNCWebView mWebView; diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java index 6f4d085f8..d59e19ce8 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewClient.java @@ -87,7 +87,7 @@ public void onPageStarted(WebView webView, String url, Bitmap favicon) { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { final RNCWebView rncWebView = (RNCWebView) view; - final boolean isJsDebugging = ((ReactContext) view.getContext()).getJavaScriptContextHolder().get() == 0; + final boolean isJsDebugging = rncWebView.getReactApplicationContext().getJavaScriptContextHolder().get() == 0; if (!isJsDebugging && rncWebView.mMessagingJSModule != null) { final Pair> lock = RNCWebViewModuleImpl.shouldOverrideUrlLoadingLock.getNewLock(); diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt index 1b6ff7aa5..6a48a810e 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManagerImpl.kt @@ -92,7 +92,7 @@ class RNCWebViewManagerImpl { } webView.setDownloadListener(DownloadListener { url, userAgent, contentDisposition, mimetype, contentLength -> webView.setIgnoreErrFailedForThisURL(url) - val module = webView.themedReactContext.getNativeModule(RNCWebViewModule::class.java) ?: return@DownloadListener + val module = webView.reactApplicationContext.getNativeModule(RNCWebViewModule::class.java) ?: return@DownloadListener val request: DownloadManager.Request = try { DownloadManager.Request(Uri.parse(url)) } catch (e: IllegalArgumentException) {