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 static Interop.VoiceControl;
20 using static Interop.VoiceControlCommand;
22 namespace Tizen.Uix.VoiceControl
25 /// This class represents a list of the voice commands.
27 /// <since_tizen> 3 </since_tizen>
28 public class VoiceCommandList
30 internal SafeCommandListHandle _handle;
31 private List<VoiceCommand> _list;
32 private VcCmdListCb _callback;
36 /// The public constructor.
38 /// <since_tizen> 3 </since_tizen>
40 /// http://tizen.org/privilege/recorder
46 /// http://tizen.org/feature/speech.control
47 /// http://tizen.org/feature/microphone
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()
55 SafeCommandListHandle handle;
56 ErrorCode error = VcCmdListCreate(out handle);
57 if (error != ErrorCode.None)
59 Log.Error(LogTag, "Create Failed with error " + error);
60 throw ExceptionFactory.CreateException(error);
63 _list = new List<VoiceCommand>();
67 internal VoiceCommandList(SafeCommandListHandle handle)
72 _list = new List<VoiceCommand>();
73 _callback = (IntPtr vcCommand, IntPtr userData) =>
75 SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
76 cmdHandle._ownership = false;
77 _list.Add(new VoiceCommand(cmdHandle));
80 ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
81 if (error != ErrorCode.None)
83 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
84 throw ExceptionFactory.CreateException(error);
89 /// Gets a command count of the list.
90 /// -1 is returned in case of an internal failure.
92 /// <since_tizen> 3 </since_tizen>
94 /// Command count of the list.
97 /// http://tizen.org/privilege/recorder
107 ErrorCode error = VcCmdListGetCount(_handle, out count);
108 if (error != ErrorCode.None)
110 Log.Error(LogTag, "Count Failed with error " + error);
119 /// Gets the current command from the command list by index.
120 /// Null will be returned in case of an empty list.
122 /// <since_tizen> 3 </since_tizen>
124 /// Current command from the command list.
127 /// http://tizen.org/privilege/recorder
132 public VoiceCommand Current
136 SafeCommandHandle current;
137 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
138 if (ErrorCode.None != error)
140 Log.Error(LogTag, "Current Failed with error " + error);
144 return _list[_index];
149 /// Adds a command to the command list.
151 /// <since_tizen> 3 </since_tizen>
153 /// http://tizen.org/privilege/recorder
159 /// http://tizen.org/feature/speech.control
160 /// http://tizen.org/feature/microphone
162 /// <param name="command">The command</param>
163 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
164 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
165 /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
166 public void Add(VoiceCommand command)
168 ErrorCode error = VcCmdListAdd(_handle, command._handle);
169 if (error != ErrorCode.None)
171 Log.Error(LogTag, "Add Failed with error " + error);
172 throw ExceptionFactory.CreateException(error);
179 /// Removes a command from the command list.
181 /// <since_tizen> 3 </since_tizen>
183 /// http://tizen.org/privilege/recorder
189 /// http://tizen.org/feature/speech.control
190 /// http://tizen.org/feature/microphone
192 /// <param name="command">The command</param>
193 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
194 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
195 /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
196 public void Remove(VoiceCommand command)
198 ErrorCode error = VcCmdListRemove(_handle, command._handle);
199 if (error != ErrorCode.None)
201 Log.Error(LogTag, "Remove Failed with error " + error);
202 throw ExceptionFactory.CreateException(error);
205 _list.Remove(command);
209 /// Retrieves all commands from the command list.
211 /// <since_tizen> 3 </since_tizen>
213 /// http://tizen.org/privilege/recorder
219 /// http://tizen.org/feature/speech.control
220 /// http://tizen.org/feature/microphone
222 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
223 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
224 public IEnumerable<VoiceCommand> GetAllCommands()
226 List<VoiceCommand> commandList = new List<VoiceCommand>();
227 _callback = (IntPtr vcCommand, IntPtr userData) =>
229 SafeCommandHandle handle = new SafeCommandHandle(vcCommand);
230 handle._ownership = false;
231 commandList.Add(new VoiceCommand(handle));
234 ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
235 if (error != ErrorCode.None)
237 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
238 throw ExceptionFactory.CreateException(error);
245 /// Moves an index to the first command.
247 /// <since_tizen> 3 </since_tizen>
249 /// http://tizen.org/privilege/recorder
255 /// http://tizen.org/feature/speech.control
256 /// http://tizen.org/feature/microphone
258 /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
259 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
260 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
263 ErrorCode error = VcCmdListFirst(_handle);
264 if (ErrorCode.None != error)
266 Log.Error(LogTag, "First Failed with error " + error);
267 throw ExceptionFactory.CreateException(error);
273 /// Moves an index to the last command.
275 /// <since_tizen> 3 </since_tizen>
277 /// http://tizen.org/privilege/recorder
283 /// http://tizen.org/feature/speech.control
284 /// http://tizen.org/feature/microphone
286 /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
287 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
288 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
291 ErrorCode error = VcCmdListLast(_handle);
292 if (ErrorCode.None != error)
294 Log.Error(LogTag, "Last Failed with error " + error);
295 throw ExceptionFactory.CreateException(error);
301 /// Moves an index to the next command.
303 /// <since_tizen> 3 </since_tizen>
305 /// http://tizen.org/privilege/recorder
311 /// http://tizen.org/feature/speech.control
312 /// http://tizen.org/feature/microphone
314 /// <exception cref="InvalidOperationException">
315 /// This exception can be due to the following reasons:
317 /// 2. List reached end
319 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
320 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
323 ErrorCode error = VcCmdListNext(_handle);
324 if (ErrorCode.None != error)
326 Log.Error(LogTag, "Next Failed with error " + error);
327 throw ExceptionFactory.CreateException(error);
333 /// Moves an index to the previous command.
335 /// <since_tizen> 3 </since_tizen>
337 /// http://tizen.org/privilege/recorder
343 /// http://tizen.org/feature/speech.control
344 /// http://tizen.org/feature/microphone
346 /// <exception cref="InvalidOperationException">
347 /// This exception can be due to the following reasons:
349 /// 2. List reached end
351 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
352 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
353 public void Previous()
355 ErrorCode error = VcCmdListPrev(_handle);
356 if (ErrorCode.None != error)
358 Log.Error(LogTag, "Previous Failed with error " + error);
359 throw ExceptionFactory.CreateException(error);