cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject [1/2] git commit: Add an AppHarnessUi.execJs() command
Date Wed, 14 May 2014 20:57:09 GMT
Repository: cordova-app-harness
Updated Branches:
  refs/heads/master 04e2fe816 -> 5319e5dea


Add an AppHarnessUi.execJs() command


Project: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/commit/44e3d14a
Tree: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/tree/44e3d14a
Diff: http://git-wip-us.apache.org/repos/asf/cordova-app-harness/diff/44e3d14a

Branch: refs/heads/master
Commit: 44e3d14a4729a1bd30092bec2f40138bd7d7a182
Parents: 04e2fe8
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Wed May 14 16:54:21 2014 -0400
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Wed May 14 16:55:30 2014 -0400

----------------------------------------------------------------------
 AppHarnessUI/AppHarnessUI.java | 23 +++++++++++++++++++++++
 AppHarnessUI/AppHarnessUI.m    | 11 +++++++++++
 AppHarnessUI/appharnessui.js   |  4 ++++
 www/cdvah/js/AppHarnessUI.js   |  5 +++++
 4 files changed, 43 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/44e3d14a/AppHarnessUI/AppHarnessUI.java
----------------------------------------------------------------------
diff --git a/AppHarnessUI/AppHarnessUI.java b/AppHarnessUI/AppHarnessUI.java
index fecf374..a8c2dd7 100644
--- a/AppHarnessUI/AppHarnessUI.java
+++ b/AppHarnessUI/AppHarnessUI.java
@@ -31,7 +31,9 @@ import org.apache.cordova.PluginEntry;
 import org.apache.cordova.PluginResult;
 import org.json.JSONException;
 
+import android.annotation.SuppressLint;
 import android.content.Context;
+import android.os.Build;
 import android.util.Log;
 import android.view.MotionEvent;
 import android.view.ScaleGestureDetector;
@@ -81,6 +83,13 @@ public class AppHarnessUI extends CordovaPlugin {
                     destroyOverlay(callbackContext);
                 }
             });
+        } else if ("evalJs".equals(action)) {
+            final String code = args.getString(0);
+            this.cordova.getActivity().runOnUiThread(new Runnable() {
+                public void run() {
+                    evalJs(code, callbackContext);
+                }
+            });
         } else if ("events".equals(action)) {
             eventsCallback = callbackContext;
         } else {
@@ -95,6 +104,20 @@ public class AppHarnessUI extends CordovaPlugin {
         eventsCallback.sendPluginResult(pluginResult );
     }
 
+    @SuppressLint("NewApi")
+    private void evalJs(String code, CallbackContext callbackContext) {
+        if (slaveWebView == null) {
+            Log.w(LOG_TAG, "Not evaluating JS since no app is active");
+        } else {
+            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
+                slaveWebView.loadUrl("javascript:" + code);
+            } else {
+                slaveWebView.evaluateJavascript(code, null);
+            }
+        }
+        callbackContext.success();
+    }
+
     private void create(String url, CallbackContext callbackContext) {
         CordovaActivity activity = (CordovaActivity)cordova.getActivity();
 

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/44e3d14a/AppHarnessUI/AppHarnessUI.m
----------------------------------------------------------------------
diff --git a/AppHarnessUI/AppHarnessUI.m b/AppHarnessUI/AppHarnessUI.m
index 01dcbef..f14c60a 100644
--- a/AppHarnessUI/AppHarnessUI.m
+++ b/AppHarnessUI/AppHarnessUI.m
@@ -111,6 +111,17 @@
     [self.commandDelegate sendPluginResult:pluginResult callbackId:_eventsCallbackId];
 }
 
+- (void)evalJs:(CDVInvokedUrlCommand*)command {
+    NSString* code = [command argumentAtIndex:0];
+    if (_slaveCordovaViewController == nil) {
+        NSLog(@"AppHarnessUI.evalJs: Not evaluating JS since no app is active.");
+    } else {
+        [_slaveCordovaViewController.webView stringByEvaluatingJavaScriptFromString:code];
+    }
+    CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
+    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
+}
+
 - (void)create:(CDVInvokedUrlCommand*)command {
     NSString* url = [command argumentAtIndex:0];
     if (_slaveCordovaViewController == nil) {

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/44e3d14a/AppHarnessUI/appharnessui.js
----------------------------------------------------------------------
diff --git a/AppHarnessUI/appharnessui.js b/AppHarnessUI/appharnessui.js
index ca1109b..65864a6 100644
--- a/AppHarnessUI/appharnessui.js
+++ b/AppHarnessUI/appharnessui.js
@@ -42,3 +42,7 @@ exports.destroyOverlay = function(win) {
     exec(win, null, 'AppHarnessUI', 'destroyOverlay', []);
 };
 
+exports.evalJs = function(code, win) {
+    exec(win, null, 'AppHarnessUI', 'evalJs', [code]);
+};
+

http://git-wip-us.apache.org/repos/asf/cordova-app-harness/blob/44e3d14a/www/cdvah/js/AppHarnessUI.js
----------------------------------------------------------------------
diff --git a/www/cdvah/js/AppHarnessUI.js b/www/cdvah/js/AppHarnessUI.js
index ed76347..be71941 100644
--- a/www/cdvah/js/AppHarnessUI.js
+++ b/www/cdvah/js/AppHarnessUI.js
@@ -25,6 +25,11 @@
             },
             setEventHandler: function(f) {
                 cordova.plugins.appharnessui.onEvent = f;
+            },
+            evalJs: function(code) {
+                var deferred = $q.defer();
+                cordova.plugins.appharnessui.evalJs(code, deferred.resolve);
+                return deferred.promise;
             }
         };
     }]);


Mime
View raw message