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.VoiceControlWidget;
20 using static Interop.VoiceControlCommand;
22 namespace Tizen.Uix.VoiceControlWidget
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>
39 /// <exception cref="OutOfMemoryException">This exception can be due to out of memory.</exception>
40 /// <exception cref="ArgumentException">This exception can be due to an invalid parameter.</exception>
41 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
42 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
43 public VoiceCommandList()
45 SafeCommandListHandle handle;
46 ErrorCode error = VcCmdListCreate(out handle);
47 if (error != ErrorCode.None)
49 Log.Error(LogTag, "Create Failed with error " + error);
50 throw ExceptionFactory.CreateException(error);
53 _list = new List<VoiceCommand>();
57 internal VoiceCommandList(SafeCommandListHandle handle)
62 _list = new List<VoiceCommand>();
63 _callback = (IntPtr vcCommand, IntPtr userData) =>
65 SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
66 cmdHandle._ownership = false;
67 _list.Add(new VoiceCommand(cmdHandle));
70 ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
71 if (error != ErrorCode.None)
73 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
74 throw ExceptionFactory.CreateException(error);
79 /// Gets a command count of the list.
80 /// -1 is returned in case of an internal failure.
82 /// <since_tizen> 3 </since_tizen>
84 /// Command count of the list.
91 ErrorCode error = VcCmdListGetCount(_handle, out count);
92 if (error != ErrorCode.None)
94 Log.Error(LogTag, "Count Failed with error " + error);
103 /// Gets the current command from the command list by index.
104 /// Null will be returned in case of an empty list.
106 /// <since_tizen> 3 </since_tizen>
108 /// Current command from the command list.
110 public VoiceCommand Current
114 SafeCommandHandle current;
115 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
116 if (ErrorCode.None != error)
118 Log.Error(LogTag, "Current Failed with error " + error);
122 current._ownership = false;
123 return _list[_index];
128 /// Adds a command to the command list.
130 /// <since_tizen> 3 </since_tizen>
131 /// <param name="command">The command</param>
132 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
133 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
134 /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
135 public void Add(VoiceCommand command)
137 ErrorCode error = VcCmdListAdd(_handle, command._handle);
138 if (error != ErrorCode.None)
140 Log.Error(LogTag, "Add Failed with error " + error);
141 throw ExceptionFactory.CreateException(error);
148 /// Removes a command from the command list.
150 /// <since_tizen> 3 </since_tizen>
151 /// <param name="command">The command</param>
152 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
153 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
154 /// <exception cref="NullReferenceException">This will occur if the provided parameter is null.</exception>
155 public void Remove(VoiceCommand command)
157 ErrorCode error = VcCmdListRemove(_handle, command._handle);
158 if (error != ErrorCode.None)
160 Log.Error(LogTag, "Remove Failed with error " + error);
161 throw ExceptionFactory.CreateException(error);
164 _list.Remove(command);
168 /// Retrieves all commands from the command list.
170 /// <since_tizen> 3 </since_tizen>
171 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
172 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
173 public IEnumerable<VoiceCommand> GetAllCommands()
175 _callback = (IntPtr vcCommand, IntPtr userData) =>
177 if (IntPtr.Zero == vcCommand) {
178 Log.Error(LogTag, "Invalid command pointer");
183 ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
184 if (error != ErrorCode.None)
186 Log.Error(LogTag, "GetAllCommands Failed with error " + error);
187 throw ExceptionFactory.CreateException(error);
194 /// Moves an index to the first command.
196 /// <since_tizen> 3 </since_tizen>
197 /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
198 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
199 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
202 ErrorCode error = VcCmdListFirst(_handle);
203 if (ErrorCode.None != error)
205 Log.Error(LogTag, "First Failed with error " + error);
206 throw ExceptionFactory.CreateException(error);
212 /// Moves an index to the last command.
214 /// <since_tizen> 3 </since_tizen>
215 /// <exception cref="InvalidOperationException">This exception can be due to list empty.</exception>
216 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
217 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
220 ErrorCode error = VcCmdListLast(_handle);
221 if (ErrorCode.None != error)
223 Log.Error(LogTag, "Last Failed with error " + error);
224 throw ExceptionFactory.CreateException(error);
230 /// Moves an index to the next command.
232 /// <since_tizen> 3 </since_tizen>
233 /// <exception cref="InvalidOperationException">This exception can be due to list empty or list end.</exception>
234 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
235 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
238 ErrorCode error = VcCmdListNext(_handle);
239 if (ErrorCode.None != error)
241 Log.Error(LogTag, "Next Failed with error " + error);
242 throw ExceptionFactory.CreateException(error);
248 /// Moves an index to the previous command.
250 /// <since_tizen> 3 </since_tizen>
251 /// <exception cref="InvalidOperationException">This exception can be due to list empty or list end.</exception>
252 /// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
253 /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
254 public void Previous()
256 ErrorCode error = VcCmdListPrev(_handle);
257 if (ErrorCode.None != error)
259 Log.Error(LogTag, "Previous Failed with error " + error);
260 throw ExceptionFactory.CreateException(error);