RhinoPlugin.java
Go to the documentation of this file.
1 //
2 // Copyright 2021 Picovoice Inc.
3 //
4 // You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE"
5 // file accompanying this source.
6 //
7 // Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8 // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9 // specific language governing permissions and limitations under the License.
10 //
11 
12 package ai.picovoice.flutter.rhino;
13 
14 import android.content.Context;
15 
16 import androidx.annotation.NonNull;
17 
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.Map;
21 
22 import ai.picovoice.rhino.*;
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;
28 
29 public class RhinoPlugin implements FlutterPlugin, MethodCallHandler {
30 
31  private enum Method {
34  DELETE
35  }
36 
37  private Context flutterContext;
38  private MethodChannel channel;
39  private final Map<String, Rhino> rhinoPool = new HashMap<>();
40 
41  @Override
42  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
43  flutterContext = flutterPluginBinding.getApplicationContext();
44  channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "rhino");
45  channel.setMethodCallHandler(this);
46  }
47 
48  @Override
49  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
50  Method method;
51  try {
52  method = Method.valueOf(call.method.toUpperCase());
53  } catch (IllegalArgumentException e) {
54  result.error(
55  RhinoRuntimeException.class.getSimpleName(),
56  String.format("Rhino method '%s' is not a valid function", call.method),
57  null);
58  return;
59  }
60 
61  switch (method) {
62  case CREATE:
63  try {
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");
69 
70  Rhino.Builder rhinoBuilder = new Rhino.Builder()
71  .setAccessKey(accessKey)
72  .setModelPath(modelPath)
73  .setContextPath(contextPath);
74 
75  if (sensitivity != null) {
76  rhinoBuilder.setSensitivity(sensitivity.floatValue());
77  }
78 
79  if (requireEndpoint != null) {
80  rhinoBuilder.setRequireEndpoint(requireEndpoint);
81  }
82 
83  Rhino rhino = rhinoBuilder.build(flutterContext);
84  rhinoPool.put(String.valueOf(System.identityHashCode(rhino)), rhino);
85 
86  Map<String, Object> param = new HashMap<>();
87  param.put("handle", String.valueOf(System.identityHashCode(rhino)));
88  param.put("contextInfo", rhino.getContextInformation());
89  param.put("frameLength", rhino.getFrameLength());
90  param.put("sampleRate", rhino.getSampleRate());
91  param.put("version", rhino.getVersion());
92 
93  result.success(param);
94  } catch (RhinoException e) {
95  result.error(e.getClass().getSimpleName(), e.getMessage(), null);
96  } catch (Exception e) {
97  result.error(RhinoException.class.getSimpleName(), e.getMessage(), null);
98  }
99  break;
100  case PROCESS:
101  try {
102  String handle = call.argument("handle");
103  ArrayList<Integer> pcmList = call.argument("frame");
104 
105  if (!rhinoPool.containsKey(handle)) {
106  result.error(
107  RhinoInvalidStateException.class.getSimpleName(),
108  "Invalid rhino handle provided to native module",
109  null);
110  return;
111  }
112 
113  short[] pcm = null;
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();
118  }
119  }
120 
121  Rhino rhino = rhinoPool.get(handle);
122  boolean isFinalized = rhino.process(pcm);
123 
124  Map<String, Object> param = new HashMap<>();
125  param.put("isFinalized", isFinalized);
126 
127  if (isFinalized) {
128  RhinoInference inference = rhino.getInference();
129  param.put("isUnderstood", inference.getIsUnderstood());
130 
131  if (inference.getIsUnderstood()) {
132  param.put("intent", inference.getIntent());
133  param.put("slots", inference.getSlots());
134  }
135  }
136 
137  result.success(param);
138  } catch (RhinoException e) {
139  result.error(
140  e.getClass().getSimpleName(),
141  e.getMessage(),
142  null);
143  }
144  break;
145  case DELETE:
146  String handle = call.argument("handle");
147 
148  if (!rhinoPool.containsKey(handle)) {
149  result.error(
150  RhinoInvalidArgumentException.class.getSimpleName(),
151  "Invalid Rhino handle provided to native module.",
152  null);
153  return;
154  }
155 
156  Rhino rhino = rhinoPool.get(handle);
157  rhino.delete();
158  rhinoPool.remove(handle);
159 
160  result.success(null);
161  break;
162  }
163  }
164 
165  @Override
166  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
167  channel.setMethodCallHandler(null);
168  }
169 }
ai.picovoice.rhino.RhinoException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoException.java:14
ai.picovoice.rhino.Rhino.process
boolean process(short[] pcm)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:90
ai.picovoice.rhino.Rhino.Builder.setSensitivity
Builder setSensitivity(float sensitivity)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:221
call
bool call(const std::string &service_name, MReq &req, MRes &res)
ai.picovoice.flutter.rhino.RhinoPlugin.onMethodCall
void onMethodCall(@NonNull MethodCall call, @NonNull Result result)
Definition: RhinoPlugin.java:49
ai.picovoice.rhino.RhinoInference.getIntent
String getIntent()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:30
ai.picovoice.rhino.Rhino.Builder.setContextPath
Builder setContextPath(String contextPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:216
ai.picovoice.rhino.Rhino.Builder.setAccessKey
Builder setAccessKey(String accessKey)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:206
ai.picovoice.flutter.rhino.RhinoPlugin.onDetachedFromEngine
void onDetachedFromEngine(@NonNull FlutterPluginBinding binding)
Definition: RhinoPlugin.java:166
ai.picovoice.rhino.RhinoInvalidArgumentException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoInvalidArgumentException.java:13
ai.picovoice.rhino.RhinoInvalidStateException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoInvalidStateException.java:13
ai.picovoice.rhino.Rhino.getSampleRate
native int getSampleRate()
ai.picovoice.rhino.RhinoInference
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:15
ai.picovoice.flutter.rhino.RhinoPlugin.Method
Definition: RhinoPlugin.java:31
ai.picovoice.flutter.rhino.RhinoPlugin.Method.PROCESS
PROCESS
Definition: RhinoPlugin.java:33
ai
ai.picovoice.rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoActivationException.java:11
ai.picovoice.rhino.Rhino.getVersion
native String getVersion()
ai.picovoice.flutter.rhino.RhinoPlugin
Definition: RhinoPlugin.java:29
ai.picovoice.flutter.rhino.RhinoPlugin.channel
MethodChannel channel
Definition: RhinoPlugin.java:38
ai.picovoice.flutter.rhino.RhinoPlugin.onAttachedToEngine
void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding)
Definition: RhinoPlugin.java:42
ai.picovoice
ai.picovoice.rhino.Rhino
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:35
ai.picovoice.rhino.Rhino.Builder.setRequireEndpoint
Builder setRequireEndpoint(boolean requireEndpoint)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:226
ai.picovoice.rhino.Rhino.Builder.setModelPath
Builder setModelPath(String modelPath)
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:211
ai.picovoice.rhino.Rhino.Builder
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:199
ai.picovoice.rhino.Rhino.getInference
RhinoInference getInference()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:120
ai.picovoice.rhino.RhinoRuntimeException
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/exception/RhinoRuntimeException.java:13
ai.picovoice.flutter.rhino.RhinoPlugin.flutterContext
Context flutterContext
Definition: RhinoPlugin.java:37
ai.picovoice.rhino.RhinoInference.getIsUnderstood
boolean getIsUnderstood()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:26
param
T param(const std::string &param_name, const T &default_val)
ai.picovoice.rhino.Rhino.getFrameLength
native int getFrameLength()
ai.picovoice.rhino.Rhino.delete
void delete()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:71
ai.picovoice.rhino.RhinoInference.getSlots
Map< String, String > getSlots()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/RhinoInference.java:34
ai.picovoice.rhino.Rhino.getContextInformation
String getContextInformation()
Definition: android/Rhino/rhino/src/main/java/ai/picovoice/rhino/Rhino.java:157
ai.picovoice.flutter.rhino.RhinoPlugin.Method.CREATE
CREATE
Definition: RhinoPlugin.java:32
ai.picovoice.flutter.rhino.RhinoPlugin.rhinoPool
final Map< String, Rhino > rhinoPool
Definition: RhinoPlugin.java:39


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:14:50