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.
This commit is contained in:
Andrew Grieve 2014-07-09 21:08:29 -04:00
parent 0c12aa163e
commit 2f24e42dc1
5 changed files with 41 additions and 13 deletions

View File

@ -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<PluginEntry> getPluginEntries() {
return parser.getPluginEntries();
}
public static CordovaPreferences getPreferences() {
return parser.getPreferences();
}
public static boolean isInitialized() {
return parser != null;
}
}

View File

@ -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;

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>

View File

@ -75,7 +75,7 @@ public class BackButtonMultiPageTest extends ActivityInstrumentationTestCase2<ba
public void run()
{
String url = testView.getUrl();
assertTrue(url.endsWith("sample2.html"));
assertEquals("file:///android_asset/www/backbuttonmultipage/sample2.html", url);
testView.sendJavascript("window.location = 'sample3.html';"); }
});
@ -84,8 +84,10 @@ public class BackButtonMultiPageTest extends ActivityInstrumentationTestCase2<ba
public void run()
{
String url = testView.getUrl();
assertTrue(url.endsWith("sample3.html"));
testView.backHistory();
assertEquals("file:///android_asset/www/backbuttonmultipage/sample3.html", url);
testView.printBackForwardList();
assertTrue(testView.backHistory());
testView.printBackForwardList();
}
});
sleep();
@ -93,8 +95,8 @@ public class BackButtonMultiPageTest extends ActivityInstrumentationTestCase2<ba
public void run()
{
String url = testView.getUrl();
assertTrue(url.endsWith("sample2.html"));
testView.backHistory();
assertEquals("file:///android_asset/www/backbuttonmultipage/sample2.html", url);
assertTrue(testView.backHistory());
}
});
sleep();
@ -102,7 +104,7 @@ public class BackButtonMultiPageTest extends ActivityInstrumentationTestCase2<ba
public void run()
{
String url = testView.getUrl();
assertTrue(url.endsWith("index.html"));
assertEquals("file:///android_asset/www/backbuttonmultipage/index.html", url);
}
});
}

View File

@ -63,7 +63,7 @@ public class ErrorUrlTest extends ActivityInstrumentationTestCase2<errorurl> {
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);
}
});