Skip to content

Commit a408213

Browse files
authored
fix(android): added ServerPath object and building options for setting initial load from portals (#6005)
1 parent 8146cd2 commit a408213

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

android/capacitor/src/main/java/com/getcapacitor/Bridge.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.getcapacitor;
22

3+
import android.annotation.SuppressLint;
34
import android.app.Activity;
45
import android.content.ActivityNotFoundException;
56
import android.content.Context;
@@ -133,6 +134,9 @@ public class Bridge {
133134
// An interface to manipulate route resolving
134135
private RouteProcessor routeProcessor;
135136

137+
// A pre-determined path to load the bridge
138+
private ServerPath serverPath;
139+
136140
/**
137141
* Create the Bridge with a reference to the main {@link Activity} for the
138142
* app, and a reference to the {@link WebView} our app will use.
@@ -150,11 +154,12 @@ public Bridge(
150154
CordovaPreferences preferences,
151155
CapConfig config
152156
) {
153-
this(context, null, webView, initialPlugins, cordovaInterface, pluginManager, preferences, config);
157+
this(context, null, null, webView, initialPlugins, cordovaInterface, pluginManager, preferences, config);
154158
}
155159

156160
private Bridge(
157161
AppCompatActivity context,
162+
ServerPath serverPath,
158163
Fragment fragment,
159164
WebView webView,
160165
List<Class<? extends Plugin>> initialPlugins,
@@ -164,6 +169,7 @@ private Bridge(
164169
CapConfig config
165170
) {
166171
this.app = new App();
172+
this.serverPath = serverPath;
167173
this.context = context;
168174
this.fragment = fragment;
169175
this.webView = webView;
@@ -253,8 +259,17 @@ private void loadWebView() {
253259
setServerBasePath(path);
254260
}
255261
}
256-
// Get to work
257-
webView.loadUrl(appUrl);
262+
263+
// If serverPath configured, start server based on provided path
264+
if (serverPath != null) {
265+
if (serverPath.getType() == ServerPath.PathType.ASSET_PATH) {
266+
setServerAssetPath(serverPath.getPath());
267+
} else {
268+
setServerBasePath(serverPath.getPath());
269+
}
270+
} else {
271+
webView.loadUrl(appUrl);
272+
}
258273
}
259274

260275
public boolean launchIntent(Uri url) {
@@ -415,6 +430,7 @@ public void reset() {
415430
/**
416431
* Initialize the WebView, setting required flags
417432
*/
433+
@SuppressLint("SetJavaScriptEnabled")
418434
private void initWebView() {
419435
WebSettings settings = webView.getSettings();
420436
settings.setJavaScriptEnabled(true);
@@ -1204,6 +1220,10 @@ void setRouteProcessor(RouteProcessor routeProcessor) {
12041220
this.routeProcessor = routeProcessor;
12051221
}
12061222

1223+
ServerPath getServerPath() {
1224+
return serverPath;
1225+
}
1226+
12071227
/**
12081228
* Add a listener that the WebViewClient can trigger on certain events.
12091229
* @param webViewListener A {@link WebViewListener} to add.
@@ -1229,6 +1249,7 @@ public static class Builder {
12291249
private Fragment fragment;
12301250
private RouteProcessor routeProcessor;
12311251
private final List<WebViewListener> webViewListeners = new ArrayList<>();
1252+
private ServerPath serverPath;
12321253

12331254
public Builder(AppCompatActivity activity) {
12341255
this.activity = activity;
@@ -1285,6 +1306,11 @@ public Builder setRouteProcessor(RouteProcessor routeProcessor) {
12851306
return this;
12861307
}
12871308

1309+
public Builder setServerPath(ServerPath serverPath) {
1310+
this.serverPath = serverPath;
1311+
return this;
1312+
}
1313+
12881314
public Bridge create() {
12891315
// Cordova initialization
12901316
ConfigXmlParser parser = new ConfigXmlParser();
@@ -1305,7 +1331,17 @@ public Bridge create() {
13051331
cordovaInterface.onCordovaInit(pluginManager);
13061332

13071333
// Bridge initialization
1308-
Bridge bridge = new Bridge(activity, fragment, webView, plugins, cordovaInterface, pluginManager, preferences, config);
1334+
Bridge bridge = new Bridge(
1335+
activity,
1336+
serverPath,
1337+
fragment,
1338+
webView,
1339+
plugins,
1340+
cordovaInterface,
1341+
pluginManager,
1342+
preferences,
1343+
config
1344+
);
13091345
bridge.setCordovaWebView(mockWebView);
13101346
bridge.setWebViewListeners(webViewListeners);
13111347
bridge.setRouteProcessor(routeProcessor);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.getcapacitor;
2+
3+
public class ServerPath {
4+
5+
public enum PathType {
6+
BASE_PATH,
7+
ASSET_PATH
8+
}
9+
10+
private final PathType type;
11+
private final String path;
12+
13+
public ServerPath(PathType type, String path) {
14+
this.type = type;
15+
this.path = path;
16+
}
17+
18+
public PathType getType() {
19+
return type;
20+
}
21+
22+
public String getPath() {
23+
return path;
24+
}
25+
}

0 commit comments

Comments
 (0)