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): Add overridable routing for WebViewLocalServer #5553

Merged
merged 4 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 18 additions & 0 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ public class Bridge {
// A list of listeners that trigger when webView events occur
private List<WebViewListener> webViewListeners = new ArrayList<>();

// An interface to manipulate route resolving
private RouteProcessor routeProcessor;

/**
* Create the Bridge with a reference to the main {@link Activity} for the
* app, and a reference to the {@link WebView} our app will use.
Expand Down Expand Up @@ -1190,6 +1193,14 @@ void setWebViewListeners(List<WebViewListener> webViewListeners) {
this.webViewListeners = webViewListeners;
}

RouteProcessor getRouteProcessor() {
return routeProcessor;
}

void setRouteProcessor(RouteProcessor routeProcessor) {
this.routeProcessor = routeProcessor;
}

/**
* Add a listener that the WebViewClient can trigger on certain events.
* @param webViewListener A {@link WebViewListener} to add.
Expand All @@ -1213,6 +1224,7 @@ public static class Builder {
private List<Class<? extends Plugin>> plugins = new ArrayList<>();
private AppCompatActivity activity;
private Fragment fragment;
private RouteProcessor routeProcessor;
private final List<WebViewListener> webViewListeners = new ArrayList<>();

public Builder(AppCompatActivity activity) {
Expand Down Expand Up @@ -1265,6 +1277,11 @@ public Builder addWebViewListeners(List<WebViewListener> webViewListeners) {
return this;
}

public Builder setRouteProcessor(RouteProcessor routeProcessor) {
this.routeProcessor = routeProcessor;
return this;
}

public Bridge create() {
// Cordova initialization
ConfigXmlParser parser = new ConfigXmlParser();
Expand All @@ -1288,6 +1305,7 @@ public Bridge create() {
Bridge bridge = new Bridge(activity, fragment, webView, plugins, cordovaInterface, pluginManager, preferences, config);
bridge.setCordovaWebView(mockWebView);
bridge.setWebViewListeners(webViewListeners);
bridge.setRouteProcessor(routeProcessor);

if (instanceState != null) {
bridge.restoreInstanceState(instanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class BridgeActivity extends AppCompatActivity {

protected Bridge bridge;
protected boolean keepRunning = true;
private CapConfig config;
protected CapConfig config;

private int activityDepth = 0;
private List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
private final Bridge.Builder bridgeBuilder = new Bridge.Builder(this);
protected int activityDepth = 0;
protected List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
protected final Bridge.Builder bridgeBuilder = new Bridge.Builder(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -66,7 +66,7 @@ protected void load(Bundle savedInstanceState) {
this.load();
}

private void load() {
protected void load() {
Logger.debug("Starting BridgeActivity");

bridge = bridgeBuilder.addPlugins(initialPlugins).setConfig(config).create();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.getcapacitor;

/**
* An interface used in the processing of routes
*/
public interface RouteProcessor {
String process(String path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -256,6 +255,10 @@ private WebResourceResponse handleLocalRequest(WebResourceRequest request, PathH
InputStream responseStream;
try {
String startPath = this.basePath + "/index.html";
if (bridge.getRouteProcessor() != null) {
startPath = this.basePath + bridge.getRouteProcessor().process("/index.html");
}

if (isAsset) {
responseStream = protocolHandler.openAsset(startPath);
} else {
Expand Down Expand Up @@ -467,6 +470,13 @@ private void createHostingDetails() {
public InputStream handle(Uri url) {
InputStream stream = null;
String path = url.getPath();

// Pass path to routeProcessor if present
RouteProcessor routeProcessor = bridge.getRouteProcessor();
if (routeProcessor != null) {
path = bridge.getRouteProcessor().process(path);
}

try {
if (path.startsWith(capacitorContentStart)) {
stream = protocolHandler.openContentUrl(url);
Expand Down