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