[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / Tizen.Uix.VoiceControl / Tizen.Uix.VoiceControl / VoiceCommandList.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 static Interop.VoiceControl;
20 using static Interop.VoiceControlCommand;
21
22 namespace Tizen.Uix.VoiceControl
23 {
24     /// <summary>
25     /// This class represents a list of the voice commands.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class VoiceCommandList
29     {
30         internal SafeCommandListHandle _handle;
31         private List<VoiceCommand> _list;
32         private VcCmdListCb _callback;
33         private int _index;
34
35         /// <summary>
36         /// The public constructor.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <privilege>
40         /// http://tizen.org/privilege/recorder
41         /// </privilege>
42         /// <privlevel>
43         /// public
44         /// </privlevel>
45         /// <feature>
46         /// http://tizen.org/feature/speech.control
47         /// http://tizen.org/feature/microphone
48         /// </feature>
49         /// <exception cref="OutOfMemoryException">This exception can be due to out of memory.</exception>
50         /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
51         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
52         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
53         public VoiceCommandList()
54         {
55             SafeCommandListHandle handle;
56             ErrorCode error = VcCmdListCreate(out handle);
57             if (error != ErrorCode.None)
58             {
59                 Log.Error(LogTag, "Create Failed with error " + error);
60                 throw ExceptionFactory.CreateException(error);
61             }
62             _handle = handle;
63             _list = new List<VoiceCommand>();
64             _index = 0;
65         }
66
67         internal VoiceCommandList(SafeCommandListHandle handle)
68         {
69             _handle = handle;
70             _index = 0;
71
72             _list = new List<VoiceCommand>();
73             _callback = (IntPtr vcCommand, IntPtr userData) =>
74             {
75                 SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
76                 cmdHandle._ownership = false;
77                 _list.Add(new VoiceCommand(cmdHandle));
78                 return true;
79             };
80             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
81             if (error != ErrorCode.None)
82             {
83                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
84                 throw ExceptionFactory.CreateException(error);
85             }
86         }
87
88         /// <summary>
89         /// Gets a command count of the list.
90         /// -1 is returned in case of an internal failure.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <value>
94         /// Command count of the list.
95         /// </value>
96         /// <privilege>
97         /// http://tizen.org/privilege/recorder
98         /// </privilege>
99         /// <privlevel>
100         /// public
101         /// </privlevel>
102         public int Count
103         {
104             get
105             {
106                 int count;
107                 ErrorCode error = VcCmdListGetCount(_handle, out count);
108                 if (error != ErrorCode.None)
109                 {
110                     Log.Error(LogTag, "Count Failed with error " + error);
111                     return -1;
112                 }
113
114                 return count;
115             }
116         }
117
118         /// <summary>
119         /// Gets the current command from the command list by index.
120         /// Null will be returned in case of an empty list.
121         /// </summary>
122         /// <since_tizen> 3 </since_tizen>
123         /// <value>
124         /// Current command from the command list.
125         /// </value>
126         /// <privilege>
127         /// http://tizen.org/privilege/recorder
128         /// </privilege>
129         /// <privlevel>
130         /// public
131         /// </privlevel>
132         public VoiceCommand Current
133         {
134             get
135             {
136                 SafeCommandHandle current;
137                 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
138                 if (ErrorCode.None != error)
139                 {
140                     Log.Error(LogTag, "Current Failed with error " + error);
141                     return null;
142                 }
143
144                 current._ownership = false;
145                 return _list[_index];
146             }
147         }
148
149         /// <summary>
150         /// Adds a command to the command list.
151         /// </summary>
152         /// <since_tizen> 3 </since_tizen>
153         /// <privilege>
154         /// http://tizen.org/privilege/recorder
155         /// </privilege>
156         /// <privlevel>
157         /// public
158         /// </privlevel>
159         /// <feature>
160         /// http://tizen.org/feature/speech.control
161         /// http://tizen.org/feature/microphone
162         /// </feature>
163         /// <param name="command">The command</param>
164         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
165         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
166         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
167         public void Add(VoiceCommand command)
168         {
169             if (null == command) {
170                 throw new NullReferenceException("Null Parameter Provided");
171             }
172             ErrorCode error = VcCmdListAdd(_handle, command._handle);
173             if (error != ErrorCode.None)
174             {
175                 Log.Error(LogTag, "Add Failed with error " + error);
176                 throw ExceptionFactory.CreateException(error);
177             }
178
179             _list.Add(command);
180         }
181
182         /// <summary>
183         /// Removes a command from the command list.
184         /// </summary>
185         /// <since_tizen> 3 </since_tizen>
186         /// <privilege>
187         /// http://tizen.org/privilege/recorder
188         /// </privilege>
189         /// <privlevel>
190         /// public
191         /// </privlevel>
192         /// <feature>
193         /// http://tizen.org/feature/speech.control
194         /// http://tizen.org/feature/microphone
195         /// </feature>
196         /// <param name="command">The command</param>
197         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
198         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
199         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
200         public void Remove(VoiceCommand command)
201         {
202             if (null == command) {
203                 throw new NullReferenceException("Null Parameter Provided");
204             }
205             ErrorCode error = VcCmdListRemove(_handle, command._handle);
206             if (error != ErrorCode.None)
207             {
208                 Log.Error(LogTag, "Remove Failed with error " + error);
209                 throw ExceptionFactory.CreateException(error);
210             }
211
212             _list.Remove(command);
213         }
214
215         /// <summary>
216         /// Retrieves all commands from the command list.
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         /// <privilege>
220         /// http://tizen.org/privilege/recorder
221         /// </privilege>
222         /// <privlevel>
223         /// public
224         /// </privlevel>
225         /// <feature>
226         /// http://tizen.org/feature/speech.control
227         /// http://tizen.org/feature/microphone
228         /// </feature>
229         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
230         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
231         public IEnumerable<VoiceCommand> GetAllCommands()
232         {
233             _callback = (IntPtr vcCommand, IntPtr userData) =>
234             {
235                 if (IntPtr.Zero == vcCommand) {
236                     Log.Error(LogTag, "Invalid command pointer");
237                     return false;
238                 }
239                 return true;
240             };
241             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
242             if (error != ErrorCode.None)
243             {
244                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
245                 throw ExceptionFactory.CreateException(error);
246             }
247
248             return _list;
249         }
250
251         /// <summary>
252         /// Moves an index to the first command.
253         /// </summary>
254         /// <since_tizen> 3 </since_tizen>
255         /// <privilege>
256         /// http://tizen.org/privilege/recorder
257         /// </privilege>
258         /// <privlevel>
259         /// public
260         /// </privlevel>
261         /// <feature>
262         /// http://tizen.org/feature/speech.control
263         /// http://tizen.org/feature/microphone
264         /// </feature>
265         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
266         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
267         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
268         public void First()
269         {
270             ErrorCode error = VcCmdListFirst(_handle);
271             if (ErrorCode.None != error)
272             {
273                 Log.Error(LogTag, "First Failed with error " + error);
274                 throw ExceptionFactory.CreateException(error);
275             }
276             _index = 0;
277         }
278
279         /// <summary>
280         /// Moves an index to the last command.
281         /// </summary>
282         /// <since_tizen> 3 </since_tizen>
283         /// <privilege>
284         /// http://tizen.org/privilege/recorder
285         /// </privilege>
286         /// <privlevel>
287         /// public
288         /// </privlevel>
289         /// <feature>
290         /// http://tizen.org/feature/speech.control
291         /// http://tizen.org/feature/microphone
292         /// </feature>
293         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
294         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
295         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
296         public void Last()
297         {
298             ErrorCode error = VcCmdListLast(_handle);
299             if (ErrorCode.None != error)
300             {
301                 Log.Error(LogTag, "Last Failed with error " + error);
302                 throw ExceptionFactory.CreateException(error);
303             }
304             _index = Count - 1;
305         }
306
307         /// <summary>
308         /// Moves an index to the next command.
309         /// </summary>
310         /// <since_tizen> 3 </since_tizen>
311         /// <privilege>
312         /// http://tizen.org/privilege/recorder
313         /// </privilege>
314         /// <privlevel>
315         /// public
316         /// </privlevel>
317         /// <feature>
318         /// http://tizen.org/feature/speech.control
319         /// http://tizen.org/feature/microphone
320         /// </feature>
321         /// <exception cref="InvalidOperationException">
322         /// This exception can be due to the following reasons:
323         /// 1. List empty
324         /// 2. List reached end
325         /// </exception>
326         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
327         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
328         public void Next()
329         {
330             ErrorCode error = VcCmdListNext(_handle);
331             if (ErrorCode.None != error)
332             {
333                 Log.Error(LogTag, "Next Failed with error " + error);
334                 throw ExceptionFactory.CreateException(error);
335             }
336            _index++;
337         }
338
339         /// <summary>
340         /// Moves an index to the previous command.
341         /// </summary>
342         /// <since_tizen> 3 </since_tizen>
343         /// <privilege>
344         /// http://tizen.org/privilege/recorder
345         /// </privilege>
346         /// <privlevel>
347         /// public
348         /// </privlevel>
349         /// <feature>
350         /// http://tizen.org/feature/speech.control
351         /// http://tizen.org/feature/microphone
352         /// </feature>
353         /// <exception cref="InvalidOperationException">
354         /// This exception can be due to the following reasons:
355         /// 1. List empty
356         /// 2. List reached end
357         /// </exception>
358         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
359         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
360         public void Previous()
361         {
362             ErrorCode error = VcCmdListPrev(_handle);
363             if (ErrorCode.None != error)
364             {
365                 Log.Error(LogTag, "Previous Failed with error " + error);
366                 throw ExceptionFactory.CreateException(error);
367             }
368             _index--;
369         }
370     }
371 }