-
Notifications
You must be signed in to change notification settings - Fork 241
/
MediaPolicy.cs
283 lines (253 loc) · 10.8 KB
/
MediaPolicy.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2018 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;
namespace Tizen.Security.DevicePolicyManager
{
/// <summary>
/// The MediaPolicy provides methods to manage media policies.
/// </summary>
/// <since_tizen> 6 </since_tizen>
/// <remarks>The MediaPolicy is created by <seealso cref="DevicePolicyManager.GetPolicy{T}"/>. and the DevicePolicyManager instance must exists when using the MediaPolicy.</remarks>
[Obsolete("Deprecated since API level 11.")]
public class MediaPolicy : DevicePolicy, IDisposable
{
/// <summary>
/// The Camera policy name. This represents <see cref="MediaPolicy.IsCameraAllowed"/>.
/// </summary>
/// <remarks>This is used in <see cref="PolicyChangedEventArgs.PolicyName"/>.</remarks>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public static readonly string CameraPolicyName = "Camera";
/// <summary>
/// The Microphone policy name. This represents <see cref="MediaPolicy.IsMicrophoneAllowed"/>.
/// </summary>
/// <remarks>This is used in <see cref="PolicyChangedEventArgs.PolicyName"/>.</remarks>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public static readonly string MicrophonePolicyName = "Microphone";
private readonly string _cameraPolicyName = "camera";
private readonly string _microphonePolicyName = "microphone";
private int _cameraCallbackId;
private int _microphoneCallbackId;
private bool _disposed = false;
private Interop.DevicePolicyManager.PolicyChangedCallback _cameraPolicyChangedCallback;
private Interop.DevicePolicyManager.PolicyChangedCallback _microphonePolicyChangedCallback;
private EventHandler<PolicyChangedEventArgs> _cameraPolicyChanged;
private EventHandler<PolicyChangedEventArgs> _microphonePolicyChanged;
internal MediaPolicy(DevicePolicyManager dpm) : base(dpm)
{
}
/// <summary>
/// A Destructor of MediaPolicy.
/// </summary>
[Obsolete("Deprecated since API level 11.")]
~MediaPolicy()
{
this.Dispose(false);
}
/// <summary>
/// Gets whether the use of camera is allowed or not.
/// </summary>
/// <value>true if the use of camera is allowed, false otherwise. The default value is true.</value>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public bool IsCameraAllowed
{
get
{
int state;
int ret = Interop.DevicePolicyManager.RestrictionGetCameraState(_dpm.GetHandle(), out state);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to get camera policy " + ret);
return true;
}
return state == 1;
}
}
/// <summary>
/// Gets whether the use of microphone is allowed or not.
/// </summary>
/// <value>true if the use of microphone is allowed, false otherwise. The default value is true.</value>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public bool IsMicrophoneAllowed
{
get
{
int state;
int ret = Interop.DevicePolicyManager.RestrictionGetMicrophoneState(_dpm.GetHandle(), out state);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to get microphone policy " + ret);
return true;
}
return state == 1;
}
}
/// <summary>
/// Releases any unmanaged resources used by this object.
/// </summary>
[Obsolete("Deprecated since API level 11.")]
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
/// </summary>
/// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
[Obsolete("Deprecated since API level 11.")]
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// to be used if there are any other disposable objects
}
if (_cameraCallbackId != 0)
{
try
{
RemoveCameraPolicyChangedCallback();
}
catch (Exception e)
{
Log.Error(Globals.LogTag, e.ToString());
}
}
if (_microphoneCallbackId != 0)
{
try
{
RemoveMicrophonePolicyChangedCallback();
}
catch (Exception e)
{
Log.Error(Globals.LogTag, e.ToString());
}
}
_disposed = true;
}
}
/// <summary>
/// The CameraPolicyChanged event is raised when the camera policy is changed.
/// </summary>
/// <remarks>This event will be removed automatically when MediaPolicy is destroyed.</remarks>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public event EventHandler<PolicyChangedEventArgs> CameraPolicyChanged
{
add
{
if (_cameraPolicyChanged == null)
{
AddCameraPolicyChangedCallback();
}
_cameraPolicyChanged += value;
}
remove
{
_cameraPolicyChanged -= value;
if (_cameraPolicyChanged == null)
{
RemoveCameraPolicyChangedCallback();
}
}
}
private void AddCameraPolicyChangedCallback()
{
if (_cameraPolicyChangedCallback == null)
{
_cameraPolicyChangedCallback = (string name, string state, IntPtr userData) =>
{
_cameraPolicyChanged?.Invoke(this, new PolicyChangedEventArgs(CameraPolicyName, state));
};
}
int ret = Interop.DevicePolicyManager.AddPolicyChangedCallback(_dpm.GetHandle(), _cameraPolicyName, _cameraPolicyChangedCallback, IntPtr.Zero, out _cameraCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to add policy changed callback, name " + _cameraPolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
}
private void RemoveCameraPolicyChangedCallback()
{
int ret = Interop.DevicePolicyManager.RemovePolicyChangedCallback(_dpm.GetHandle(), _cameraCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to remove policy changed callback, name " + _cameraPolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
_cameraPolicyChangedCallback = null;
_cameraCallbackId = 0;
}
/// <summary>
/// The MicrophonePolicyChanged event is raised when the microphone policy is changed.
/// </summary>
/// <remarks>This event will be removed automatically when MediaPolicy is destroyed.</remarks>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public event EventHandler<PolicyChangedEventArgs> MicrophonePolicyChanged
{
add
{
if (_microphonePolicyChanged == null)
{
AddMicrophonePolicyChangedCallback();
}
_microphonePolicyChanged += value;
}
remove
{
_microphonePolicyChanged -= value;
if (_microphonePolicyChanged == null)
{
RemoveMicrophonePolicyChangedCallback();
}
}
}
private void AddMicrophonePolicyChangedCallback()
{
if (_microphonePolicyChangedCallback == null)
{
_microphonePolicyChangedCallback = (string name, string state, IntPtr userData) =>
{
_microphonePolicyChanged?.Invoke(this, new PolicyChangedEventArgs(MicrophonePolicyName, state));
};
}
int ret = Interop.DevicePolicyManager.AddPolicyChangedCallback(_dpm.GetHandle(), _microphonePolicyName, _microphonePolicyChangedCallback, IntPtr.Zero, out _microphoneCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to add policy changed callback, name " + _microphonePolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
}
private void RemoveMicrophonePolicyChangedCallback()
{
int ret = Interop.DevicePolicyManager.RemovePolicyChangedCallback(_dpm.GetHandle(), _microphoneCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to remove policy changed callback, name " + _microphonePolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
_microphonePolicyChangedCallback = null;
_microphoneCallbackId = 0;
}
}
}