Mostly working arraybuffer changes, needs Base64.

This commit is contained in:
Braden Shepherdson 2013-01-15 11:16:32 -05:00
parent 29230d0316
commit f145605c63
3 changed files with 98 additions and 5 deletions

View File

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

View File

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

View File

@ -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",