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 current._ownership = false;
145 return _list[_index];
150 /// Adds a command to the command list.
152 /// <since_tizen> 3 </since_tizen>
154 /// http://tizen.org/privilege/recorder
160 /// http://tizen.org/feature/speech.control
161 /// http://tizen.org/feature/microphone
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)
169 ErrorCode error = VcCmdListAdd(_handle, command._handle);
170 if (error != ErrorCode.None)
172 Log.Error(LogTag, "Add Failed with error " + error);
173 throw ExceptionFactory.CreateException(error);
180 /// Removes a command from the command list.
182 /// <since_tizen> 3 </since_tizen>
184 /// http://tizen.org/privilege/recorder
190 /// http://tizen.org/feature/speech.control
191 /// http://tizen.org/feature/microphone
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)
199 ErrorCode error = VcCmdListRemove(_handle, command._handle);
200 if (error != ErrorCode.None)
202 Log.Error(LogTag, "Remove Failed with error " + error);
203 throw ExceptionFactory.CreateException(error);
206 _list.Remove(command);
210 /// Retrieves all commands from the command list.
212 /// <since_tizen> 3 </since_tizen>
214 /// http://tizen.org/privilege/recorder
220 /// http://tizen.org/feature/speech.control
221 /// http://tizen.org/feature/microphone
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()
227 _callback = (IntPtr vcCommand, IntPtr userData) =>
229 if (IntPtr.Zero == vcCommand) {
230 Log.Error(LogTag, "Invalid command pointer");
235 ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
236 if (error != ErrorCode.None)
238 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
239 throw ExceptionFactory.CreateException(error);
246 /// Moves an index to the first command.
248 /// <since_tizen> 3 </since_tizen>
250 /// http://tizen.org/privilege/recorder
256 /// http://tizen.org/feature/speech.control
257 /// http://tizen.org/feature/microphone
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>
264 ErrorCode error = VcCmdListFirst(_handle);
265 if (ErrorCode.None != error)
267 Log.Error(LogTag, "First Failed with error " + error);
268 throw ExceptionFactory.CreateException(error);
274 /// Moves an index to the last command.
276 /// <since_tizen> 3 </since_tizen>
278 /// http://tizen.org/privilege/recorder
284 /// http://tizen.org/feature/speech.control
285 /// http://tizen.org/feature/microphone
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>
292 ErrorCode error = VcCmdListLast(_handle);
293 if (ErrorCode.None != error)
295 Log.Error(LogTag, "Last Failed with error " + error);
296 throw ExceptionFactory.CreateException(error);
302 /// Moves an index to the next command.
304 /// <since_tizen> 3 </since_tizen>
306 /// http://tizen.org/privilege/recorder
312 /// http://tizen.org/feature/speech.control
313 /// http://tizen.org/feature/microphone
315 /// <exception cref="InvalidOperationException">
316 /// This exception can be due to the following reasons:
318 /// 2. List reached end
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>
324 ErrorCode error = VcCmdListNext(_handle);
325 if (ErrorCode.None != error)
327 Log.Error(LogTag, "Next Failed with error " + error);
328 throw ExceptionFactory.CreateException(error);
334 /// Moves an index to the previous command.
336 /// <since_tizen> 3 </since_tizen>
338 /// http://tizen.org/privilege/recorder
344 /// http://tizen.org/feature/speech.control
345 /// http://tizen.org/feature/microphone
347 /// <exception cref="InvalidOperationException">
348 /// This exception can be due to the following reasons:
350 /// 2. List reached end
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()
356 ErrorCode error = VcCmdListPrev(_handle);
357 if (ErrorCode.None != error)
359 Log.Error(LogTag, "Previous Failed with error " + error);
360 throw ExceptionFactory.CreateException(error);