You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ag...@apache.org on 2014/07/10 16:30:36 UTC

[02/11] android commit: Make CordovaWebview resilient to init() not being called (for backwards-compatibility)

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.


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/2f24e42d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/2f24e42d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/2f24e42d

Branch: refs/heads/4.0.x
Commit: 2f24e42dc1e9ab96b377445a1c6d1716e5c513f9
Parents: 0c12aa1
Author: Andrew Grieve <ag...@chromium.org>
Authored: Wed Jul 9 21:08:29 2014 -0400
Committer: Andrew Grieve <ag...@chromium.org>
Committed: Wed Jul 9 21:08:29 2014 -0400

----------------------------------------------------------------------
 framework/src/org/apache/cordova/Config.java      | 14 ++++++++++++++
 .../src/org/apache/cordova/CordovaWebView.java    | 18 +++++++++++++++---
 test/.classpath                                   |  6 +++---
 .../test/junit/BackButtonMultiPageTest.java       | 14 ++++++++------
 .../apache/cordova/test/junit/ErrorUrlTest.java   |  2 +-
 5 files changed, 41 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/2f24e42d/framework/src/org/apache/cordova/Config.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/Config.java b/framework/src/org/apache/cordova/Config.java
index c13d397..5cae9a2 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<PluginEntry> getPluginEntries() {
+        return parser.getPluginEntries();
+    }
+    
+    public static CordovaPreferences getPreferences() {
+        return parser.getPreferences();
+    }
+
+    public static boolean isInitialized() {
+        return parser != null;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/2f24e42d/framework/src/org/apache/cordova/CordovaWebView.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaWebView.java b/framework/src/org/apache/cordova/CordovaWebView.java
index 7c1974c..7ed7fff 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;

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/2f24e42d/test/.classpath
----------------------------------------------------------------------
diff --git a/test/.classpath b/test/.classpath
index bb0c759..5176974 100644
--- a/test/.classpath
+++ b/test/.classpath
@@ -1,9 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-	<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.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="output" path="bin/classes"/>
 </classpath>

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/2f24e42d/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java b/test/src/org/apache/cordova/test/junit/BackButtonMultiPageTest.java
index 923b376..007069f 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<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);
           }
       });
   }

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/2f24e42d/test/src/org/apache/cordova/test/junit/ErrorUrlTest.java
----------------------------------------------------------------------
diff --git a/test/src/org/apache/cordova/test/junit/ErrorUrlTest.java b/test/src/org/apache/cordova/test/junit/ErrorUrlTest.java
index afa31d4..de12bc4 100644
--- a/test/src/org/apache/cordova/test/junit/ErrorUrlTest.java
+++ b/test/src/org/apache/cordova/test/junit/ErrorUrlTest.java
@@ -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);
          
         }
     });