2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using static Interop.VoiceControlWidget;
21 using static Interop.VoiceControlCommand;
23 namespace Tizen.Uix.VoiceControlWidget
26 /// Enumeration for the error values that can occur.
28 /// <since_tizen> 3 </since_tizen>
32 /// Successful, no error.
44 /// Invalid parameter.
48 /// No answer from the STT service.
52 /// Device or resource busy.
56 /// Permission denied.
72 /// No available engine.
80 /// Operation rejected.
96 /// Progress to ready is not finished.
100 /// Progress to recording is not finished.
102 InProgressToRecording,
104 /// Progress to processing is not finished.
106 InProgressToProcessing
110 /// Enumeration for the client state.
112 /// <since_tizen> 3 </since_tizen>
120 /// 'Initialized' state.
128 /// The state cannot be determined.
134 /// Enumeration for the service state.
136 /// <since_tizen> 3 </since_tizen>
137 public enum ServiceState
148 /// 'Recording' state.
152 /// 'Processing' state.
156 /// The state cannot be determined.
162 /// Enumeration for the result event.
164 /// <since_tizen> 3 </since_tizen>
165 public enum ResultEvent
178 /// Enumeration for the command type.
180 /// <since_tizen> 3 </since_tizen>
181 public enum CommandType
184 /// foreground command type
188 /// background command type
192 /// widget command type
196 /// system command type
200 /// system background command type
202 SystemBackground = 5,
204 /// exclusive command type
208 /// Undefined command
214 /// Enumerations of send event type
216 /// <since_tizen> 3 </since_tizen>
217 public enum SendEventType
220 /// Send text event to vc engine
224 /// Send list event to vc engine
228 /// Send haptic event to vc engine
234 /// Enumerations of recognition mode
236 /// <since_tizen> 3 </since_tizen>
237 public enum RecognitionModeType
244 /// Restart recognition after rejected result
248 /// Continuously restart recognition - not support yet
252 /// Start and stop manually without silence
261 public class VoiceControlWidget
263 private event EventHandler<StateChangedEventArgs> _stateChanged;
264 private event EventHandler<ServiceStateChangedEventArgs> _serviceStateChanged;
265 private event EventHandler<ErrorOccurredEventArgs> _errorOccurred;
266 private event EventHandler<CurrentLanguageChangedEventArgs> _currentLanguageChanged;
267 private event EventHandler<CurrentCommandListEventArgs> _currentCommandList;
268 private event EventHandler<AsrResultEventArgs> _asrResult;
269 private event EventHandler<TooltipEventArgs> _tooltip;
270 private event EventHandler<RecognitionResult> _recognitionResult;
271 private VcWidgetStateChangedCallback _stateDelegate;
272 private VcWidgetServiceStateChangedCallback _serviceStateDelegate;
273 private VcWidgetErrorCallback _errorDelegate;
274 private VcWidgetCurrentLanguageChangedCallback _languageDelegate;
275 private VcWidgetSendCurrentCommandListCb _currentCommandListDelegate;
276 private VcWidgetAsrResultCallback _asrResultDelegate;
277 private VcWidgetShowTooltipCb _showTooltiptDelegate;
278 private List<string> _supportedLanguages;
279 private IntPtr _handle;
280 private VcWidgetResultCallback _resultDelegate;
281 private VcWidgetSupportedLanguageCb _supportedLanguagesDelegate;
284 public string CurrentLanguage
288 string currentLanguage;
290 ErrorCode error = VcWidgetGetCurrentLanguage(_handle, out currentLanguage);
291 if (error != ErrorCode.None)
293 Log.Error(LogTag, "CurrentLanaguge Failed with error " + error);
297 return currentLanguage;
307 ErrorCode error = VcWidgetGetState(_handle, out state);
308 if (error != ErrorCode.None)
310 Log.Error(LogTag, "State Failed with error " + error);
311 return State.Unavailable;
318 public ServiceState ServiceState
324 ErrorCode error = VcWidgetGetServiceState(_handle, out state);
325 if (error != ErrorCode.None)
327 Log.Error(LogTag, "ServiceState Failed with error " + error);
328 return ServiceState.Unavailable;
335 public bool IsCommandFormatSupported(CommandFormat format)
337 bool supported = false;
338 ErrorCode error = VcWidgetIsCommandFormatSupported(_handle, format, out supported);
339 if (error != ErrorCode.None)
341 Log.Error(LogTag, "IsCommandFormatSupported Failed with error " + error);
342 throw ExceptionFactory.CreateException(error);
347 public void SetForeground(bool value)
349 ErrorCode error = VcWidgetSetForeground(_handle, value);
350 if (error != ErrorCode.None)
352 Log.Error(LogTag, "SetForeground Failed with error " + error);
353 throw ExceptionFactory.CreateException(error);
359 ErrorCode error = VcWidgetCancel(_handle);
360 if (error != ErrorCode.None)
362 Log.Error(LogTag, "Cancel Failed with error " + error);
363 throw ExceptionFactory.CreateException(error);
367 public void Initialize()
369 ErrorCode error = VcWidgetInitialize(out _handle);
370 if (error != ErrorCode.None)
372 Log.Error(LogTag, "Initialize Failed with error " + error);
373 throw ExceptionFactory.CreateException(error);
377 public void Deinitialize()
379 ErrorCode error = VcWidgetDeinitialize(_handle);
380 if (error != ErrorCode.None)
382 Log.Error(LogTag, "Deinitialize Failed with error " + error);
383 throw ExceptionFactory.CreateException(error);
387 public void Prepare()
389 ErrorCode error = VcWidgetPrepare(_handle);
390 if (error != ErrorCode.None)
392 Log.Error(LogTag, "Prepare Failed with error " + error);
393 throw ExceptionFactory.CreateException(error);
397 public void Unprepare()
399 ErrorCode error = VcWidgetUnprepare(_handle);
400 if (error != ErrorCode.None)
402 Log.Error(LogTag, "Unprepare Failed with error " + error);
403 throw ExceptionFactory.CreateException(error);
407 public void EnableAsr(bool enable)
409 ErrorCode error = VcWidgetEnableAsrResult(_handle, enable);
410 if (error != ErrorCode.None)
412 Log.Error(LogTag, "EnableAsr Failed with error " + error);
413 throw ExceptionFactory.CreateException(error);
417 public IEnumerable<string> GetSupportedLanguages()
419 _supportedLanguages = new List<string>();
420 _supportedLanguagesDelegate = (IntPtr language, IntPtr userData) =>
422 string languageStr = Marshal.PtrToStringAnsi(language);
423 _supportedLanguages.Add(languageStr);
426 ErrorCode error = VcWidgetForeachSupportedLanguages(_handle, _supportedLanguagesDelegate, IntPtr.Zero);
427 if (error != ErrorCode.None)
429 Log.Error(LogTag, "GetSupportedLanguages Failed with error " + error);
430 throw ExceptionFactory.CreateException(error);
433 return _supportedLanguages;
436 public event EventHandler<ServiceStateChangedEventArgs> ServiceStateChanged
440 _serviceStateDelegate = (ServiceState previous, ServiceState current, IntPtr userData) =>
442 ServiceStateChangedEventArgs args = new ServiceStateChangedEventArgs(previous, current);
443 _serviceStateChanged?.Invoke(null, args);
445 ErrorCode error = VcWidgetSetServiceStateChangedCb(_handle, _serviceStateDelegate, IntPtr.Zero);
446 if (error != ErrorCode.None)
448 Log.Error(LogTag, "Add ServiceStateChanged Failed with error " + error);
452 _serviceStateChanged += value;
458 ErrorCode error = VcWidgetUnsetServiceStateChangedCb(_handle);
459 if (error != ErrorCode.None)
461 Log.Error(LogTag, "Remove ServiceStateChanged Failed with error " + error);
464 _serviceStateChanged -= value;
468 public event EventHandler<StateChangedEventArgs> StateChanged
472 _stateDelegate = (State previous, State current, IntPtr userData) =>
474 StateChangedEventArgs args = new StateChangedEventArgs(previous, current);
475 _stateChanged?.Invoke(null, args);
477 ErrorCode error = VcWidgetSetStateChangedCb(_handle, _stateDelegate, IntPtr.Zero);
478 if (error != ErrorCode.None)
480 Log.Error(LogTag, "Add StateChanged Failed with error " + error);
484 _stateChanged += value;
490 ErrorCode error = VcWidgetUnsetStateChangedCb(_handle);
491 if (error != ErrorCode.None)
493 Log.Error(LogTag, "Remove StateChanged Failed with error " + error);
496 _stateChanged -= value;
500 public event EventHandler<ErrorOccurredEventArgs> ErrorOccurred
504 _errorDelegate = (ErrorCode reason, IntPtr userData) =>
506 ErrorOccurredEventArgs args = new ErrorOccurredEventArgs(reason);
507 _errorOccurred?.Invoke(null, args);
509 ErrorCode error = VcWidgetSetErrorCb(_handle, _errorDelegate, IntPtr.Zero);
510 if (error != ErrorCode.None)
512 Log.Error(LogTag, "Add ErrorOccurred Failed with error " + error);
517 _errorOccurred += value;
524 ErrorCode error = VcWidgetUnsetErrorCb(_handle);
525 if (error != ErrorCode.None)
527 Log.Error(LogTag, "Remove ErrorOccurred Failed with error " + error);
530 _errorOccurred -= value;
534 public event EventHandler<CurrentLanguageChangedEventArgs> CurrentLanguageChanged
538 _languageDelegate = (IntPtr previousLanguage, IntPtr currentLanguage, IntPtr userData) =>
540 string previousLanguageString = Marshal.PtrToStringAnsi(previousLanguage);
541 string currentLanguageString = Marshal.PtrToStringAnsi(currentLanguage);
542 CurrentLanguageChangedEventArgs args = new CurrentLanguageChangedEventArgs(previousLanguageString, currentLanguageString);
543 _currentLanguageChanged?.Invoke(null, args);
545 ErrorCode error = VcWidgetSetCurrentLanguageChangedCb(_handle, _languageDelegate, IntPtr.Zero);
546 if (error != ErrorCode.None)
548 Log.Error(LogTag, "Add CurrentLanguageChanged Failed with error " + error);
553 _currentLanguageChanged += value;
559 ErrorCode error = VcWidgetUnsetCurrentLanguageChangedCb(_handle);
560 if (error != ErrorCode.None)
562 Log.Error(LogTag, "Remove CurrentLanguageChanged Failed with error " + error);
565 _currentLanguageChanged -= value;
569 public event EventHandler<CurrentCommandListEventArgs> CurrentCommandList
573 _currentCommandListDelegate = (out IntPtr listPtr, IntPtr userData) =>
575 CurrentCommandListEventArgs args = new CurrentCommandListEventArgs();
576 _currentCommandList?.Invoke(null, args);
578 listPtr = args.CommandList._handle.DangerousGetHandle();
579 Log.Debug(LogTag, "Send current Command list");
581 ErrorCode error = VcWidgetSetSendCurrentCommandListCb(_handle, _currentCommandListDelegate, IntPtr.Zero);
582 if (error != ErrorCode.None)
584 Log.Error(LogTag, "Add CurrentCommandList Failed with error " + error);
589 _currentCommandList += value;
595 ErrorCode error = VcWidgetUnSetSendCurrentCommandListCb(_handle);
596 if (error != ErrorCode.None)
598 Log.Error(LogTag, "Remove CurrentCommandList Failed with error " + error);
601 _currentCommandList -= value;
605 public event EventHandler<AsrResultEventArgs> AsrResult
609 _asrResultDelegate = (ResultEvent evt, IntPtr result, IntPtr userData) =>
611 string resultString = Marshal.PtrToStringAnsi(result);
612 AsrResultEventArgs args = new AsrResultEventArgs(evt, resultString);
613 _asrResult?.Invoke(null, args);
616 ErrorCode error = VcWidgetSetAsrResultCb(_handle, _asrResultDelegate, IntPtr.Zero);
617 if (error != ErrorCode.None)
619 Log.Error(LogTag, "Add AsrResult Failed with error " + error);
630 ErrorCode error = VcWidgetUnSetAsrResultCb(_handle);
631 if (error != ErrorCode.None)
633 Log.Error(LogTag, "Remove AsrResult Failed with error " + error);
640 public event EventHandler<TooltipEventArgs> ShowTooltip
644 _showTooltiptDelegate = (bool show, IntPtr userData) =>
646 TooltipEventArgs args = new TooltipEventArgs(show);
647 _tooltip?.Invoke(null, args);
649 ErrorCode error = VcWidgetSetShowTooltipCb(_handle, _showTooltiptDelegate, IntPtr.Zero);
650 if (error != ErrorCode.None)
652 Log.Error(LogTag, "Add ShowTooltip Failed with error " + error);
663 ErrorCode error = VcWidgetUnSetShowTooltipCb(_handle);
664 if (error != ErrorCode.None)
666 Log.Error(LogTag, "Remove ShowTooltip Failed with error " + error);
673 public event EventHandler<RecognitionResult> RecognitionResult
677 _resultDelegate = (ResultEvent evt, IntPtr cmdList, IntPtr result, IntPtr userData) =>
679 Log.Info(LogTag, "Recognition Result Event Triggered");
680 if ((cmdList != null) && (result != null))
682 RecognitionResult args = new RecognitionResult(evt, cmdList, result);
683 _recognitionResult?.Invoke(null, args);
687 Log.Info(LogTag, "Recognition Result Event null received");
690 ErrorCode error = VcWidgetSetResultCb(_handle, _resultDelegate, IntPtr.Zero);
691 if (error != ErrorCode.None)
693 Log.Error(LogTag, "Add RecognitionResult Failed with error " + error);
697 _recognitionResult += value;
703 ErrorCode error = VcWidgetUnsetResultCb(_handle);
704 if (error != ErrorCode.None)
706 Log.Error(LogTag, "Remove RecognitionResult Failed with error " + error);
709 _recognitionResult -= value;