Skip to content

Commit

Permalink
Add partialResultsCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Skachkov committed Nov 25, 2015
1 parent 0e7c195 commit 8b2beda
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ ApiAIPlugin.setListeningStartCallback(callback)
// Set callback for listening finished callback
// callback - Function - must be simple function without arguments: function () {}
ApiAIPlugin.setListeningFinishCallback(callback)

// Set callback for getting partial recognition results (Available only on Android platform!)
// callback - Function - must be `function(str) { }` with string argument
// You can get the json array of strings with partial recognition results
ApiAIPlugin.setPartialResultsCallback(callback)
```
## Request Options
Expand Down
25 changes: 25 additions & 0 deletions src/android/ApiAiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import ai.api.model.AIRequest;
import ai.api.model.AIResponse;
import ai.api.model.Entity;
import ai.api.services.GoogleRecognitionServiceImpl;

public class ApiAiPlugin extends CordovaPlugin implements AIListener {

Expand All @@ -66,6 +67,7 @@ public class ApiAiPlugin extends CordovaPlugin implements AIListener {
private CallbackContext listeningStartCallback;
private CallbackContext listeningFinishCallback;
private CallbackContext listeningCanceledCallback;
private CallbackContext partialResultsCallback;

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

return false;
Expand Down Expand Up @@ -226,6 +231,15 @@ public void init(final JSONObject argObject, CallbackContext callbackContext) {
aiService = AIService.getService(this.cordova.getActivity().getApplicationContext(), config);
aiService.setListener(this);

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

callbackContext.success();
}
catch(Exception ex){
Expand Down Expand Up @@ -301,6 +315,10 @@ public void setListeningFinishCallback(final CallbackContext callback){
listeningFinishCallback = callback;
}

public void setPartialResultsCallback(final CallbackContext callback){
partialResultsCallback = callback;
}

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

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);
}
}
}
8 changes: 8 additions & 0 deletions www/ApiAIPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ ApiAIPluginProto.prototype = {
[]);
},

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

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

0 comments on commit 8b2beda

Please sign in to comment.