diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index ee6e6b7fa..bca83fbaf 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -142,6 +142,9 @@ public class Bridge { // An interface to manipulate route resolving private RouteProcessor routeProcessor; + // A pre-determined path to load the bridge + private ServerPath serverPath; + /** * 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. @@ -159,11 +162,12 @@ public Bridge( CordovaPreferences preferences, CapConfig config ) { - this(context, null, webView, initialPlugins, cordovaInterface, pluginManager, preferences, config); + this(context, null, null, webView, initialPlugins, cordovaInterface, pluginManager, preferences, config); } private Bridge( AppCompatActivity context, + ServerPath serverPath, Fragment fragment, WebView webView, List> initialPlugins, @@ -173,6 +177,7 @@ private Bridge( CapConfig config ) { this.app = new App(); + this.serverPath = serverPath; this.context = context; this.fragment = fragment; this.webView = webView; @@ -294,8 +299,17 @@ private void loadWebView() { } } - // Get to work - webView.loadUrl(appUrl); + // If serverPath configured, start server based on provided path + if (serverPath != null) { + if (serverPath.getType() == ServerPath.PathType.ASSET_PATH) { + setServerAssetPath(serverPath.getPath()); + } else { + setServerBasePath(serverPath.getPath()); + } + } else { + // Get to work + webView.loadUrl(appUrl); + } } @SuppressLint("WebViewApiAvailability") @@ -514,6 +528,7 @@ public void reset() { /** * Initialize the WebView, setting required flags */ + @SuppressLint("SetJavaScriptEnabled") private void initWebView() { WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); @@ -1320,6 +1335,10 @@ void setRouteProcessor(RouteProcessor routeProcessor) { this.routeProcessor = routeProcessor; } + ServerPath getServerPath() { + return serverPath; + } + /** * Add a listener that the WebViewClient can trigger on certain events. * @param webViewListener A {@link WebViewListener} to add. @@ -1345,6 +1364,7 @@ public static class Builder { private Fragment fragment; private RouteProcessor routeProcessor; private final List webViewListeners = new ArrayList<>(); + private ServerPath serverPath; public Builder(AppCompatActivity activity) { this.activity = activity; @@ -1401,6 +1421,11 @@ public Builder setRouteProcessor(RouteProcessor routeProcessor) { return this; } + public Builder setServerPath(ServerPath serverPath) { + this.serverPath = serverPath; + return this; + } + public Bridge create() { // Cordova initialization ConfigXmlParser parser = new ConfigXmlParser(); @@ -1421,7 +1446,17 @@ public Bridge create() { cordovaInterface.onCordovaInit(pluginManager); // Bridge initialization - Bridge bridge = new Bridge(activity, fragment, webView, plugins, cordovaInterface, pluginManager, preferences, config); + Bridge bridge = new Bridge( + activity, + serverPath, + fragment, + webView, + plugins, + cordovaInterface, + pluginManager, + preferences, + config + ); bridge.setCordovaWebView(mockWebView); bridge.setWebViewListeners(webViewListeners); bridge.setRouteProcessor(routeProcessor); diff --git a/android/capacitor/src/main/java/com/getcapacitor/ServerPath.java b/android/capacitor/src/main/java/com/getcapacitor/ServerPath.java new file mode 100644 index 000000000..5b34b460c --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/ServerPath.java @@ -0,0 +1,25 @@ +package com.getcapacitor; + +public class ServerPath { + + public enum PathType { + BASE_PATH, + ASSET_PATH + } + + private final PathType type; + private final String path; + + public ServerPath(PathType type, String path) { + this.type = type; + this.path = path; + } + + public PathType getType() { + return type; + } + + public String getPath() { + return path; + } +}