From f145605c630a6733f664ece6981e624b586259e7 Mon Sep 17 00:00:00 2001 From: Braden Shepherdson Date: Tue, 15 Jan 2013 11:16:32 -0500 Subject: [PATCH] Mostly working arraybuffer changes, needs Base64. --- .../src/org/apache/cordova/BinaryEcho.java | 67 +++++++++++++++++++ .../cordova/NativeToJsMessageQueue.java | 22 +++++- .../org/apache/cordova/api/PluginResult.java | 14 +++- 3 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 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 new file mode 100644 index 00000000..1a25f6e8 --- /dev/null +++ b/framework/src/org/apache/cordova/BinaryEcho.java @@ -0,0 +1,67 @@ +/* + 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; + +import android.util.Log; + +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")) { + //CordovaJSONArray cdvArgs = (CordovaJSONArray) args; + //byte[] data = cdvArgs.getArrayBuffer(0); + Log.i("Braden", "BinaryEcho top"); + String str = args.getString(0); + byte[] data = args.getString(0).getBytes(); + Log.i("Braden", "byte[] retrieved: " + data.length); + for (int i = 0; i < 10; i++) { + Log.i("Braden", str.substring(i, i+1) + " " + data[i]); + } + + // 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); + Log.i("Braden", "PluginResult created"); + callbackContext.sendPluginResult(pluginResult); + Log.i("Braden", "sendPluginResult() complete"); + return true; + } + } catch (JSONException e) { + e.printStackTrace(); + } + + return false; + } +} + diff --git a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java index 31466238..27de8ed3 100755 --- a/framework/src/org/apache/cordova/NativeToJsMessageQueue.java +++ b/framework/src/org/apache/cordova/NativeToJsMessageQueue.java @@ -130,9 +130,13 @@ public class NativeToJsMessageQueue { } private void packMessage(JsMessage message, StringBuilder sb) { - sb.append(message.calculateEncodedLength()) + int len = message.calculateEncodedLength(); + Log.i("Braden", "Length " + len); + sb.append(len) .append(' '); message.encodeAsMessage(sb); + Log.i("Braden", "End of packMessage"); + Log.i("Braden", sb.toString()); } /** @@ -166,7 +170,8 @@ public class NativeToJsMessageQueue { // Attach a char to indicate that there are more messages pending. sb.append('*'); } - return sb.toString(); + String ret = sb.toString(); + return ret; } } @@ -209,7 +214,8 @@ public class NativeToJsMessageQueue { for (int i = willSendAllMessages ? 1 : 0; i < numMessagesToSend; ++i) { sb.append('}'); } - return sb.toString(); + String ret = sb.toString(); + return ret; } } @@ -406,6 +412,9 @@ public class NativeToJsMessageQueue { case PluginResult.MESSAGE_TYPE_STRING: // s ret += 1 + pluginResult.getStrMessage().length(); break; + case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: + ret += 1 + pluginResult.getMessage().length(); + break; case PluginResult.MESSAGE_TYPE_JSON: default: ret += pluginResult.getMessage().length(); @@ -445,6 +454,13 @@ public class NativeToJsMessageQueue { sb.append('s'); sb.append(pluginResult.getStrMessage()); break; + case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: + Log.i("Braden", "ArrayBuffer response encoding"); + sb.append('A'); + String temp = pluginResult.getMessage(); + Log.i("Braden", temp); + sb.append(temp); + break; case PluginResult.MESSAGE_TYPE_JSON: default: sb.append(pluginResult.getMessage()); // [ or { diff --git a/framework/src/org/apache/cordova/api/PluginResult.java b/framework/src/org/apache/cordova/api/PluginResult.java index 0058f375..610da972 100755 --- a/framework/src/org/apache/cordova/api/PluginResult.java +++ b/framework/src/org/apache/cordova/api/PluginResult.java @@ -21,6 +21,8 @@ package org.apache.cordova.api; import org.json.JSONArray; import org.json.JSONObject; +import android.util.Log; + public class PluginResult { private final int status; private final int messageType; @@ -68,6 +70,13 @@ public class PluginResult { this.encodedMessage = Boolean.toString(b); } + public PluginResult(Status status, byte[] data) { + this.status = status.ordinal(); + this.messageType = MESSAGE_TYPE_ARRAYBUFFER; + this.encodedMessage = new String(data); + Log.i("Braden", "Message.length() = " + this.encodedMessage.length()); + } + public void setKeepCallback(boolean b) { this.keepCallback = b; } @@ -79,7 +88,7 @@ public class PluginResult { public int getMessageType() { return messageType; } - + public String getMessage() { if (encodedMessage == null) { encodedMessage = JSONObject.quote(strMessage); @@ -134,7 +143,8 @@ public class PluginResult { public static final int MESSAGE_TYPE_NUMBER = 3; public static final int MESSAGE_TYPE_BOOLEAN = 4; public static final int MESSAGE_TYPE_NULL = 5; - + public static final int MESSAGE_TYPE_ARRAYBUFFER = 6; + public static String[] StatusMessages = new String[] { "No result", "OK",