From 2f24e42dc1e9ab96b377445a1c6d1716e5c513f9 Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Wed, 9 Jul 2014 21:08:29 -0400 Subject: [PATCH] Make CordovaWebview resilient to init() not being called (for backwards-compatibility) This can happen when apps are not utilizing CordovaActivity and instead creating their own CordovaWebView. --- framework/src/org/apache/cordova/Config.java | 14 ++++++++++++++ .../src/org/apache/cordova/CordovaWebView.java | 18 +++++++++++++++--- test/.classpath | 6 +++--- .../test/junit/BackButtonMultiPageTest.java | 14 ++++++++------ .../cordova/test/junit/ErrorUrlTest.java | 2 +- 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/framework/src/org/apache/cordova/Config.java b/framework/src/org/apache/cordova/Config.java index c13d3972..5cae9a27 100644 --- a/framework/src/org/apache/cordova/Config.java +++ b/framework/src/org/apache/cordova/Config.java @@ -19,6 +19,8 @@ package org.apache.cordova; +import java.util.List; + import android.app.Activity; import android.util.Log; @@ -87,4 +89,16 @@ public class Config { public static Whitelist getWhitelist() { return parser.getWhitelist(); } + + public static List getPluginEntries() { + return parser.getPluginEntries(); + } + + public static CordovaPreferences getPreferences() { + return parser.getPreferences(); + } + + public static boolean isInitialized() { + return parser != null; + } } diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java index 7c1974c9..7ed7fff8 100755 --- a/framework/src/org/apache/cordova/CordovaWebView.java +++ b/framework/src/org/apache/cordova/CordovaWebView.java @@ -158,6 +158,19 @@ public class CordovaWebView extends WebView { exposeJsInterface(); } + @SuppressWarnings("deprecation") + private void initIfNecessary() { + if (pluginManager == null) { + Log.w(TAG, "CordovaWebView.init() was not called. This will soon be required."); + // Before the refactor to a two-phase init, the Context needed to implement CordovaInterface. + CordovaInterface cdv = (CordovaInterface)getContext(); + if (!Config.isInitialized()) { + Config.init(cdv.getActivity()); + } + init(cdv, makeWebViewClient(cdv), makeWebChromeClient(cdv), Config.getPluginEntries(), Config.getWhitelist(), Config.getPreferences()); + } + } + @SuppressLint("SetJavaScriptEnabled") @SuppressWarnings("deprecation") private void initWebViewSettings() { @@ -357,6 +370,8 @@ public class CordovaWebView extends WebView { public void loadUrlIntoView(final String url, boolean recreatePlugins) { LOG.d(TAG, ">>> loadUrl(" + url + ")"); + initIfNecessary(); + if (recreatePlugins) { this.loadedUrl = url; this.pluginManager.init(); @@ -515,13 +530,10 @@ public class CordovaWebView extends WebView { * @return true if we went back, false if we are already at top */ public boolean backHistory() { - // Check webview first to see if there is a history // This is needed to support curPage#diffLink, since they are added to appView's history, but not our history url array (JQMobile behavior) if (super.canGoBack()) { - printBackForwardList(); super.goBack(); - return true; } return false; diff --git a/test/.classpath b/test/.classpath index bb0c7597..51769745 100644 --- a/test/.classpath +++ b/test/.classpath @@ -1,9 +1,9 @@ + + + - - - diff --git a/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java b/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java index 923b3763..007069f9 100644 --- a/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java +++ b/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java @@ -75,7 +75,7 @@ public class BackButtonMultiPageTest extends ActivityInstrumentationTestCase2 { String good_url = "file:///android_asset/www/htmlnotfound/error.html"; String url = testView.getUrl(); assertNotNull(url); - assertTrue(url.equals(good_url)); + assertEquals(good_url, url); } });