Release 4.0.0-preview1-00130
[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             ErrorCode error = VcCmdListAdd(_handle, command._handle);
170             if (error != ErrorCode.None)
171             {
172                 Log.Error(LogTag, "Add Failed with error " + error);
173                 throw ExceptionFactory.CreateException(error);
174             }
175
176             _list.Add(command);
177         }
178
179         /// <summary>
180         /// Removes a command from the command list.
181         /// </summary>
182         /// <since_tizen> 3 </since_tizen>
183         /// <privilege>
184         /// http://tizen.org/privilege/recorder
185         /// </privilege>
186         /// <privlevel>
187         /// public
188         /// </privlevel>
189         /// <feature>
190         /// http://tizen.org/feature/speech.control
191         /// http://tizen.org/feature/microphone
192         /// </feature>
193         /// <param name="command">The command</param>
194         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
195         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
196         /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
197         public void Remove(VoiceCommand command)
198         {
199             ErrorCode error = VcCmdListRemove(_handle, command._handle);
200             if (error != ErrorCode.None)
201             {
202                 Log.Error(LogTag, "Remove Failed with error " + error);
203                 throw ExceptionFactory.CreateException(error);
204             }
205
206             _list.Remove(command);
207         }
208
209         /// <summary>
210         /// Retrieves all commands from the command list.
211         /// </summary>
212         /// <since_tizen> 3 </since_tizen>
213         /// <privilege>
214         /// http://tizen.org/privilege/recorder
215         /// </privilege>
216         /// <privlevel>
217         /// public
218         /// </privlevel>
219         /// <feature>
220         /// http://tizen.org/feature/speech.control
221         /// http://tizen.org/feature/microphone
222         /// </feature>
223         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
224         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
225         public IEnumerable<VoiceCommand> GetAllCommands()
226         {
227             _callback = (IntPtr vcCommand, IntPtr userData) =>
228             {
229                 if (IntPtr.Zero == vcCommand) {
230                     Log.Error(LogTag, "Invalid command pointer");
231                     return false;
232                 }
233                 return true;
234             };
235             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
236             if (error != ErrorCode.None)
237             {
238                 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
239                 throw ExceptionFactory.CreateException(error);
240             }
241
242             return _list;
243         }
244
245         /// <summary>
246         /// Moves an index to the first command.
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         /// <privilege>
250         /// http://tizen.org/privilege/recorder
251         /// </privilege>
252         /// <privlevel>
253         /// public
254         /// </privlevel>
255         /// <feature>
256         /// http://tizen.org/feature/speech.control
257         /// http://tizen.org/feature/microphone
258         /// </feature>
259         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
260         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
261         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
262         public void First()
263         {
264             ErrorCode error = VcCmdListFirst(_handle);
265             if (ErrorCode.None != error)
266             {
267                 Log.Error(LogTag, "First Failed with error " + error);
268                 throw ExceptionFactory.CreateException(error);
269             }
270             _index = 0;
271         }
272
273         /// <summary>
274         /// Moves an index to the last command.
275         /// </summary>
276         /// <since_tizen> 3 </since_tizen>
277         /// <privilege>
278         /// http://tizen.org/privilege/recorder
279         /// </privilege>
280         /// <privlevel>
281         /// public
282         /// </privlevel>
283         /// <feature>
284         /// http://tizen.org/feature/speech.control
285         /// http://tizen.org/feature/microphone
286         /// </feature>
287         /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
288         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
289         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
290         public void Last()
291         {
292             ErrorCode error = VcCmdListLast(_handle);
293             if (ErrorCode.None != error)
294             {
295                 Log.Error(LogTag, "Last Failed with error " + error);
296                 throw ExceptionFactory.CreateException(error);
297             }
298             _index = Count - 1;
299         }
300
301         /// <summary>
302         /// Moves an index to the next command.
303         /// </summary>
304         /// <since_tizen> 3 </since_tizen>
305         /// <privilege>
306         /// http://tizen.org/privilege/recorder
307         /// </privilege>
308         /// <privlevel>
309         /// public
310         /// </privlevel>
311         /// <feature>
312         /// http://tizen.org/feature/speech.control
313         /// http://tizen.org/feature/microphone
314         /// </feature>
315         /// <exception cref="InvalidOperationException">
316         /// This exception can be due to the following reasons:
317         /// 1. List empty
318         /// 2. List reached end
319         /// </exception>
320         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
321         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
322         public void Next()
323         {
324             ErrorCode error = VcCmdListNext(_handle);
325             if (ErrorCode.None != error)
326             {
327                 Log.Error(LogTag, "Next Failed with error " + error);
328                 throw ExceptionFactory.CreateException(error);
329             }
330            _index++;
331         }
332
333         /// <summary>
334         /// Moves an index to the previous command.
335         /// </summary>
336         /// <since_tizen> 3 </since_tizen>
337         /// <privilege>
338         /// http://tizen.org/privilege/recorder
339         /// </privilege>
340         /// <privlevel>
341         /// public
342         /// </privlevel>
343         /// <feature>
344         /// http://tizen.org/feature/speech.control
345         /// http://tizen.org/feature/microphone
346         /// </feature>
347         /// <exception cref="InvalidOperationException">
348         /// This exception can be due to the following reasons:
349         /// 1. List empty
350         /// 2. List reached end
351         /// </exception>
352         /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
353         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
354         public void Previous()
355         {
356             ErrorCode error = VcCmdListPrev(_handle);
357             if (ErrorCode.None != error)
358             {
359                 Log.Error(LogTag, "Previous Failed with error " + error);
360                 throw ExceptionFactory.CreateException(error);
361             }
362             _index--;
363         }
364     }
365 }