[CB-4281] Adding echo plugin after breakout
Project: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/commit/f5c0a54d Tree: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/tree/f5c0a54d Diff: http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/diff/f5c0a54d Branch: refs/heads/master Commit: f5c0a54d056c6e9b76f59eac2b2908ddbfed3e65 Parents: 94f1514 Author: David Kemp <[email protected]> Authored: Mon Jul 22 18:04:31 2013 -0400 Committer: Max Woghiren <[email protected]> Committed: Mon Jul 22 18:09:12 2013 -0400 ---------------------------------------------------------------------- cordova-plugin-echo/README.md | 5 ++ cordova-plugin-echo/docs/echo.md | 50 +++++++++++++++++ cordova-plugin-echo/plugin.xml | 35 ++++++++++++ cordova-plugin-echo/src/android/Echo.java | 74 ++++++++++++++++++++++++++ cordova-plugin-echo/src/ios/CDVEcho.h | 23 ++++++++ cordova-plugin-echo/src/ios/CDVEcho.m | 61 +++++++++++++++++++++ cordova-plugin-echo/www/echo.js | 53 ++++++++++++++++++ dependencies-plugin/plugin.xml | 1 + 8 files changed, 302 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/README.md ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/README.md b/cordova-plugin-echo/README.md new file mode 100644 index 0000000..a4237af --- /dev/null +++ b/cordova-plugin-echo/README.md @@ -0,0 +1,5 @@ +cordova-plugin-echo +------------------------ + +This is a plugin implementation of the Echo function to test the bridge. It is currently used by Mobile Spec to test and benchmark the bridge. + http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/docs/echo.md ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/docs/echo.md b/cordova-plugin-echo/docs/echo.md new file mode 100644 index 0000000..6fe0f8f --- /dev/null +++ b/cordova-plugin-echo/docs/echo.md @@ -0,0 +1,50 @@ +--- + license: 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. +--- + +Echo +========= + +> The `echo` object provides an interface for testing the bridge. It +> sends any data received back across the bridge unmodifed. This +> allows the bridge modes to be tested and benchmarked. + +Methods +------- + +- cordova.echo.echo +- cordova.echo.echoAsync +- cordova.echo.echoArrayBuffer +- cordova.echo.echoMultiPart + +### cordova.echo.echo + +Echos string data in the same thread. + +### cordova.echo.echoAsync + +Echos string data in a new UI thread. + +### cordova.echo.echoArrayBuffer + +Echos base64 encoded data in the same thread. + +### cordova.echo.echoMultiPart + +Echos a multipart message in the same thread. + http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/plugin.xml ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/plugin.xml b/cordova-plugin-echo/plugin.xml new file mode 100644 index 0000000..816d77b --- /dev/null +++ b/cordova-plugin-echo/plugin.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0" + id="org.apache.cordova.test.echo" + version="0.1.0"> + <name>Echo</name> + + <js-module src="www/echo.js" name="echo"> + <clobbers target="cordova.echo" /> + </js-module> + + <!-- android --> + <platform name="android"> + <config-file target="res/xml/config.xml" parent="/*"> + <feature name="Echo" > + <param name="android-package" value="org.apache.cordova.test.Echo"/> + </feature> + </config-file> + + <source-file src="src/android/Echo.java" target-dir="src/org/apache/cordova/test" /> + </platform> + + <!-- ios --> + <platform name="ios"> + <config-file target="config.xml" parent="/*"> + <feature name="Echo"> + <param name="ios-package" value="CDVEcho"/> + </feature> + </config-file> + + <header-file src="src/ios/CDVEcho.h" /> + <source-file src="src/ios/CDVEcho.m" /> + </platform> + +</plugin> http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/src/android/Echo.java ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/src/android/Echo.java b/cordova-plugin-echo/src/android/Echo.java new file mode 100644 index 0000000..0bd2e31 --- /dev/null +++ b/cordova-plugin-echo/src/android/Echo.java @@ -0,0 +1,74 @@ +/* +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.test; + +import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.PluginResult; +import org.apache.cordova.PluginResult.Status; +import org.json.JSONArray; +import org.json.JSONException; + +import android.annotation.SuppressLint; +import android.util.Base64; + +/** +* This class exposes methods in Cordova that can be called from JavaScript. +*/ +public class Echo extends CordovaPlugin { + + /** + * Executes the request and returns PluginResult. + * + * @param action The action to execute. + * @param args JSONArry of arguments for the plugin. + * @param callbackContext The callback context from which we were invoked. + * @return A PluginResult object with a status and message. + */ + @SuppressLint("NewApi") + public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException { + try { + if (action.equals("echo")) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, args.getString(0))); + return true; + } else if(action.equals("echoAsync")) { + cordova.getActivity().runOnUiThread(new Runnable() { + public void run() { + callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.OK, args.optString(0))); + } + }); + return true; + } else if(action.equals("echoArrayBuffer")) { + String data = args.optString(0); + byte[] rawData= Base64.decode(data, Base64.DEFAULT); + callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.OK, rawData)); + return true; + } else if(action.equals("echoMultiPart")) { + callbackContext.sendPluginResult( new PluginResult(PluginResult.Status.OK, args.getJSONObject(0))); + return true; + } + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR)); + return false; + } catch (JSONException e) { + callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); + return false; + } + } +} http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/src/ios/CDVEcho.h ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/src/ios/CDVEcho.h b/cordova-plugin-echo/src/ios/CDVEcho.h new file mode 100644 index 0000000..785524e --- /dev/null +++ b/cordova-plugin-echo/src/ios/CDVEcho.h @@ -0,0 +1,23 @@ +/* + 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. + */ + +#import <Cordova/CDVPlugin.h> + +@interface CDVEcho : CDVPlugin +@end http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/src/ios/CDVEcho.m ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/src/ios/CDVEcho.m b/cordova-plugin-echo/src/ios/CDVEcho.m new file mode 100644 index 0000000..a5dc325 --- /dev/null +++ b/cordova-plugin-echo/src/ios/CDVEcho.m @@ -0,0 +1,61 @@ +/* + 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. + */ + +#import "CDVEcho.h" +#import <Cordova/CDV.h> + +@implementation CDVEcho + +- (void)echo:(CDVInvokedUrlCommand*)command +{ + id message = [command.arguments objectAtIndex:0]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)echoAsyncHelper:(NSArray*)args +{ + [self.commandDelegate sendPluginResult:[args objectAtIndex:0] callbackId:[args objectAtIndex:1]]; +} + +- (void)echoAsync:(CDVInvokedUrlCommand*)command +{ + id message = [command.arguments objectAtIndex:0]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; + + [self performSelector:@selector(echoAsyncHelper:) withObject:[NSArray arrayWithObjects:pluginResult, command.callbackId, nil] afterDelay:0]; +} + +- (void)echoArrayBuffer:(CDVInvokedUrlCommand*)command +{ + id message = [command.arguments objectAtIndex:0]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArrayBuffer:message]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +- (void)echoMultiPart:(CDVInvokedUrlCommand*)command +{ + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:command.arguments]; + + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; +} + +@end http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/cordova-plugin-echo/www/echo.js ---------------------------------------------------------------------- diff --git a/cordova-plugin-echo/www/echo.js b/cordova-plugin-echo/www/echo.js new file mode 100644 index 0000000..76fe3f0 --- /dev/null +++ b/cordova-plugin-echo/www/echo.js @@ -0,0 +1,53 @@ +/* + * + * 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. + * +*/ + +var exec = require('cordova/exec'), + utils = require('cordova/utils'); + +/** + * Sends the given message through exec() to the Echo plugin, which sends it back to the successCallback. + * @param successCallback invoked with a FileSystem object + * @param errorCallback invoked if error occurs retrieving file system + * @param message The string to be echoed. + * @param forceAsync Whether to force an async return value (for testing native->js bridge). + */ +module.exports = function(successCallback, errorCallback, message, forceAsync) { + var action = 'echo'; + var messageIsMultipart = (utils.typeName(message) == "Array"); + var args = messageIsMultipart ? message : [message]; + + if (utils.typeName(message) == 'ArrayBuffer') { + if (forceAsync) { + console.warn('Cannot echo ArrayBuffer with forced async, falling back to sync.'); + } + action += 'ArrayBuffer'; + } else if (messageIsMultipart) { + if (forceAsync) { + console.warn('Cannot echo MultiPart Array with forced async, falling back to sync.'); + } + action += 'MultiPart'; + } else if (forceAsync) { + action += 'Async'; + } + + exec(successCallback, errorCallback, "Echo", action, args); +}; + http://git-wip-us.apache.org/repos/asf/cordova-mobile-spec/blob/f5c0a54d/dependencies-plugin/plugin.xml ---------------------------------------------------------------------- diff --git a/dependencies-plugin/plugin.xml b/dependencies-plugin/plugin.xml index 70c15a9..7fb6ebc 100644 --- a/dependencies-plugin/plugin.xml +++ b/dependencies-plugin/plugin.xml @@ -46,4 +46,5 @@ <dependency id="org.apache.cordova.core.splashscreen" url="." subdir="../cordova-plugin-splashscreen" /> <dependency id="org.apache.cordova.core.vibration" url="." subdir="../cordova-plugin-vibration" /> <dependency id="org.apache.cordova.test.whitelist" url="." subdir="cordova-plugin-whitelist" /> + <dependency id="org.apache.cordova.test.echo" url="." subdir="cordova-plugin-echo" /> </plugin>
