12 package ai.picovoice.flutter.rhino;
14 import android.content.Context;
16 import androidx.annotation.NonNull;
18 import java.util.ArrayList;
19 import java.util.HashMap;
23 import io.flutter.embedding.engine.plugins.FlutterPlugin;
24 import io.flutter.plugin.common.MethodCall;
25 import io.flutter.plugin.common.MethodChannel;
26 import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
27 import io.flutter.plugin.common.MethodChannel.Result;
29 public class RhinoPlugin implements FlutterPlugin, MethodCallHandler {
39 private final Map<String, Rhino>
rhinoPool =
new HashMap<>();
44 channel =
new MethodChannel(flutterPluginBinding.getBinaryMessenger(),
"rhino");
45 channel.setMethodCallHandler(
this);
49 public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
52 method =
Method.valueOf(
call.method.toUpperCase());
53 }
catch (IllegalArgumentException e) {
56 String.format(
"Rhino method '%s' is not a valid function",
call.method),
64 String accessKey =
call.argument(
"accessKey");
65 String modelPath =
call.argument(
"modelPath");
66 String contextPath =
call.argument(
"contextPath");
67 Double sensitivity =
call.argument(
"sensitivity");
68 Boolean requireEndpoint =
call.argument(
"requireEndpoint");
75 if (sensitivity !=
null) {
79 if (requireEndpoint !=
null) {
84 rhinoPool.put(String.valueOf(System.identityHashCode(rhino)), rhino);
86 Map<String, Object>
param =
new HashMap<>();
87 param.put(
"handle", String.valueOf(System.identityHashCode(rhino)));
93 result.success(
param);
95 result.error(e.getClass().getSimpleName(), e.getMessage(),
null);
96 }
catch (Exception e) {
97 result.error(
RhinoException.class.getSimpleName(), e.getMessage(),
null);
102 String handle =
call.argument(
"handle");
103 ArrayList<Integer> pcmList =
call.argument(
"frame");
108 "Invalid rhino handle provided to native module",
114 if (pcmList !=
null) {
115 pcm =
new short[pcmList.size()];
116 for (
int i = 0; i < pcmList.size(); i++) {
117 pcm[i] = pcmList.get(i).shortValue();
122 boolean isFinalized = rhino.
process(pcm);
124 Map<String, Object>
param =
new HashMap<>();
125 param.put(
"isFinalized", isFinalized);
137 result.success(
param);
140 e.getClass().getSimpleName(),
146 String handle =
call.argument(
"handle");
151 "Invalid Rhino handle provided to native module.",
160 result.success(
null);
167 channel.setMethodCallHandler(
null);