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 if (null == command) {
170 throw new NullReferenceException("Null Parameter Provided");
172 ErrorCode error = VcCmdListAdd(_handle, command._handle);
173 if (error != ErrorCode.None)
175 Log.Error(LogTag, "Add Failed with error " + error);
176 throw ExceptionFactory.CreateException(error);
183 /// Removes a command from the command list.
185 /// <since_tizen> 3 </since_tizen>
187 /// http://tizen.org/privilege/recorder
193 /// http://tizen.org/feature/speech.control
194 /// http://tizen.org/feature/microphone
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)
202 if (null == command) {
203 throw new NullReferenceException("Null Parameter Provided");
205 ErrorCode error = VcCmdListRemove(_handle, command._handle);
206 if (error != ErrorCode.None)
208 Log.Error(LogTag, "Remove Failed with error " + error);
209 throw ExceptionFactory.CreateException(error);
212 _list.Remove(command);
216 /// Retrieves all commands from the command list.
218 /// <since_tizen> 3 </since_tizen>
220 /// http://tizen.org/privilege/recorder
226 /// http://tizen.org/feature/speech.control
227 /// http://tizen.org/feature/microphone
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()
233 _callback = (IntPtr vcCommand, IntPtr userData) =>
235 if (IntPtr.Zero == vcCommand) {
236 Log.Error(LogTag, "Invalid command pointer");
241 ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
242 if (error != ErrorCode.None)
244 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
245 throw ExceptionFactory.CreateException(error);
252 /// Moves an index to the first command.
254 /// <since_tizen> 3 </since_tizen>
256 /// http://tizen.org/privilege/recorder
262 /// http://tizen.org/feature/speech.control
263 /// http://tizen.org/feature/microphone
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>
270 ErrorCode error = VcCmdListFirst(_handle);
271 if (ErrorCode.None != error)
273 Log.Error(LogTag, "First Failed with error " + error);
274 throw ExceptionFactory.CreateException(error);
280 /// Moves an index to the last command.
282 /// <since_tizen> 3 </since_tizen>
284 /// http://tizen.org/privilege/recorder
290 /// http://tizen.org/feature/speech.control
291 /// http://tizen.org/feature/microphone
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>
298 ErrorCode error = VcCmdListLast(_handle);
299 if (ErrorCode.None != error)
301 Log.Error(LogTag, "Last Failed with error " + error);
302 throw ExceptionFactory.CreateException(error);
308 /// Moves an index to the next command.
310 /// <since_tizen> 3 </since_tizen>
312 /// http://tizen.org/privilege/recorder
318 /// http://tizen.org/feature/speech.control
319 /// http://tizen.org/feature/microphone
321 /// <exception cref="InvalidOperationException">
322 /// This exception can be due to the following reasons:
324 /// 2. List reached end
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>
330 ErrorCode error = VcCmdListNext(_handle);
331 if (ErrorCode.None != error)
333 Log.Error(LogTag, "Next Failed with error " + error);
334 throw ExceptionFactory.CreateException(error);
340 /// Moves an index to the previous command.
342 /// <since_tizen> 3 </since_tizen>
344 /// http://tizen.org/privilege/recorder
350 /// http://tizen.org/feature/speech.control
351 /// http://tizen.org/feature/microphone
353 /// <exception cref="InvalidOperationException">
354 /// This exception can be due to the following reasons:
356 /// 2. List reached end
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()
362 ErrorCode error = VcCmdListPrev(_handle);
363 if (ErrorCode.None != error)
365 Log.Error(LogTag, "Previous Failed with error " + error);
366 throw ExceptionFactory.CreateException(error);