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

[Android] Use correct prefix for remapping URLs #17

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
85 changes: 67 additions & 18 deletions src/android/AppScopePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,38 +123,87 @@ public Uri remapUri(Uri uri) {
remapped = "index.html" + remapped;
}

String resultURL = "file:///android_asset/www/" + remapped;
String resultURL = getLaunchUrlPrefix() + remapped;

String codePushPrefix = tryFindCodePushPrefix();
if (codePushPrefix != null) {
resultURL = codePushPrefix + remapped;
}

LOG.d(TAG, "Result URL is " + resultURL);
return Uri.parse(resultURL);
}

/**
* Get the default prefix for Cordova web assets.
*
* This needs to determine whether we're serving from a file:/// URL or
* from a scheme/hostname, and return the correct prefix for the remapped
* URL.
*/
private String getLaunchUrlPrefix() {
Integer major = 0;
try {
major = Integer.parseInt(CordovaWebView.CORDOVA_VERSION.split("\\.", 2)[0]);
} catch (Exception e) {
major = 0;
}

// Before Cordova Android 10, we always use file:/// URLs
if ((major > 0 && major < 10) || preferences.getBoolean("AndroidInsecureFileModeEnabled", false)) {
return "file:///android_asset/www/";
} else {
String scheme = preferences.getString("scheme", "https").toLowerCase();
String hostname = preferences.getString("hostname", "localhost").toLowerCase();

if (!scheme.contentEquals("http") && !scheme.contentEquals("https")) {
scheme = "https";
}

return scheme + "://" + hostname + '/';
}
}

/**
* Try to find a CodePush path prefix for the current version.
*
* This conditionally checks if the CodePush plugin is installed and then
* tries to look up the directory for the current CodePush package version,
* so that the right prefix can be used for a remapped URL within the
* CodePush package.
*/
private String tryFindCodePushPrefix() {
try {
CordovaPlugin codepush = this.webView.getPluginManager().getPlugin("CodePush");
if (codepush != null) {
Class<?> codepushClass = codepush.getClass();
Field pkgMgr = codepushClass.getDeclaredField("codePushPackageManager");
pkgMgr.setAccessible(true);
if (codepush == null) {
return null;
}

Class<?> codepushClass = codepush.getClass();
Field pkgMgr = codepushClass.getDeclaredField("codePushPackageManager");
pkgMgr.setAccessible(true);

Object codePushPackageManager = pkgMgr.get(codepush);
Object codePushPackageManager = pkgMgr.get(codepush);

Class<?> cppmClass = pkgMgr.getType();
Class<?> cppmClass = pkgMgr.getType();

Method getCurrentPackageMetadata = cppmClass.getDeclaredMethod("getCurrentPackageMetadata");
Method getCurrentPackageMetadata = cppmClass.getDeclaredMethod("getCurrentPackageMetadata");

Object packageMetadata = getCurrentPackageMetadata.invoke(codePushPackageManager);
Object packageMetadata = getCurrentPackageMetadata.invoke(codePushPackageManager);

if (packageMetadata != null) {
Class<?> metadataClass = getCurrentPackageMetadata.getReturnType();
Field localPathField = metadataClass.getDeclaredField("localPath");
String localPath = (String)localPathField.get(packageMetadata);
if (packageMetadata != null) {
Class<?> metadataClass = getCurrentPackageMetadata.getReturnType();
Field localPathField = metadataClass.getDeclaredField("localPath");
String localPath = (String)localPathField.get(packageMetadata);

if (localPath != null) {
resultURL = "file://" + this.cordova.getActivity().getFilesDir() + localPath + "www/" + remapped;
}
if (localPath != null) {
return "file://" + this.cordova.getActivity().getFilesDir() + localPath + "www/";
}
}
} catch (Exception e) {
LOG.e(TAG, Log.getStackTraceString(e));
}

LOG.d(TAG, "Result URL is " + resultURL);
return Uri.parse(resultURL);
return null;
}
}