Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(android): Move bridge localUrl initialization to initWebView #6685

Merged
merged 2 commits into from
Jun 28, 2023
Merged
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
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 @@ -104,6 +104,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 @@ -208,7 +209,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 @@ -231,51 +231,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 @@ -295,7 +260,6 @@ private void loadWebView() {
setServerBasePath(path);
}
}

if (!this.isMinimumWebViewInstalled()) {
String errorUrl = this.getErrorUrl();
if (errorUrl != null) {
Expand Down Expand Up @@ -586,6 +550,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