diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index e082ac5c9..72ecfcbc6 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -13,11 +13,8 @@ import android.os.HandlerThread; import android.util.Log; import android.webkit.ValueCallback; -import android.webkit.WebResourceRequest; -import android.webkit.WebResourceResponse; import android.webkit.WebSettings; import android.webkit.WebView; -import android.webkit.WebViewClient; import android.content.SharedPreferences; import com.getcapacitor.android.BuildConfig; @@ -108,6 +105,7 @@ public class Bridge { private final WebView webView; public final CordovaInterfaceImpl cordovaInterface; private CordovaPreferences preferences; + private BridgeWebViewClient webViewClient; // Our MessageHandler for sending and receiving data to the WebView private final MessageHandler msgHandler; @@ -143,6 +141,7 @@ public class Bridge { public Bridge(Activity context, WebView webView, List> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) { this.context = context; this.webView = webView; + this.webViewClient = new BridgeWebViewClient(this); this.initialPlugins = initialPlugins; this.cordovaInterface = cordovaInterface; this.preferences = preferences; @@ -213,36 +212,7 @@ private void loadWebView() { Log.d(LOG_TAG, "Loading app at " + appUrl); webView.setWebChromeClient(new BridgeWebChromeClient(this)); - webView.setWebViewClient(new WebViewClient() { - @Override - public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { - return localServer.shouldInterceptRequest(request); - } - - @Override - public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { - Uri url = request.getUrl(); - return launchIntent(url); - } - - @Override - public boolean shouldOverrideUrlLoading(WebView view, String url) { - return launchIntent(Uri.parse(url)); - } - - private boolean launchIntent(Uri url) { - if (!url.toString().contains(appUrl) && !appAllowNavigationMask.matches(url.getHost())) { - try { - Intent openIntent = new Intent(Intent.ACTION_VIEW, url); - getContext().startActivity(openIntent); - } catch (ActivityNotFoundException e) { - // TODO - trigger an event - } - return true; - } - return false; - } - }); + webView.setWebViewClient(this.webViewClient); if (!isDeployDisabled() && !isNewBinary()) { SharedPreferences prefs = getContext().getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE); @@ -255,6 +225,19 @@ private boolean launchIntent(Uri url) { webView.loadUrl(appUrl); } + public boolean launchIntent(Uri url) { + if (!url.toString().contains(appUrl) && !appAllowNavigationMask.matches(url.getHost())) { + try { + Intent openIntent = new Intent(Intent.ACTION_VIEW, url); + getContext().startActivity(openIntent); + } catch (ActivityNotFoundException e) { + // TODO - trigger an event + } + return true; + } + return false; + } + private boolean isNewBinary() { String versionCode = ""; @@ -899,4 +882,13 @@ public WebViewLocalServer getLocalServer() { public HostMask getAppAllowNavigationMask() { return appAllowNavigationMask; } + + public BridgeWebViewClient getWebViewClient() { + return this.webViewClient; + } + + public void setWebViewClient(BridgeWebViewClient client) { + this.webViewClient = client; + } + } diff --git a/android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java b/android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java new file mode 100644 index 000000000..d76139777 --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/BridgeWebViewClient.java @@ -0,0 +1,32 @@ +package com.getcapacitor; + +import android.net.Uri; +import android.webkit.WebResourceRequest; +import android.webkit.WebResourceResponse; +import android.webkit.WebView; +import android.webkit.WebViewClient; + +public class BridgeWebViewClient extends WebViewClient { + private Bridge bridge; + + public BridgeWebViewClient(Bridge bridge) { + this.bridge = bridge; + } + + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + return bridge.getLocalServer().shouldInterceptRequest(request); + } + + @Override + public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { + Uri url = request.getUrl(); + return bridge.launchIntent(url); + } + + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + return bridge.launchIntent(Uri.parse(url)); + } + +}