-
Notifications
You must be signed in to change notification settings - Fork 241
/
VoiceCommandsGroup.cs
167 lines (153 loc) · 5.79 KB
/
VoiceCommandsGroup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2019 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 System.Collections.ObjectModel;
using static Interop.VoiceControlManager;
using static Interop.VoiceControlCommand;
using System.Collections.Specialized;
namespace Tizen.Uix.VoiceControlManager
{
/// <summary>
/// This class represents a list of the voice commands.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public class VoiceCommandsGroup : IDisposable
{
ObservableCollection<VoiceCommand> _commands = new ObservableCollection<VoiceCommand>();
internal SafeCommandListHandle _handle;
private bool _disposed = false;
/// <summary>
/// The public constructor.
/// </summary>
/// <since_tizen> 6 </since_tizen>
/// <privilege> http://tizen.org/privilege/recorder </privilege>
/// <privlevel>public</privlevel>
/// <feature> http://tizen.org/feature/microphone </feature>
/// <feature> http://tizen.org/feature/speech.control </feature>
/// <exception cref="InvalidOperationException">This exception can be due to out of memory.</exception>
/// <exception cref="UnauthorizedAccessException">This exception can be due to permission denied.</exception>
/// <exception cref="NotSupportedException">The required feature is not supported.</exception>
public VoiceCommandsGroup()
{
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;
_commands.CollectionChanged += OnCollectionChanged;
}
internal VoiceCommandsGroup(SafeCommandListHandle handle)
{
_handle = handle;
VcCmdListCb _callback = (IntPtr vcCommand, IntPtr userData) =>
{
SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
_commands.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);
}
_commands.CollectionChanged += OnCollectionChanged;
}
/// <summary>
/// The destructor of the VoiceCommandList class.
/// </summary>
/// <since_tizen> 6 </since_tizen>
~VoiceCommandsGroup()
{
Dispose(false);
}
/// <summary>
/// Gets the command list.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public IList<VoiceCommand> Commands { get => _commands; }
/// <summary>
/// Release any unmanaged resources used by this object.
/// </summary>
/// <since_tizen> 6 </since_tizen>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Release any unmanaged resources used by this object.
/// </summary>
/// <param name="disposing">
/// If true, disposes any disposable objects. If false, does not dispose disposable objects.
/// </param>
/// <since_tizen> 6 </since_tizen>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
return;
if (disposing)
{
_handle?.Dispose();
_handle = null;
_commands.CollectionChanged -= OnCollectionChanged;
_commands.Clear();
}
_disposed = true;
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (e.NewItems!= null && e.NewItems.Count > 0)
{
foreach (VoiceCommand item in e.NewItems)
Add(item);
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove)
{
if (e.OldItems!= null && e.OldItems.Count > 0)
{
foreach (VoiceCommand item in e.OldItems)
Remove(item);
}
}
}
void Add(VoiceCommand command)
{
ErrorCode error = VcCmdListAdd(_handle, command._handle);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "Add Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
}
void Remove(VoiceCommand command)
{
ErrorCode error = VcCmdListRemove(_handle, command._handle);
if (error != ErrorCode.None)
{
Log.Error(LogTag, "Remove Failed with error " + error);
throw ExceptionFactory.CreateException(error);
}
}
}
}