/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using static Interop.VoiceControl;
using static Interop.VoiceControlCommand;
namespace Tizen.Uix.VoiceControl
{
///
/// This class represents a list of the voice commands.
///
/// 3
public class VoiceCommandList
{
internal SafeCommandListHandle _handle;
private List _list;
private VcCmdListCb _callback;
private int _index;
///
/// The public constructor.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
/// This exception can be due to out of memory.
/// This exception can be due to an invalid parameter.
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
public VoiceCommandList()
{
SafeCommandListHandle handle;
ErrorCode error = VcCmdListCreate(out handle);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "Create Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_handle = handle;
_list = new List();
_index = 0;
}
internal VoiceCommandList(SafeCommandListHandle handle)
{
_handle = handle;
_index = 0;
_list = new List();
_callback = (IntPtr vcCommand, IntPtr userData) =>
{
SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
cmdHandle._ownership = false;
_list.Add(new VoiceCommand(cmdHandle));
return true;
};
ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "GetAllCommands Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
}
///
/// Gets a command count of the list.
/// -1 is returned in case of an internal failure.
///
/// 3
///
/// Command count of the list.
///
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
public int Count
{
get
{
int count;
ErrorCode error = VcCmdListGetCount(_handle, out count);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "Count Failed with error " + error);
return -1;
}
return count;
}
}
///
/// Gets the current command from the command list by index.
/// Null will be returned in case of an empty list.
///
/// 3
///
/// Current command from the command list.
///
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
public VoiceCommand Current
{
get
{
SafeCommandHandle current;
ErrorCode error = VcCmdListGetCurrent(_handle, out current);
if (ErrorCode.None != error)
{
Log.Error(LogTag, "Current Failed with error " + error);
return null;
}
current._ownership = false;
return _list[_index];
}
}
///
/// Adds a command to the command list.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
/// The command
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
/// This will occur if the provided parameter is null.
public void Add(VoiceCommand command)
{
if (null == command) {
throw new NullReferenceException("Null Parameter Provided");
}
ErrorCode error = VcCmdListAdd(_handle, command._handle);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "Add Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_list.Add(command);
}
///
/// Removes a command from the command list.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
/// The command
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
/// This will occur if the provided parameter is null.
public void Remove(VoiceCommand command)
{
if (null == command) {
throw new NullReferenceException("Null Parameter Provided");
}
ErrorCode error = VcCmdListRemove(_handle, command._handle);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "Remove Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_list.Remove(command);
}
///
/// Retrieves all commands from the command list.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
public IEnumerable GetAllCommands()
{
_callback = (IntPtr vcCommand, IntPtr userData) =>
{
if (IntPtr.Zero == vcCommand) {
Log.Error(LogTag, "Invalid command pointer");
return false;
}
return true;
};
ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "GetAllCommands Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
return _list;
}
///
/// Moves an index to the first command.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
/// This exception can be due to list empty.
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
public void First()
{
ErrorCode error = VcCmdListFirst(_handle);
if (ErrorCode.None != error)
{
Log.Error(LogTag, "First Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_index = 0;
}
///
/// Moves an index to the last command.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
/// This exception can be due to list empty.
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
public void Last()
{
ErrorCode error = VcCmdListLast(_handle);
if (ErrorCode.None != error)
{
Log.Error(LogTag, "Last Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_index = Count - 1;
}
///
/// Moves an index to the next command.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
///
/// This exception can be due to the following reasons:
/// 1. List empty
/// 2. List reached end
///
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
public void Next()
{
ErrorCode error = VcCmdListNext(_handle);
if (ErrorCode.None != error)
{
Log.Error(LogTag, "Next Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_index++;
}
///
/// Moves an index to the previous command.
///
/// 3
///
/// http://tizen.org/privilege/recorder
///
///
/// public
///
///
/// http://tizen.org/feature/speech.control
/// http://tizen.org/feature/microphone
///
///
/// This exception can be due to the following reasons:
/// 1. List empty
/// 2. List reached end
///
/// This exception can be due to permission denied.
/// This exception can be due to not supported.
public void Previous()
{
ErrorCode error = VcCmdListPrev(_handle);
if (ErrorCode.None != error)
{
Log.Error(LogTag, "Previous Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
_index--;
}
}
}