diff --git a/src/android/com/ionicframework/cordova/webview/IonicWebView.java b/src/android/com/ionicframework/cordova/webview/IonicWebView.java index ff771371..a9f32b4c 100644 --- a/src/android/com/ionicframework/cordova/webview/IonicWebView.java +++ b/src/android/com/ionicframework/cordova/webview/IonicWebView.java @@ -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 { @@ -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; } diff --git a/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java b/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java index 81d04978..31a702a7 100644 --- a/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java +++ b/src/android/com/ionicframework/cordova/webview/IonicWebViewEngine.java @@ -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"; @@ -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)); @@ -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); @@ -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); + } } diff --git a/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java b/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java index cf134932..a7553104 100644 --- a/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java +++ b/src/android/com/ionicframework/cordova/webview/WebViewLocalServer.java @@ -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, @@ -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; } @@ -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) { @@ -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); diff --git a/src/www/util.js b/src/www/util.js index 4acf8851..1d9a33d1 100644 --- a/src/www/util.js +++ b/src/www/util.js @@ -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]); } }