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

fix(android): added ServerPath object and building options for setting initial load from portals #6005

Merged
merged 2 commits into from
Oct 19, 2022
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
44 changes: 40 additions & 4 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.getcapacitor;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
Expand Down Expand Up @@ -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.
Expand All @@ -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<Class<? extends Plugin>> initialPlugins,
Expand All @@ -164,6 +169,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 @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand All @@ -1229,6 +1249,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 @@ -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();
Expand All @@ -1305,7 +1331,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
Original file line number Diff line number Diff line change
@@ -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;
}
}