1
1
package com .getcapacitor ;
2
2
3
+ import android .annotation .SuppressLint ;
3
4
import android .app .Activity ;
4
5
import android .content .ActivityNotFoundException ;
5
6
import android .content .Context ;
@@ -133,6 +134,9 @@ public class Bridge {
133
134
// An interface to manipulate route resolving
134
135
private RouteProcessor routeProcessor ;
135
136
137
+ // A pre-determined path to load the bridge
138
+ private ServerPath serverPath ;
139
+
136
140
/**
137
141
* Create the Bridge with a reference to the main {@link Activity} for the
138
142
* app, and a reference to the {@link WebView} our app will use.
@@ -150,11 +154,12 @@ public Bridge(
150
154
CordovaPreferences preferences ,
151
155
CapConfig config
152
156
) {
153
- this (context , null , webView , initialPlugins , cordovaInterface , pluginManager , preferences , config );
157
+ this (context , null , null , webView , initialPlugins , cordovaInterface , pluginManager , preferences , config );
154
158
}
155
159
156
160
private Bridge (
157
161
AppCompatActivity context ,
162
+ ServerPath serverPath ,
158
163
Fragment fragment ,
159
164
WebView webView ,
160
165
List <Class <? extends Plugin >> initialPlugins ,
@@ -164,6 +169,7 @@ private Bridge(
164
169
CapConfig config
165
170
) {
166
171
this .app = new App ();
172
+ this .serverPath = serverPath ;
167
173
this .context = context ;
168
174
this .fragment = fragment ;
169
175
this .webView = webView ;
@@ -253,8 +259,17 @@ private void loadWebView() {
253
259
setServerBasePath (path );
254
260
}
255
261
}
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
+ }
258
273
}
259
274
260
275
public boolean launchIntent (Uri url ) {
@@ -415,6 +430,7 @@ public void reset() {
415
430
/**
416
431
* Initialize the WebView, setting required flags
417
432
*/
433
+ @ SuppressLint ("SetJavaScriptEnabled" )
418
434
private void initWebView () {
419
435
WebSettings settings = webView .getSettings ();
420
436
settings .setJavaScriptEnabled (true );
@@ -1204,6 +1220,10 @@ void setRouteProcessor(RouteProcessor routeProcessor) {
1204
1220
this .routeProcessor = routeProcessor ;
1205
1221
}
1206
1222
1223
+ ServerPath getServerPath () {
1224
+ return serverPath ;
1225
+ }
1226
+
1207
1227
/**
1208
1228
* Add a listener that the WebViewClient can trigger on certain events.
1209
1229
* @param webViewListener A {@link WebViewListener} to add.
@@ -1229,6 +1249,7 @@ public static class Builder {
1229
1249
private Fragment fragment ;
1230
1250
private RouteProcessor routeProcessor ;
1231
1251
private final List <WebViewListener > webViewListeners = new ArrayList <>();
1252
+ private ServerPath serverPath ;
1232
1253
1233
1254
public Builder (AppCompatActivity activity ) {
1234
1255
this .activity = activity ;
@@ -1285,6 +1306,11 @@ public Builder setRouteProcessor(RouteProcessor routeProcessor) {
1285
1306
return this ;
1286
1307
}
1287
1308
1309
+ public Builder setServerPath (ServerPath serverPath ) {
1310
+ this .serverPath = serverPath ;
1311
+ return this ;
1312
+ }
1313
+
1288
1314
public Bridge create () {
1289
1315
// Cordova initialization
1290
1316
ConfigXmlParser parser = new ConfigXmlParser ();
@@ -1305,7 +1331,17 @@ public Bridge create() {
1305
1331
cordovaInterface .onCordovaInit (pluginManager );
1306
1332
1307
1333
// 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
+ );
1309
1345
bridge .setCordovaWebView (mockWebView );
1310
1346
bridge .setWebViewListeners (webViewListeners );
1311
1347
bridge .setRouteProcessor (routeProcessor );
0 commit comments