Skip to content

Commit

Permalink
feat(android): provide WebViewClient accessor (#2477)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Feb 24, 2020
1 parent a05311d commit dd3875b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 33 deletions.
58 changes: 25 additions & 33 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -143,6 +141,7 @@ public class Bridge {
public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> 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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 = "";
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit dd3875b

Please sign in to comment.