Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
302 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters