Skip to content

Commit

Permalink
fix(android): Move bridge localUrl initialization to initWebView (#6686)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Jun 28, 2023
1 parent ac6219c commit 85fd4a6
Showing 1 changed file with 33 additions and 39 deletions.
72 changes: 33 additions & 39 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class Bridge {
private String appUrlConfig;
private HostMask appAllowNavigationMask;
private Set<String> allowedOriginRules = new HashSet<String>();
private ArrayList<String> authorities = new ArrayList<>();
// A reference to the main WebView for the app
private final WebView webView;
public final MockCordovaInterfaceImpl cordovaInterface;
Expand Down Expand Up @@ -205,7 +206,6 @@ private Bridge(
// Grab any intent info that our app was launched with
Intent intent = context.getIntent();
this.intentUri = intent.getData();

// Register our core plugins
this.registerAllPlugins();

Expand All @@ -228,51 +228,16 @@ private void setAllowedOriginRules() {
allowedOriginRules.add(allowNavigation);
}
}
authorities.addAll(Arrays.asList(appAllowNavigationConfig));
}
this.appAllowNavigationMask = HostMask.Parser.parse(appAllowNavigationConfig);
}

public App getApp() {
return app;
}

private void loadWebView() {
appUrlConfig = this.getServerUrl();
String[] appAllowNavigationConfig = this.config.getAllowNavigation();

ArrayList<String> authorities = new ArrayList<>();

if (appAllowNavigationConfig != null) {
authorities.addAll(Arrays.asList(appAllowNavigationConfig));
}
this.appAllowNavigationMask = HostMask.Parser.parse(appAllowNavigationConfig);
String authority = this.getHost();
authorities.add(authority);
String scheme = this.getScheme();

localUrl = scheme + "://" + authority;

if (appUrlConfig != null) {
try {
URL appUrlObject = new URL(appUrlConfig);
authorities.add(appUrlObject.getAuthority());
} catch (Exception ex) {
Logger.error("Provided server url is invalid: " + ex.getMessage());
return;
}
localUrl = appUrlConfig;
appUrl = appUrlConfig;
} else {
appUrl = localUrl;
// custom URL schemes requires path ending with /
if (!scheme.equals(Bridge.CAPACITOR_HTTP_SCHEME) && !scheme.equals(CAPACITOR_HTTPS_SCHEME)) {
appUrl += "/";
}
}

String appUrlPath = this.config.getStartPath();
if (appUrlPath != null && !appUrlPath.trim().isEmpty()) {
appUrl += appUrlPath;
}
final boolean html5mode = this.config.isHTML5Mode();

// Start the local web server
Expand All @@ -292,7 +257,6 @@ private void loadWebView() {
setServerBasePath(path);
}
}

if (!this.isMinimumWebViewInstalled()) {
String errorUrl = this.getErrorUrl();
if (errorUrl != null) {
Expand Down Expand Up @@ -573,6 +537,36 @@ private void initWebView() {
}

WebView.setWebContentsDebuggingEnabled(this.config.isWebContentsDebuggingEnabled());

appUrlConfig = this.getServerUrl();
String authority = this.getHost();
authorities.add(authority);
String scheme = this.getScheme();

localUrl = scheme + "://" + authority;

if (appUrlConfig != null) {
try {
URL appUrlObject = new URL(appUrlConfig);
authorities.add(appUrlObject.getAuthority());
} catch (Exception ex) {
Logger.error("Provided server url is invalid: " + ex.getMessage());
return;
}
localUrl = appUrlConfig;
appUrl = appUrlConfig;
} else {
appUrl = localUrl;
// custom URL schemes requires path ending with /
if (!scheme.equals(Bridge.CAPACITOR_HTTP_SCHEME) && !scheme.equals(CAPACITOR_HTTPS_SCHEME)) {
appUrl += "/";
}
}

String appUrlPath = this.config.getStartPath();
if (appUrlPath != null && !appUrlPath.trim().isEmpty()) {
appUrl += appUrlPath;
}
}

/**
Expand Down

0 comments on commit 85fd4a6

Please sign in to comment.