You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by br...@apache.org on 2013/01/18 18:33:40 UTC

[6/8] android commit: Full binary data support.

Full binary data support.

- Removed BinaryEcho; made Echo support a new binaryEcho action.
- Added CordovaArgs wrapper for JSONArray, and a new overload for
  execute that accepts a CordovaArgs. There is now a default
  implementation for the JSONArray version of execute that builds a
  CordovaArgs and calls that version of execute. The default
  implementation for the CordovaArgs execute is to return false.
- Added byte[] version of success() in CallbackContext.


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

Branch: refs/heads/master
Commit: 7530c21a9f6d5eff2d1826acdc7a8d8e286f7008
Parents: 547b683
Author: Braden Shepherdson <br...@chromium.org>
Authored: Thu Jan 17 15:58:38 2013 -0500
Committer: Braden Shepherdson <br...@chromium.org>
Committed: Thu Jan 17 15:58:38 2013 -0500

----------------------------------------------------------------------
 framework/src/org/apache/cordova/BinaryEcho.java   |   55 -------
 framework/src/org/apache/cordova/CordovaArgs.java  |  112 +++++++++++++++
 .../src/org/apache/cordova/CordovaArguments.java   |   33 -----
 framework/src/org/apache/cordova/Echo.java         |   10 +-
 .../org/apache/cordova/api/CallbackContext.java    |    9 ++
 .../src/org/apache/cordova/api/CordovaPlugin.java  |   23 +++-
 6 files changed, 149 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7530c21a/framework/src/org/apache/cordova/BinaryEcho.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/BinaryEcho.java b/framework/src/org/apache/cordova/BinaryEcho.java
deleted file mode 100644
index 38ca939..0000000
--- a/framework/src/org/apache/cordova/BinaryEcho.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
-       Licensed to the Apache Software Foundation (ASF) under one
-       or more contributor license agreements.  See the NOTICE file
-       distributed with this work for additional information
-       regarding copyright ownership.  The ASF licenses this file
-       to you under the Apache License, Version 2.0 (the
-       "License"); you may not use this file except in compliance
-       with the License.  You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing,
-       software distributed under the License is distributed on an
-       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-       KIND, either express or implied.  See the License for the
-       specific language governing permissions and limitations
-       under the License.
-*/
-package org.apache.cordova;
-
-import org.apache.cordova.api.CallbackContext;
-import org.apache.cordova.api.CordovaPlugin;
-import org.apache.cordova.api.PluginResult;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-
-public class BinaryEcho extends CordovaPlugin {
-
-    /**
-     * Executes the request.
-     *
-     * @param action        	The action to execute.
-     * @param args          	JSONArry of arguments for the plugin.
-     * @param callbackContext 	The callback context used when calling back into JavaScript.
-     * @return              	True if the action was valid, false if not.
-     */
-    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
-        try {
-            if (action.equals("echo")) {
-                byte[] data = CordovaArguments.getArrayBuffer(args, 0);
-
-                // Don't return any result now, since status results will be sent when events come in from broadcast receiver
-                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, data);
-                callbackContext.sendPluginResult(pluginResult);
-                return true;
-            }
-        } catch (JSONException e) {
-            e.printStackTrace();
-        }
-
-        return false;
-    }
-}
-

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7530c21a/framework/src/org/apache/cordova/CordovaArgs.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaArgs.java b/framework/src/org/apache/cordova/CordovaArgs.java
new file mode 100644
index 0000000..3a8b7c0
--- /dev/null
+++ b/framework/src/org/apache/cordova/CordovaArgs.java
@@ -0,0 +1,112 @@
+/*
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License.
+*/
+package org.apache.cordova;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+
+import android.util.Base64;
+
+public class CordovaArgs {
+    private JSONArray baseArgs;
+
+    public CordovaArgs(JSONArray args) {
+        this.baseArgs = args;
+    }
+
+
+    // Pass through the basics to the base args.
+    public Object get(int index) throws JSONException {
+        return baseArgs.get(index);
+    }
+
+    public boolean getBoolean(int index) throws JSONException {
+        return baseArgs.getBoolean(index);
+    }
+
+    public double getDouble(int index) throws JSONException {
+        return baseArgs.getDouble(index);
+    }
+
+    public int getInt(int index) throws JSONException {
+        return baseArgs.getInt(index);
+    }
+
+    public JSONArray getJSONArray(int index) throws JSONException {
+        return baseArgs.getJSONArray(index);
+    }
+
+    public Object getJSONObject(int index) throws JSONException {
+        return baseArgs.getJSONObject(index);
+    }
+
+    public long getLong(int index) throws JSONException {
+        return baseArgs.getLong(index);
+    }
+
+    public String getString(int index) throws JSONException {
+        return baseArgs.getString(index);
+    }
+
+
+    public Object opt(int index) {
+        return baseArgs.opt(index);
+    }
+
+    public boolean optBoolean(int index) {
+        return baseArgs.optBoolean(index);
+    }
+
+    public double optDouble(int index) {
+        return baseArgs.optDouble(index);
+    }
+
+    public int optInt(int index) {
+        return baseArgs.optInt(index);
+    }
+
+    public JSONArray optJSONArray(int index) {
+        return baseArgs.optJSONArray(index);
+    }
+
+    public Object optJSONObject(int index) {
+        return baseArgs.optJSONObject(index);
+    }
+
+    public long optLong(int index) {
+        return baseArgs.optLong(index);
+    }
+
+    public String optString(int index) {
+        return baseArgs.optString(index);
+    }
+
+    public boolean isNull(int index) {
+        return baseArgs.isNull(index);
+    }
+
+
+    // The interesting custom helpers.
+    public byte[] getArrayBuffer(int index) throws JSONException {
+        String encoded = baseArgs.getString(index);
+        return Base64.decode(encoded, Base64.DEFAULT);
+    }
+}
+
+

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7530c21a/framework/src/org/apache/cordova/CordovaArguments.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/CordovaArguments.java b/framework/src/org/apache/cordova/CordovaArguments.java
deleted file mode 100644
index e71c39c..0000000
--- a/framework/src/org/apache/cordova/CordovaArguments.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
-       Licensed to the Apache Software Foundation (ASF) under one
-       or more contributor license agreements.  See the NOTICE file
-       distributed with this work for additional information
-       regarding copyright ownership.  The ASF licenses this file
-       to you under the Apache License, Version 2.0 (the
-       "License"); you may not use this file except in compliance
-       with the License.  You may obtain a copy of the License at
-
-         http://www.apache.org/licenses/LICENSE-2.0
-
-       Unless required by applicable law or agreed to in writing,
-       software distributed under the License is distributed on an
-       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-       KIND, either express or implied.  See the License for the
-       specific language governing permissions and limitations
-       under the License.
-*/
-package org.apache.cordova;
-
-import org.json.JSONArray;
-import org.json.JSONException;
-
-import android.util.Base64;
-
-public class CordovaArguments {
-    public static byte[] getArrayBuffer(JSONArray args, int index) throws JSONException {
-        String encoded = args.getString(index);
-        return Base64.decode(encoded, Base64.DEFAULT);
-    }
-}
-
-

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7530c21a/framework/src/org/apache/cordova/Echo.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/Echo.java b/framework/src/org/apache/cordova/Echo.java
index 5f1fed6..b84cc3f 100644
--- a/framework/src/org/apache/cordova/Echo.java
+++ b/framework/src/org/apache/cordova/Echo.java
@@ -20,24 +20,28 @@ package org.apache.cordova;
 
 import org.apache.cordova.api.CallbackContext;
 import org.apache.cordova.api.CordovaPlugin;
