Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): provide WebViewClient accessor #2477

Merged
merged 1 commit into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}

}