Skip to content

Commit

Permalink
Add recognitionResultsCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Skachkov committed Nov 27, 2015
1 parent ab43abd commit aa7d451
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Binary file modified libs/ailib.jar
Binary file not shown.
25 changes: 23 additions & 2 deletions src/android/ApiAiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ApiAiPlugin extends CordovaPlugin implements AIListener {
private CallbackContext listeningFinishCallback;
private CallbackContext listeningCanceledCallback;
private CallbackContext partialResultsCallback;
private CallbackContext recognitionResultsCallback;

private float maxLevel;
private float minLevel;
Expand Down Expand Up @@ -160,6 +161,9 @@ public void run() {
} else if (action.equals("partialResultsCallback")) {
setPartialResultsCallback(callbackContext);
return true;
} else if (action.equals("recognitionResultsCallback")) {
setRecognitionResultsCallback(callbackContext);
return true;
}

return false;
Expand Down Expand Up @@ -232,11 +236,16 @@ public void init(final JSONObject argObject, CallbackContext callbackContext) {
aiService.setListener(this);

if (aiService instanceof GoogleRecognitionServiceImpl) {
((GoogleRecognitionServiceImpl) aiService).setPartialResultsListener(new PartialResultsListener() {
((GoogleRecognitionServiceImpl) aiService).setRecognitionResultsListener(new RecognitionResultsListener() {
@Override
public void onPartialResults(final List<String> partialResults) {
ApiAiPlugin.this.onPartialResults(partialResults);
}

@Override
public void onRecognitionResults(final List<String> recognitionResults) {
ApiAiPlugin.this.onRecognitionResults(recognitionResults);
}
});
}

Expand Down Expand Up @@ -319,6 +328,10 @@ public void setPartialResultsCallback(final CallbackContext callback){
partialResultsCallback = callback;
}

public void setRecognitionResultsCallback(final CallbackContext callback){
recognitionResultsCallback = callback;
}

@Override
public void onReset() {
try{
Expand Down Expand Up @@ -440,11 +453,19 @@ public void onListeningCanceled() {
}
}

public void onPartialResults(final List<String> results){
public void onPartialResults(final List<String> results) {
if (partialResultsCallback != null){
final PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, gson.toJson(results));
pluginResult.setKeepCallback(true);
partialResultsCallback.sendPluginResult(pluginResult);
}
}

public void onRecognitionResults(final List<String> recognitionResults) {
if (recognitionResultsCallback != null && recognitionResults != null) {
final PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, gson.toJson(recognitionResults));
pluginResult.setKeepCallback(true);
recognitionResultsCallback.sendPluginResult(pluginResult);
}
}
}
4 changes: 2 additions & 2 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ exports.defineAutoTests = function() {
});
});

it("should recieve parameters", function (done) {
it("should receive parameters", function (done) {

ApiAIPromises.requestText(
{
Expand Down Expand Up @@ -451,7 +451,7 @@ exports.defineAutoTests = function() {
});
});

it("shoud call listeningStrarted", function (done) {
it("should call listeningStrarted", function (done) {

var called = false;
ApiAIPromises.setListeningStartCallback(function () {
Expand Down
8 changes: 8 additions & 0 deletions www/ApiAIPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ ApiAIPluginProto.prototype = {
[]);
},

setRecognitionResultsCallback: function (callback) {
cordova.exec(callback,
null,
"ApiAIPlugin",
"recognitionResultsCallback",
[]);
},

levelMeterCallback: function (callback) {
cordova.exec(callback,
null,
Expand Down

0 comments on commit aa7d451

Please sign in to comment.