-import org.json.JSONArray;
 import org.json.JSONException;
 
 public class Echo extends CordovaPlugin {
 
     @Override
-    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
-        final String result = args.isNull(0) ? null : args.getString(0);
+    public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException {
         if ("echo".equals(action)) {
+            final String result = args.isNull(0) ? null : args.getString(0);
             callbackContext.success(result);
             return true;
         } else if ("echoAsync".equals(action)) {
+            final String result = args.isNull(0) ? null : args.getString(0);
             cordova.getThreadPool().execute(new Runnable() {
                 public void run() {
                     callbackContext.success(result);
                 }
             });
             return true;
+        } else if ("binaryEcho".equals(action)) {
+            final byte[] result = args.getArrayBuffer(0);
+            callbackContext.success(result);
+            return true;
         }
         return false;
     }

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7530c21a/framework/src/org/apache/cordova/api/CallbackContext.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/CallbackContext.java b/framework/src/org/apache/cordova/api/CallbackContext.java
index 86cc813..2f7b15f 100644
--- a/framework/src/org/apache/cordova/api/CallbackContext.java
+++ b/framework/src/org/apache/cordova/api/CallbackContext.java
@@ -76,6 +76,15 @@ public class CallbackContext {
      *
      * @param message           The message to add to the success result.
      */
+    public void success(byte[] message) {
+        sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
+    }
+
+    /**
+     * Helper for success callbacks that just returns the Status.OK by default
+     *
+     * @param message           The message to add to the success result.
+     */
     public void success() {
         sendPluginResult(new PluginResult(PluginResult.Status.OK));
     }

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/7530c21a/framework/src/org/apache/cordova/api/CordovaPlugin.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/api/CordovaPlugin.java b/framework/src/org/apache/cordova/api/CordovaPlugin.java
index 18f3c55..f4c785e 100644
--- a/framework/src/org/apache/cordova/api/CordovaPlugin.java
+++ b/framework/src/org/apache/cordova/api/CordovaPlugin.java
@@ -18,12 +18,12 @@
 */
 package org.apache.cordova.api;
 
+import org.apache.cordova.CordovaArgs;
 import org.apache.cordova.CordovaWebView;
 import org.json.JSONArray;
 import org.json.JSONException;
 import android.content.Intent;
 
-
 /**
  * Plugins must extend this class and override one of the execute methods.
  */
@@ -76,9 +76,28 @@ public class CordovaPlugin {
      * @return                Whether the action was valid.
      */
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
+        CordovaArgs cordovaArgs = new CordovaArgs(args);
+        return execute(action, cordovaArgs, callbackContext);
+    }
+
+    /**
+     * Executes the request.
+     *
+     * This method is called from the WebView thread. To do a non-trivial amount of work, use:
+     *     cordova.getThreadPool().execute(runnable);
+     *
+     * To run on the UI thread, use:
+     *     cordova.getActivity().runOnUiThread(runnable);
+     *
+     * @param action          The action to execute.
+     * @param args            The exec() arguments, wrapped with some Cordova helpers.
+     * @param callbackContext The callback context used when calling back into JavaScript.
+     * @return                Whether the action was valid.
+     */
+    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
         return false;
     }
-    
+
     /**
      * Called when the system is about to start resuming a previous activity.
      *