From e22597a951cc8b321165916a2f024f952c873c4f Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Tue, 18 Oct 2022 13:12:43 -0500 Subject: [PATCH 1/2] added ServerPath object and building options for setting initial load from portals --- .../main/java/com/getcapacitor/Bridge.java | 34 ++++++++++++++++--- .../java/com/getcapacitor/ServerPath.java | 23 +++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 android/capacitor/src/main/java/com/getcapacitor/ServerPath.java diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index e2816d82b1..a62f0b60ee 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -1,5 +1,6 @@ package com.getcapacitor; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Context; @@ -133,6 +134,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. @@ -150,11 +154,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, @@ -164,6 +169,7 @@ private Bridge( CapConfig config ) { this.app = new App(); + this.serverPath = serverPath; this.context = context; this.fragment = fragment; this.webView = webView; @@ -253,8 +259,17 @@ private void loadWebView() { setServerBasePath(path); } } - // 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 { + webView.loadUrl(appUrl); + } } public boolean launchIntent(Uri url) { @@ -415,6 +430,7 @@ public void reset() { /** * Initialize the WebView, setting required flags */ + @SuppressLint("SetJavaScriptEnabled") private void initWebView() { WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); @@ -1204,6 +1220,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. @@ -1229,6 +1249,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; @@ -1285,6 +1306,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(); @@ -1305,7 +1331,7 @@ 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 0000000000..93807d7617 --- /dev/null +++ b/android/capacitor/src/main/java/com/getcapacitor/ServerPath.java @@ -0,0 +1,23 @@ +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; + } +} From 7163823708256ac5bf2a997bab4ae02b46011137 Mon Sep 17 00:00:00 2001 From: Carl Poole Date: Tue, 18 Oct 2022 13:18:14 -0500 Subject: [PATCH 2/2] fmt --- .../src/main/java/com/getcapacitor/Bridge.java | 14 ++++++++++++-- .../src/main/java/com/getcapacitor/ServerPath.java | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java index a62f0b60ee..b6c5c14a74 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/Bridge.java +++ b/android/capacitor/src/main/java/com/getcapacitor/Bridge.java @@ -262,7 +262,7 @@ private void loadWebView() { // If serverPath configured, start server based on provided path if (serverPath != null) { - if(serverPath.getType() == ServerPath.PathType.ASSET_PATH) { + if (serverPath.getType() == ServerPath.PathType.ASSET_PATH) { setServerAssetPath(serverPath.getPath()); } else { setServerBasePath(serverPath.getPath()); @@ -1331,7 +1331,17 @@ public Bridge create() { cordovaInterface.onCordovaInit(pluginManager); // Bridge initialization - Bridge bridge = new Bridge(activity, serverPath, 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 index 93807d7617..5b34b460cc 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/ServerPath.java +++ b/android/capacitor/src/main/java/com/getcapacitor/ServerPath.java @@ -1,8 +1,10 @@ package com.getcapacitor; public class ServerPath { + public enum PathType { - BASE_PATH, ASSET_PATH + BASE_PATH, + ASSET_PATH } private final PathType type;