From 9b25d45b9355ec1fe76a96f5694577a7f1edf680 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 8 Jul 2014 14:08:15 -0400 Subject: [PATCH 1/3] Change getProperty -> prefs.get* within CordovaActivity --- .../src/org/apache/cordova/CordovaActivity.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java index ff966676..d3cc9c65 100755 --- a/framework/src/org/apache/cordova/CordovaActivity.java +++ b/framework/src/org/apache/cordova/CordovaActivity.java @@ -360,7 +360,7 @@ public class CordovaActivity extends Activity implements CordovaInterface { } // If keepRunning - this.keepRunning = this.getBooleanProperty("KeepRunning", true); + this.keepRunning = preferences.getBoolean("KeepRunning", true); //Check if the view is attached to anything if(appView.getParent() != null) @@ -401,10 +401,10 @@ public class CordovaActivity extends Activity implements CordovaInterface { // If loadingDialog property, then show the App loading dialog for first page of app String loading = null; if ((this.appView == null) || !this.appView.canGoBack()) { - loading = this.getStringProperty("LoadingDialog", null); + loading = preferences.getString("LoadingDialog", null); } else { - loading = this.getStringProperty("LoadingPageDialog", null); + loading = preferences.getString("LoadingPageDialog", null); } if (loading != null) { @@ -785,8 +785,8 @@ public class CordovaActivity extends Activity implements CordovaInterface { final CordovaActivity me = this; // If errorUrl specified, then load it - final String errorUrl = me.getStringProperty("errorUrl", null); - if ((errorUrl != null) && (errorUrl.startsWith("file://") || Config.isUrlWhiteListed(errorUrl)) && (!failingUrl.equals(errorUrl))) { + final String errorUrl = preferences.getString("errorUrl", null); + if ((errorUrl != null) && (errorUrl.startsWith("file://") || whitelist.isUrlWhiteListed(errorUrl)) && (!failingUrl.equals(errorUrl))) { // Load URL on UI thread me.runOnUiThread(new Runnable() { @@ -924,7 +924,7 @@ public class CordovaActivity extends Activity implements CordovaInterface { root.setMinimumHeight(display.getHeight()); root.setMinimumWidth(display.getWidth()); root.setOrientation(LinearLayout.VERTICAL); - root.setBackgroundColor(that.getIntegerProperty("backgroundColor", Color.BLACK)); + root.setBackgroundColor(preferences.getInteger("backgroundColor", Color.BLACK)); root.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.0F)); root.setBackgroundResource(that.splashscreen); @@ -1002,7 +1002,10 @@ public class CordovaActivity extends Activity implements CordovaInterface { else { // If the splash dialog is showing don't try to show it again if (this.splashDialog == null || !this.splashDialog.isShowing()) { - this.splashscreen = this.getIntegerProperty("SplashScreen", 0); + String splashResource = preferences.getString("SplashScreen", null); + if (splashResource != null) { + splashscreen = getResources().getIdentifier(splashResource, "drawable", getClass().getPackage().getName()); + } this.showSplashScreen(this.splashscreenTime); } } From d31ee20ba568aed498896a4165e27d87b509f946 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 8 Jul 2014 14:11:14 -0400 Subject: [PATCH 2/3] Convert usages of Config.* to use the non-static versions --- framework/src/org/apache/cordova/Config.java | 5 +++ .../org/apache/cordova/CordovaActivity.java | 5 +-- .../apache/cordova/CordovaChromeClient.java | 3 +- .../org/apache/cordova/CordovaUriHelper.java | 2 +- .../org/apache/cordova/CordovaWebView.java | 31 +++++++++---------- .../cordova/IceCreamCordovaWebViewClient.java | 2 +- 6 files changed, 26 insertions(+), 22 deletions(-) diff --git a/framework/src/org/apache/cordova/Config.java b/framework/src/org/apache/cordova/Config.java index dfb039d8..c13d3972 100644 --- a/framework/src/org/apache/cordova/Config.java +++ b/framework/src/org/apache/cordova/Config.java @@ -22,6 +22,7 @@ package org.apache.cordova; import android.app.Activity; import android.util.Log; +@Deprecated // Use Whitelist, CordovaPrefences, etc. directly. public class Config { private static final String TAG = "Config"; @@ -82,4 +83,8 @@ public class Config { public static String getErrorUrl() { return parser.getPreferences().getString("errorurl", null); } + + public static Whitelist getWhitelist() { + return parser.getWhitelist(); + } } diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java index d3cc9c65..0d079576 100755 --- a/framework/src/org/apache/cordova/CordovaActivity.java +++ b/framework/src/org/apache/cordova/CordovaActivity.java @@ -227,7 +227,7 @@ public class CordovaActivity extends Activity implements CordovaInterface { } appView = makeWebView(); - appView.init(this, makeWebViewClient(appView), makeChromeClient(appView), pluginEntries); + appView.init(this, makeWebViewClient(appView), makeChromeClient(appView), pluginEntries, whitelist); // TODO: Have the views set this themselves. if (preferences.getBoolean("DisallowOverscroll", false)) { @@ -844,8 +844,9 @@ public class CordovaActivity extends Activity implements CordovaInterface { /** * Determine if URL is in approved list of URLs to load. */ + @Deprecated // Use whitelist object directly. public boolean isUrlWhiteListed(String url) { - return Config.isUrlWhiteListed(url); + return whitelist.isUrlWhiteListed(url); } /* diff --git a/framework/src/org/apache/cordova/CordovaChromeClient.java b/framework/src/org/apache/cordova/CordovaChromeClient.java index 0337098b..cebabba2 100755 --- a/framework/src/org/apache/cordova/CordovaChromeClient.java +++ b/framework/src/org/apache/cordova/CordovaChromeClient.java @@ -240,11 +240,10 @@ public class CordovaChromeClient extends WebChromeClient { } else if (defaultValue != null && defaultValue.startsWith("gap_init:")) { - String startUrl = Config.getStartUrl(); // Protect against random iframes being able to talk through the bridge. // Trust only file URLs and the start URL's domain. // The extra origin.startsWith("http") is to protect against iframes with data: having "" as origin. - if (origin.startsWith("file:") || (origin.startsWith("http") && startUrl.startsWith(origin))) { + if (origin.startsWith("file:") || (origin.startsWith("http") && appView.loadedUrl.startsWith(origin))) { // Enable the bridge int bridgeMode = Integer.parseInt(defaultValue.substring(9)); appView.jsMessageQueue.setBridgeMode(bridgeMode); diff --git a/framework/src/org/apache/cordova/CordovaUriHelper.java b/framework/src/org/apache/cordova/CordovaUriHelper.java index a6a0dcc9..f189f1ce 100644 --- a/framework/src/org/apache/cordova/CordovaUriHelper.java +++ b/framework/src/org/apache/cordova/CordovaUriHelper.java @@ -49,7 +49,7 @@ class CordovaUriHelper { if(url.startsWith("http:") || url.startsWith("https:")) { // We only need to whitelist sites on the Internet! - if(Config.isUrlWhiteListed(url)) + if(appView.getWhitelist().isUrlWhiteListed(url)) { return false; } diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index 4650588a..fb442cc8 100755 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -26,12 +26,6 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; -import org.apache.cordova.Config; -import org.apache.cordova.CordovaInterface; -import org.apache.cordova.LOG; -import org.apache.cordova.PluginManager; -import org.apache.cordova.PluginResult; - import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.BroadcastReceiver; @@ -80,11 +74,8 @@ public class CordovaWebView extends WebView { /** Activities and other important classes **/ private CordovaInterface cordova; CordovaWebViewClient viewClient; - @SuppressWarnings("unused") private CordovaChromeClient chromeClient; - private String url; - // Flag to track that a loadUrl timeout occurred int loadUrlTimeout = 0; @@ -97,9 +88,10 @@ public class CordovaWebView extends WebView { private View mCustomView; private WebChromeClient.CustomViewCallback mCustomViewCallback; - private ActivityResult mResult = null; - private CordovaResourceApi resourceApi; + private Whitelist whitelist; + // The URL passed to loadUrl(), not necessarily the URL of the current page. + String loadedUrl; class ActivityResult { @@ -142,13 +134,15 @@ public class CordovaWebView extends WebView { } // Use two-phase init so that the control will work with XML layouts. - public void init(CordovaInterface cordova, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient, List pluginEntries) { + public void init(CordovaInterface cordova, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient, + List pluginEntries, Whitelist whitelist) { if (this.cordova != null) { throw new IllegalStateException(); } this.cordova = cordova; this.viewClient = webViewClient; this.chromeClient = webChromeClient; + this.whitelist = whitelist; super.setWebChromeClient(webChromeClient); super.setWebViewClient(webViewClient); @@ -310,6 +304,11 @@ public class CordovaWebView extends WebView { return this.chromeClient; } + + public Whitelist getWhitelist() { + return this.whitelist; + } + /** * Load the url into the webview. * @@ -357,7 +356,7 @@ public class CordovaWebView extends WebView { LOG.d(TAG, ">>> loadUrl(" + url + ")"); if (recreatePlugins) { - this.url = url; + this.loadedUrl = url; this.pluginManager.init(); } @@ -413,7 +412,7 @@ public class CordovaWebView extends WebView { if (LOG.isLoggable(LOG.DEBUG) && !url.startsWith("javascript:")) { LOG.d(TAG, ">>> loadUrlNow()"); } - if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url)) { + if (url.startsWith("file://") || url.startsWith("javascript:") || whitelist.isUrlWhiteListed(url)) { super.loadUrl(url); } } @@ -549,7 +548,7 @@ public class CordovaWebView extends WebView { if (!openExternal) { // Make sure url is in whitelist - if (url.startsWith("file://") || Config.isUrlWhiteListed(url)) { + if (url.startsWith("file://") || whitelist.isUrlWhiteListed(url)) { // TODO: What about params? // Load new URL this.loadUrl(url); @@ -897,8 +896,8 @@ public class CordovaWebView extends WebView { return myList; } + @Deprecated // This never did anything public void storeResult(int requestCode, int resultCode, Intent intent) { - mResult = new ActivityResult(requestCode, resultCode, intent); } public CordovaResourceApi getResourceApi() { diff --git a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java index 67793d73..27bb5ef8 100644 --- a/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java +++ b/framework/src/org/apache/cordova/IceCreamCordovaWebViewClient.java @@ -77,7 +77,7 @@ public class IceCreamCordovaWebViewClient extends CordovaWebViewClient { } private boolean isUrlHarmful(String url) { - return ((url.startsWith("http:") || url.startsWith("https:")) && !Config.isUrlWhiteListed(url)) + return ((url.startsWith("http:") || url.startsWith("https:")) && !appView.getWhitelist().isUrlWhiteListed(url)) || url.contains("app_webview"); } From 04ccb06e3f6297f3956d847507423c66006eae08 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 8 Jul 2014 14:26:21 -0400 Subject: [PATCH 3/3] Provide CordovaPlugin with CordovaPreferences. Add new Plugin.initialize() This adds CordovaPlugin.initialize() (no args) and deprecates CordovaPlugin.initialize(app, webView). This will allow us to refactor more easily by using the package-private privateInitialize() to set fields. --- framework/src/org/apache/cordova/App.java | 8 ++----- .../org/apache/cordova/CordovaActivity.java | 3 ++- .../src/org/apache/cordova/CordovaPlugin.java | 21 ++++++++++++++----- .../org/apache/cordova/CordovaWebView.java | 8 ++++++- .../src/org/apache/cordova/PluginEntry.java | 2 +- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/framework/src/org/apache/cordova/App.java b/framework/src/org/apache/cordova/App.java index 21125194..c488f10a 100755 --- a/framework/src/org/apache/cordova/App.java +++ b/framework/src/org/apache/cordova/App.java @@ -47,16 +47,12 @@ public class App extends CordovaPlugin { /** * Sets the context of the Command. This can then be used to do things like * get file paths associated with the Activity. - * - * @param cordova The context of the main Activity. - * @param webView The CordovaWebView Cordova is running in. */ - public void initialize(CordovaInterface cordova, CordovaWebView webView) { - super.initialize(cordova, webView); + @Override + public void initialize() { this.initTelephonyReceiver(); } - /** * Executes the request and returns PluginResult. * diff --git a/framework/src/org/apache/cordova/CordovaActivity.java b/framework/src/org/apache/cordova/CordovaActivity.java index 0d079576..371172c9 100755 --- a/framework/src/org/apache/cordova/CordovaActivity.java +++ b/framework/src/org/apache/cordova/CordovaActivity.java @@ -227,7 +227,7 @@ public class CordovaActivity extends Activity implements CordovaInterface { } appView = makeWebView(); - appView.init(this, makeWebViewClient(appView), makeChromeClient(appView), pluginEntries, whitelist); + appView.init(this, makeWebViewClient(appView), makeChromeClient(appView), pluginEntries, whitelist, preferences); // TODO: Have the views set this themselves. if (preferences.getBoolean("DisallowOverscroll", false)) { @@ -240,6 +240,7 @@ public class CordovaActivity extends Activity implements CordovaInterface { setVolumeControlStream(AudioManager.STREAM_MUSIC); } + @SuppressWarnings("deprecation") protected void loadConfig() { ConfigXmlParser parser = new ConfigXmlParser(); parser.parse(this); diff --git a/framework/src/org/apache/cordova/CordovaPlugin.java b/framework/src/org/apache/cordova/CordovaPlugin.java index 8111e7b7..eff66c89 100644 --- a/framework/src/org/apache/cordova/CordovaPlugin.java +++ b/framework/src/org/apache/cordova/CordovaPlugin.java @@ -32,20 +32,31 @@ import android.net.Uri; * Plugins must extend this class and override one of the execute methods. */ public class CordovaPlugin { + @Deprecated // This is never set. public String id; public CordovaWebView webView; // WebView object public CordovaInterface cordova; + protected CordovaPreferences preferences; - /** - * @param cordova The context of the main Activity. - * @param webView The associated CordovaWebView. - */ - public void initialize(CordovaInterface cordova, CordovaWebView webView) { + void privateInitialize(CordovaInterface cordova, CordovaWebView webView, CordovaPreferences preferences) { assert this.cordova == null; this.cordova = cordova; this.webView = webView; + this.preferences = preferences; + initialize(cordova, webView); + initialize(); } + @Deprecated // Override initialize() instead. + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + } + + /** + * This is where you can do start-up logic with protected fields set. + */ + protected void initialize() { + } + /** * Executes the request. * diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index fb442cc8..7c1974c9 100755 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -92,6 +92,7 @@ public class CordovaWebView extends WebView { private Whitelist whitelist; // The URL passed to loadUrl(), not necessarily the URL of the current page. String loadedUrl; + private CordovaPreferences preferences; class ActivityResult { @@ -135,7 +136,7 @@ public class CordovaWebView extends WebView { // Use two-phase init so that the control will work with XML layouts. public void init(CordovaInterface cordova, CordovaWebViewClient webViewClient, CordovaChromeClient webChromeClient, - List pluginEntries, Whitelist whitelist) { + List pluginEntries, Whitelist whitelist, CordovaPreferences preferences) { if (this.cordova != null) { throw new IllegalStateException(); } @@ -143,6 +144,7 @@ public class CordovaWebView extends WebView { this.viewClient = webViewClient; this.chromeClient = webChromeClient; this.whitelist = whitelist; + this.preferences = preferences; super.setWebChromeClient(webChromeClient); super.setWebViewClient(webViewClient); @@ -903,4 +905,8 @@ public class CordovaWebView extends WebView { public CordovaResourceApi getResourceApi() { return resourceApi; } + + public CordovaPreferences getPreferences() { + return preferences; + } } diff --git a/framework/src/org/apache/cordova/PluginEntry.java b/framework/src/org/apache/cordova/PluginEntry.java index c54f6cb2..e94cf1c7 100755 --- a/framework/src/org/apache/cordova/PluginEntry.java +++ b/framework/src/org/apache/cordova/PluginEntry.java @@ -98,7 +98,7 @@ public class PluginEntry { Class c = getClassByName(this.pluginClass); if (isCordovaPlugin(c)) { this.plugin = (CordovaPlugin) c.newInstance(); - this.plugin.initialize(ctx, webView); + this.plugin.privateInitialize(ctx, webView, webView.getPreferences()); return plugin; } } catch (Exception e) {