From 04ccb06e3f6297f3956d847507423c66006eae08 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Tue, 8 Jul 2014 14:26:21 -0400 Subject: [PATCH] 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) {