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

feat(android): hostname switching #486

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/android/com/ionicframework/cordova/webview/IonicWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public class IonicWebView extends CordovaPlugin {

public static final String WEBVIEW_PREFS_NAME = "WebViewSettings";
public static final String CDV_SERVER_PATH = "serverBasePath";
public static final String CDV_HOSTNAME = "hostname";
public static final String CDV_SCHEME = "scheme";
public static final String CDV_PATHS = "paths";

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

Expand All @@ -33,6 +36,23 @@ public void run() {
editor.putString(CDV_SERVER_PATH, path);
editor.apply();
return true;
} else if (action.equals("setOrigin")) {
final String hostname = args.getString(0);
final String scheme = args.getString(1);
final JSONArray paths = args.getJSONArray(2);
SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(WEBVIEW_PREFS_NAME,
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(CDV_HOSTNAME, hostname);
editor.putString(CDV_SCHEME, scheme);
editor.putString(CDV_PATHS, paths.toString());
editor.apply();
cordova.getActivity().runOnUiThread(new Runnable() {
public void run() {
((IonicWebViewEngine) webView.getEngine()).setOrigin(hostname, scheme, paths);
}
});
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.apache.cordova.engine.SystemWebViewClient;
import org.apache.cordova.engine.SystemWebViewEngine;
import org.apache.cordova.engine.SystemWebView;
import java.util.ArrayList;
import org.json.JSONArray;

public class IonicWebViewEngine extends SystemWebViewEngine {
public static final String TAG = "IonicWebViewEngine";
Expand Down Expand Up @@ -62,9 +64,26 @@ public void init(CordovaWebView parentWebView, CordovaInterface cordova, final C

String hostname = preferences.getString("Hostname", "localhost");
scheme = preferences.getString("Scheme", "http");

SharedPreferences prefs = cordova.getActivity().getApplicationContext()
.getSharedPreferences(IonicWebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
String hostnamePref = prefs.getString(IonicWebView.CDV_HOSTNAME, null);
String schemePref = prefs.getString(IonicWebView.CDV_SCHEME, null);
JSONArray paths = null;
if (hostnamePref != null && schemePref != null) {
String pathsPref = prefs.getString(IonicWebView.CDV_PATHS, null);
try {
paths = new JSONArray(pathsPref);
} catch(Exception e) {
//invalid ignore
}
hostname = hostnamePref;
scheme = schemePref;
}

CDV_LOCAL_SERVER = scheme + "://" + hostname;

localServer = new WebViewLocalServer(cordova.getActivity(), hostname, true, parser, scheme);
localServer = new WebViewLocalServer(cordova.getActivity(), hostname, true, parser, scheme, paths);
localServer.hostAssets("www");

webView.setWebViewClient(new ServerClient(this, parser));
Expand All @@ -75,7 +94,6 @@ public void init(CordovaWebView parentWebView, CordovaInterface cordova, final C
int mode = preferences.getInteger("MixedContentMode", 0);
settings.setMixedContentMode(mode);
}
SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(IonicWebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
String path = prefs.getString(IonicWebView.CDV_SERVER_PATH, null);
if (!isDeployDisabled() && !isNewBinary() && path != null && !path.isEmpty()) {
setServerBasePath(path);
Expand Down Expand Up @@ -163,4 +181,18 @@ public void setServerBasePath(String path) {
public String getServerBasePath() {
return this.localServer.getBasePath();
}

public void setOrigin(String hostname, String scheme, JSONArray paths) {

ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(cordova.getActivity());
this.scheme = scheme;
localServer = new WebViewLocalServer(cordova.getActivity(), hostname, true, parser, scheme, paths);
localServer.hostAssets("www");
String url = webView.getUrl();
String oldHost = CDV_LOCAL_SERVER;
CDV_LOCAL_SERVER = scheme + "://" + hostname;
url = url.replace(oldHost, CDV_LOCAL_SERVER);
webView.loadUrl(url);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.ArrayList;

import org.json.JSONArray;

/**
* Helper class meant to be used with the android.webkit.WebView class to enable hosting assets,
Expand Down Expand Up @@ -64,6 +67,7 @@ public class WebViewLocalServer {
// Whether to route all requests to paths without extensions back to `index.html`
private final boolean html5mode;
private ConfigXmlParser parser;
private JSONArray ignorePaths;

public String getAuthority() { return authority; }

Expand Down Expand Up @@ -163,13 +167,14 @@ public Uri getHttpsPrefix() {
}
}

WebViewLocalServer(Context context, String authority, boolean html5mode, ConfigXmlParser parser, String customScheme) {
WebViewLocalServer(Context context, String authority, boolean html5mode, ConfigXmlParser parser, String customScheme, JSONArray ignorePaths) {
uriMatcher = new UriMatcher(null);
this.html5mode = html5mode;
this.parser = parser;
this.protocolHandler = new AndroidProtocolHandler(context.getApplicationContext());
this.authority = authority;
this.customScheme = customScheme;
this.ignorePaths = ignorePaths;
}

private static Uri parseAndVerifyUrl(String url) {
Expand Down Expand Up @@ -223,6 +228,20 @@ public WebResourceResponse shouldInterceptRequest(Uri uri, WebResourceRequest re
return null;
}

if(this.ignorePaths != null) {
for (int i = 0; i < this.ignorePaths.length(); i++) {
try {
String path = this.ignorePaths.getString(i);

if (uri.getPath().contains(path)) {
return null;
}
} catch(Exception e) {
// Ingore
}
}
}

if (isLocalFile(uri) || uri.getAuthority().equals(this.authority)) {
Log.d("SERVER", "Handling local request: " + uri.toString());
return handleLocalRequest(uri, handler, request);
Expand Down
3 changes: 3 additions & 0 deletions src/www/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ var WebView = {
},
persistServerBasePath: function() {
exec(null, null, 'IonicWebView', 'persistServerBasePath', []);
},
setOrigin: function(hostname, scheme, paths) {
exec(null, null, 'IonicWebView', 'setOrigin', [hostname, scheme, paths]);
}
}

Expand Down