Skip to content

Commit

Permalink
fix(android): added ServerPath object and building options for settin…
Browse files Browse the repository at this point in the history
…g initial load from portals (#6008)
  • Loading branch information
carlpoole committed Oct 19, 2022
1 parent 2272abf commit 205b6e6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
43 changes: 39 additions & 4 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Expand Up @@ -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.
Expand All @@ -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<Class<? extends Plugin>> initialPlugins,
Expand All @@ -173,6 +177,7 @@ private Bridge(
CapConfig config
) {
this.app = new App();
this.serverPath = serverPath;
this.context = context;
this.fragment = fragment;
this.webView = webView;
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -1345,6 +1364,7 @@ public static class Builder {
private Fragment fragment;
private RouteProcessor routeProcessor;
private final List<WebViewListener> webViewListeners = new ArrayList<>();
private ServerPath serverPath;

public Builder(AppCompatActivity activity) {
this.activity = activity;
Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down
25 changes: 25 additions & 0 deletions 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;
}
}

0 comments on commit 205b6e6

Please sign in to comment.