75d7c4c089f4d9f7bff0348de359d8ecaec39906
[platform/core/csapi/tizenfx.git] / internals / src / Tizen.Uix.VoiceControlWidget / Tizen.Uix.VoiceControlWidget / VoiceControlWidget.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using static Interop.VoiceControlWidget;
21 using static Interop.VoiceControlCommand;
22
23 namespace Tizen.Uix.VoiceControlWidget
24 {
25     /// <summary>
26     /// Enumeration for the error values that can occur.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public enum Error
30     {
31         /// <summary>
32         /// Successful, no error.
33         /// </summary>
34         None,
35         /// <summary>
36         /// Out of memory.
37         /// </summary>
38         OutOfMemory,
39         /// <summary>
40         /// I/O error.
41         /// </summary>
42         IoError,
43         /// <summary>
44         /// Invalid parameter.
45         /// </summary>
46         InvalidParameter,
47         /// <summary>
48         /// No answer from the STT service.
49         /// </summary>
50         TimedOut,
51         /// <summary>
52         /// Device or resource busy.
53         /// </summary>
54         RecorderBusy,
55         /// <summary>
56         /// Permission denied.
57         /// </summary>
58         PermissionDenied,
59         /// <summary>
60         /// VC NOT supported.
61         /// </summary>
62         NotSupported,
63         /// <summary>
64         /// Invalid state.
65         /// </summary>
66         InvalidState,
67         /// <summary>
68         /// Invalid language.
69         /// </summary>
70         InvalidLanguage,
71         /// <summary>
72         /// No available engine.
73         /// </summary>
74         EngineNotFound,
75         /// <summary>
76         /// Operation failed.
77         /// </summary>
78         OperationFailed,
79         /// <summary>
80         /// Operation rejected.
81         /// </summary>
82         OperationRejected,
83         /// <summary>
84         /// List reached end.
85         /// </summary>
86         IterationEnd,
87         /// <summary>
88         /// List empty.
89         /// </summary>
90         Empty,
91         /// <summary>
92         /// Service reset.
93         /// </summary>
94         ServiceReset,
95         /// <summary>
96         /// Progress to ready is not finished.
97         /// </summary>
98         InProgressToReady,
99         /// <summary>
100         /// Progress to recording is not finished.
101         /// </summary>
102         InProgressToRecording,
103         /// <summary>
104         /// Progress to processing is not finished.
105         /// </summary>
106         InProgressToProcessing
107     };
108
109     /// <summary>
110     /// Enumeration for the client state.
111     /// </summary>
112     /// <since_tizen> 3 </since_tizen>
113     public enum State
114     {
115         /// <summary>
116         /// 'None' state.
117         /// </summary>
118         None = 0,
119         /// <summary>
120         /// 'Initialized' state.
121         /// </summary>
122         Initialized = 1,
123         /// <summary>
124         /// 'Ready' state.
125         /// </summary>
126         Ready = 2,
127         /// <summary>
128         /// The state cannot be determined.
129         /// </summary>
130         Unavailable = -1
131     };
132
133     /// <summary>
134     /// Enumeration for the service state.
135     /// </summary>
136     /// <since_tizen> 3 </since_tizen>
137     public enum ServiceState
138     {
139         /// <summary>
140         /// 'None' state.
141         /// </summary>
142         None = 0,
143         /// <summary>
144         /// 'Ready' state.
145         /// </summary>
146         Ready = 1,
147         /// <summary>
148         /// 'Recording' state.
149         /// </summary>
150         Recording = 2,
151         /// <summary>
152         /// 'Processing' state.
153         /// </summary>
154         Processing = 3,
155         /// <summary>
156         /// The state cannot be determined.
157         /// </summary>
158         Unavailable = -1
159     };
160
161     /// <summary>
162     /// Enumeration for the result event.
163     /// </summary>
164     /// <since_tizen> 3 </since_tizen>
165     public enum ResultEvent
166     {
167         /// <summary>
168         /// Normal result.
169         /// </summary>
170         Success = 0,
171         /// <summary>
172         /// Rejected result.
173         /// </summary>
174         Rejected = 1
175     };
176
177     /// <summary>
178     /// Enumeration for the command type.
179     /// </summary>
180     /// <since_tizen> 3 </since_tizen>
181     public enum CommandType
182     {
183         /// <summary>
184         /// foreground command type
185         /// </summary>
186         Foreground = 1,
187         /// <summary>
188         /// background command type
189         /// </summary>
190         Background = 2,
191         /// <summary>
192         /// widget command type
193         /// </summary>
194         Widget = 3,
195         /// <summary>
196         /// system command type
197         /// </summary>
198         System = 4,
199         /// <summary>
200         /// system background command type
201         /// </summary>
202         SystemBackground = 5,
203         /// <summary>
204         /// exclusive command type
205         /// </summary>
206         Exclusive = 6,
207         /// <summary>
208         /// Undefined command
209         /// </summary>
210         Undefined = -1
211     };
212
213     /// <summary>
214     /// Enumerations of send event type
215     /// </summary>
216     /// <since_tizen> 3 </since_tizen>
217     public enum SendEventType
218     {
219         /// <summary>
220         /// Send text event to vc engine
221         /// </summary>
222         Text,
223         /// <summary>
224         /// Send list event to vc engine
225         /// </summary>
226         ListEvent,
227         /// <summary>
228         /// Send haptic event to vc engine
229         /// </summary>
230         HapticEvent,
231     }
232
233     /// <summary>
234     /// Enumerations of recognition mode
235     /// </summary>
236     /// <since_tizen> 3 </since_tizen>
237     public enum RecognitionModeType
238     {
239         /// <summary>
240         /// Default mode
241         /// </summary>
242         StopBySilence,
243         /// <summary>
244         /// Restart recognition after rejected result
245         /// </summary>
246         RestartAfterReject,
247         /// <summary>
248         /// Continuously restart recognition - not support yet
249         /// </summary>
250         RestartContinuously,
251         /// <summary>
252         /// Start and stop manually without silence
253         /// </summary>
254         Manual,
255         /// <summary>
256         /// Undefined
257         /// </summary>
258         Undefined = -1
259     }
260
261     public class VoiceControlWidget
262     {
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;
282
283
284         public string CurrentLanguage
285         {
286             get
287             {
288                 string currentLanguage;
289
290                 ErrorCode error = VcWidgetGetCurrentLanguage(_handle, out currentLanguage);
291                 if (error != ErrorCode.None)
292                 {
293                     Log.Error(LogTag, "CurrentLanaguge Failed with error " + error);
294                     return "";
295                 }
296
297                 return currentLanguage;
298             }
299         }
300
301         public State State
302         {
303             get
304             {
305                 State state;
306
307                 ErrorCode error = VcWidgetGetState(_handle, out state);
308                 if (error != ErrorCode.None)
309                 {
310                     Log.Error(LogTag, "State Failed with error " + error);
311                     return State.Unavailable;
312                 }
313
314                 return state;
315             }
316         }
317
318         public ServiceState ServiceState
319         {
320             get
321             {
322                 ServiceState state;
323
324                 ErrorCode error = VcWidgetGetServiceState(_handle, out state);
325                 if (error != ErrorCode.None)
326                 {
327                     Log.Error(LogTag, "ServiceState Failed with error " + error);
328                     return ServiceState.Unavailable;
329                 }
330
331                 return state;
332             }
333         }
334
335         public bool IsCommandFormatSupported(CommandFormat format)
336         {
337             bool supported = false;
338             ErrorCode error = VcWidgetIsCommandFormatSupported(_handle, format, out supported);
339             if (error != ErrorCode.None)
340             {
341                 Log.Error(LogTag, "IsCommandFormatSupported Failed with error " + error);
342                 throw ExceptionFactory.CreateException(error);
343             }
344             return supported;
345         }
346
347         public void SetForeground(bool value)
348         {
349             ErrorCode error = VcWidgetSetForeground(_handle, value);
350             if (error != ErrorCode.None)
351             {
352                 Log.Error(LogTag, "SetForeground Failed with error " + error);
353                 throw ExceptionFactory.CreateException(error);
354             }
355         }
356
357         public void Cancel()
358         {
359             ErrorCode error = VcWidgetCancel(_handle);
360             if (error != ErrorCode.None)
361             {
362                 Log.Error(LogTag, "Cancel Failed with error " + error);
363                 throw ExceptionFactory.CreateException(error);
364             }
365         }
366
367         public void Initialize()
368         {
369             ErrorCode error = VcWidgetInitialize(out _handle);
370             if (error != ErrorCode.None)
371             {
372                 Log.Error(LogTag, "Initialize Failed with error " + error);
373                 throw ExceptionFactory.CreateException(error);
374             }
375         }
376
377         public void Deinitialize()
378         {
379             ErrorCode error = VcWidgetDeinitialize(_handle);
380             if (error != ErrorCode.None)
381             {
382                 Log.Error(LogTag, "Deinitialize Failed with error " + error);
383                 throw ExceptionFactory.CreateException(error);
384             }
385         }
386
387         public void Prepare()
388         {
389             ErrorCode error = VcWidgetPrepare(_handle);
390             if (error != ErrorCode.None)
391             {
392                 Log.Error(LogTag, "Prepare Failed with error " + error);
393                 throw ExceptionFactory.CreateException(error);
394             }
395         }
396
397         public void Unprepare()
398         {
399             ErrorCode error = VcWidgetUnprepare(_handle);
400             if (error != ErrorCode.None)
401             {
402                 Log.Error(LogTag, "Unprepare Failed with error " + error);
403                 throw ExceptionFactory.CreateException(error);
404             }
405         }
406
407         public void EnableAsr(bool enable)
408         {
409             ErrorCode error = VcWidgetEnableAsrResult(_handle, enable);
410             if (error != ErrorCode.None)
411             {
412                 Log.Error(LogTag, "EnableAsr Failed with error " + error);
413                 throw ExceptionFactory.CreateException(error);
414             }
415         }
416
417         public IEnumerable<string> GetSupportedLanguages()
418         {
419             _supportedLanguages = new List<string>();
420             _supportedLanguagesDelegate = (IntPtr language, IntPtr userData) =>
421             {
422                 string languageStr = Marshal.PtrToStringAnsi(language);
423                 _supportedLanguages.Add(languageStr);
424                 return true;
425             };
426             ErrorCode error = VcWidgetForeachSupportedLanguages(_handle, _supportedLanguagesDelegate, IntPtr.Zero);
427             if (error != ErrorCode.None)
428             {
429                 Log.Error(LogTag, "GetSupportedLanguages Failed with error " + error);
430                 throw ExceptionFactory.CreateException(error);
431             }
432
433             return _supportedLanguages;
434         }
435
436         public event EventHandler<ServiceStateChangedEventArgs> ServiceStateChanged
437         {
438             add
439             {
440                 _serviceStateDelegate = (ServiceState previous, ServiceState current, IntPtr userData) =>
441                 {
442                     ServiceStateChangedEventArgs args = new ServiceStateChangedEventArgs(previous, current);
443                     _serviceStateChanged?.Invoke(null, args);
444                 };
445                 ErrorCode error = VcWidgetSetServiceStateChangedCb(_handle, _serviceStateDelegate, IntPtr.Zero);
446                 if (error != ErrorCode.None)
447                 {
448                     Log.Error(LogTag, "Add ServiceStateChanged Failed with error " + error);
449                 }
450                 else
451                 {
452                     _serviceStateChanged += value;
453                 }
454             }
455
456             remove
457             {
458                 ErrorCode error = VcWidgetUnsetServiceStateChangedCb(_handle);
459                 if (error != ErrorCode.None)
460                 {
461                     Log.Error(LogTag, "Remove ServiceStateChanged Failed with error " + error);
462                 }
463
464                 _serviceStateChanged -= value;
465             }
466         }
467
468         public event EventHandler<StateChangedEventArgs> StateChanged
469         {
470             add
471             {
472                 _stateDelegate = (State previous, State current, IntPtr userData) =>
473                 {
474                     StateChangedEventArgs args = new StateChangedEventArgs(previous, current);
475                     _stateChanged?.Invoke(null, args);
476                 };
477                 ErrorCode error = VcWidgetSetStateChangedCb(_handle, _stateDelegate, IntPtr.Zero);
478                 if (error != ErrorCode.None)
479                 {
480                     Log.Error(LogTag, "Add StateChanged Failed with error " + error);
481                 }
482                 else
483                 {
484                     _stateChanged += value;
485                 }
486             }
487
488             remove
489             {
490                 ErrorCode error = VcWidgetUnsetStateChangedCb(_handle);
491                 if (error != ErrorCode.None)
492                 {
493                     Log.Error(LogTag, "Remove StateChanged Failed with error " + error);
494                 }
495
496                 _stateChanged -= value;
497             }
498         }
499
500         public event EventHandler<ErrorOccurredEventArgs> ErrorOccurred
501         {
502             add
503             {
504                 _errorDelegate = (ErrorCode reason, IntPtr userData) =>
505             {
506                 ErrorOccurredEventArgs args = new ErrorOccurredEventArgs(reason);
507                 _errorOccurred?.Invoke(null, args);
508             };
509                 ErrorCode error = VcWidgetSetErrorCb(_handle, _errorDelegate, IntPtr.Zero);
510                 if (error != ErrorCode.None)
511                 {
512                     Log.Error(LogTag, "Add ErrorOccurred Failed with error " + error);
513                 }
514
515                 else
516                 {
517                     _errorOccurred += value;
518                 }
519             }
520
521
522             remove
523             {
524                 ErrorCode error = VcWidgetUnsetErrorCb(_handle);
525                 if (error != ErrorCode.None)
526                 {
527                     Log.Error(LogTag, "Remove ErrorOccurred Failed with error " + error);
528                 }
529
530                 _errorOccurred -= value;
531             }
532         }
533
534         public event EventHandler<CurrentLanguageChangedEventArgs> CurrentLanguageChanged
535         {
536             add
537             {
538                 _languageDelegate = (IntPtr previousLanguage, IntPtr currentLanguage, IntPtr userData) =>
539             {
540                 string previousLanguageString = Marshal.PtrToStringAnsi(previousLanguage);
541                 string currentLanguageString = Marshal.PtrToStringAnsi(currentLanguage);
542                 CurrentLanguageChangedEventArgs args = new CurrentLanguageChangedEventArgs(previousLanguageString, currentLanguageString);
543                 _currentLanguageChanged?.Invoke(null, args);
544             };
545                 ErrorCode error = VcWidgetSetCurrentLanguageChangedCb(_handle, _languageDelegate, IntPtr.Zero);
546                 if (error != ErrorCode.None)
547                 {
548                     Log.Error(LogTag, "Add CurrentLanguageChanged Failed with error " + error);
549                 }
550
551                 else
552                 {
553                     _currentLanguageChanged += value;
554                 }
555             }
556
557             remove
558             {
559                 ErrorCode error = VcWidgetUnsetCurrentLanguageChangedCb(_handle);
560                 if (error != ErrorCode.None)
561                 {
562                     Log.Error(LogTag, "Remove CurrentLanguageChanged Failed with error " + error);
563                 }
564
565                 _currentLanguageChanged -= value;
566             }
567         }
568
569         public event EventHandler<CurrentCommandListEventArgs> CurrentCommandList
570         {
571             add
572             {
573                 _currentCommandListDelegate = (out IntPtr listPtr, IntPtr userData) =>
574                 {
575                     CurrentCommandListEventArgs args = new CurrentCommandListEventArgs();
576                     _currentCommandList?.Invoke(null, args);
577
578                     listPtr = args.CommandList._handle.DangerousGetHandle();
579                     Log.Debug(LogTag, "Send current Command list");
580                 };
581                 ErrorCode error = VcWidgetSetSendCurrentCommandListCb(_handle, _currentCommandListDelegate, IntPtr.Zero);
582                 if (error != ErrorCode.None)
583                 {
584                     Log.Error(LogTag, "Add CurrentCommandList Failed with error " + error);
585                 }
586
587                 else
588                 {
589                     _currentCommandList += value;
590                 }
591             }
592
593             remove
594             {
595                 ErrorCode error = VcWidgetUnSetSendCurrentCommandListCb(_handle);
596                 if (error != ErrorCode.None)
597                 {
598                     Log.Error(LogTag, "Remove CurrentCommandList Failed with error " + error);
599                 }
600
601                 _currentCommandList -= value;
602             }
603         }
604
605         public event EventHandler<AsrResultEventArgs> AsrResult
606         {
607             add
608             {
609                 _asrResultDelegate = (ResultEvent evt, IntPtr result, IntPtr userData) =>
610                 {
611                     string resultString = Marshal.PtrToStringAnsi(result);
612                     AsrResultEventArgs args = new AsrResultEventArgs(evt, resultString);
613                     _asrResult?.Invoke(null, args);
614                     return true;
615                 };
616                 ErrorCode error = VcWidgetSetAsrResultCb(_handle, _asrResultDelegate, IntPtr.Zero);
617                 if (error != ErrorCode.None)
618                 {
619                     Log.Error(LogTag, "Add AsrResult Failed with error " + error);
620                 }
621
622                 else
623                 {
624                     _asrResult += value;
625                 }
626             }
627
628             remove
629             {
630                 ErrorCode error = VcWidgetUnSetAsrResultCb(_handle);
631                 if (error != ErrorCode.None)
632                 {
633                     Log.Error(LogTag, "Remove AsrResult Failed with error " + error);
634                 }
635
636                 _asrResult -= value;
637             }
638         }
639
640         public event EventHandler<TooltipEventArgs> ShowTooltip
641         {
642             add
643             {
644                 _showTooltiptDelegate = (bool show, IntPtr userData) =>
645                 {
646                     TooltipEventArgs args = new TooltipEventArgs(show);
647                     _tooltip?.Invoke(null, args);
648                 };
649                 ErrorCode error = VcWidgetSetShowTooltipCb(_handle, _showTooltiptDelegate, IntPtr.Zero);
650                 if (error != ErrorCode.None)
651                 {
652                     Log.Error(LogTag, "Add ShowTooltip Failed with error " + error);
653                 }
654
655                 else
656                 {
657                     _tooltip += value;
658                 }
659             }
660
661             remove
662             {
663                 ErrorCode error = VcWidgetUnSetShowTooltipCb(_handle);
664                 if (error != ErrorCode.None)
665                 {
666                     Log.Error(LogTag, "Remove ShowTooltip Failed with error " + error);
667                 }
668
669                 _tooltip -= value;
670             }
671         }
672
673         public event EventHandler<RecognitionResult> RecognitionResult
674         {
675             add
676             {
677                 _resultDelegate = (ResultEvent evt, IntPtr cmdList, IntPtr result, IntPtr userData) =>
678                 {
679                     Log.Info(LogTag, "Recognition Result Event Triggered");
680                     if ((cmdList != null) && (result != null))
681                     {
682                         RecognitionResult args = new RecognitionResult(evt, cmdList, result);
683                         _recognitionResult?.Invoke(null, args);
684                     }
685                     else
686                     {
687                         Log.Info(LogTag, "Recognition Result Event null received");
688                     }
689                 };
690                 ErrorCode error = VcWidgetSetResultCb(_handle, _resultDelegate, IntPtr.Zero);
691                 if (error != ErrorCode.None)
692                 {
693                     Log.Error(LogTag, "Add RecognitionResult Failed with error " + error);
694                 }
695                 else
696                 {
697                     _recognitionResult += value;
698                 }
699             }
700
701             remove
702             {
703                 ErrorCode error = VcWidgetUnsetResultCb(_handle);
704                 if (error != ErrorCode.None)
705                 {
706                     Log.Error(LogTag, "Remove RecognitionResult Failed with error " + error);
707                 }
708
709                 _recognitionResult -= value;
710             }
711         }
712     }
713 }