-
Notifications
You must be signed in to change notification settings - Fork 241
/
WifiPolicy.cs
284 lines (253 loc) · 10.8 KB
/
WifiPolicy.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
284
/*
* 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 WifiPolicy provides methods to manage wifi policies.
/// </summary>
/// <since_tizen> 6 </since_tizen>
/// <remarks>The WifiPolicy is created by <seealso cref="DevicePolicyManager.GetPolicy{T}"/>. and the DevicePolicyManager instance must exists when using the WifiPolicy.</remarks>
[Obsolete("Deprecated since API level 11.")]
public class WifiPolicy : DevicePolicy, IDisposable
{
/// <summary>
/// The Wifi policy name. This represents <see cref="WifiPolicy.IsWifiAllowed"/>.
/// </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 WifiPolicyName = "Wifi";
/// <summary>
/// The Wifi hotspot policy name. This represents <see cref="WifiPolicy.IsWifiHotspotAllowed"/>.
/// </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 WifiHotspotPolicyName = "WifiHotspot";
private readonly string _wifiPolicyName = "wifi";
private readonly string _wifiHotspotPolicyName = "wifi_hotspot";
private int _wifiCallbackId;
private int _wifiHotspotCallbackId;
private bool _disposed = false;
private Interop.DevicePolicyManager.PolicyChangedCallback _wifiPolicyChangedCallback;
private Interop.DevicePolicyManager.PolicyChangedCallback _wifiHotspotStatePolicyChangedCallback;
private EventHandler<PolicyChangedEventArgs> _wifiPolicyChanged;
private EventHandler<PolicyChangedEventArgs> _wifiHotspotPolicyChanged;
internal WifiPolicy(DevicePolicyManager dpm) : base(dpm)
{
}
/// <summary>
/// A Destructor of WifiPolicy.
/// </summary>
[Obsolete("Deprecated since API level 11.")]
~WifiPolicy()
{
this.Dispose(false);
}
/// <summary>
/// Gets whether the Wi-Fi state change is allowed or not.
/// </summary>
/// <value>true if the state change is allowed, false otherwise. The default value is true.</value>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public bool IsWifiAllowed
{
get
{
int state;
int ret = Interop.DevicePolicyManager.RestrictionGetWifiState(_dpm.GetHandle(), out state);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to get wifi policy " + ret);
return true;
}
return state == 1;
}
}
/// <summary>
/// Gets whether the the Wi-Fi hotspot state change is allowed or not.
/// </summary>
/// <value>true if the state change is allowed, false otherwise. The default value is true.</value>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public bool IsWifiHotspotAllowed
{
get
{
int state;
int ret = Interop.DevicePolicyManager.RestrictionGetWifiHotspotState(_dpm.GetHandle(), out state);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to get wifi hotspot 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 (_wifiCallbackId != 0)
{
try
{
RemoveWifiStatePolicyChangedCallback();
}
catch (Exception e)
{
Log.Error(Globals.LogTag, e.ToString());
}
}
if (_wifiHotspotCallbackId != 0)
{
try
{
RemoveWifiHotspotPolicyChangedCallback();
}
catch (Exception e)
{
Log.Error(Globals.LogTag, e.ToString());
}
}
_disposed = true;
}
}
/// <summary>
/// The WifiPolicyChanged event is raised when the wifi state policy is changed.
/// </summary>
/// <remarks>This event will be removed automatically when WifiPolicy is destroyed.</remarks>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public event EventHandler<PolicyChangedEventArgs> WifiPolicyChanged
{
add
{
if (_wifiPolicyChanged == null)
{
AddWifiStatePolicyChangedCallback();
}
_wifiPolicyChanged += value;
}
remove
{
_wifiPolicyChanged -= value;
if (_wifiPolicyChanged == null)
{
RemoveWifiStatePolicyChangedCallback();
}
}
}
private void AddWifiStatePolicyChangedCallback()
{
if (_wifiPolicyChangedCallback == null)
{
_wifiPolicyChangedCallback = (string name, string state, IntPtr userData) =>
{
_wifiPolicyChanged?.Invoke(this, new PolicyChangedEventArgs(WifiPolicyName, state));
};
}
int ret = Interop.DevicePolicyManager.AddPolicyChangedCallback(_dpm.GetHandle(), _wifiPolicyName, _wifiPolicyChangedCallback, IntPtr.Zero, out _wifiCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to add policy changed callback, name " + _wifiPolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
}
private void RemoveWifiStatePolicyChangedCallback()
{
int ret = Interop.DevicePolicyManager.RemovePolicyChangedCallback(_dpm.GetHandle(), _wifiCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to remove policy changed callback, name " + _wifiPolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
_wifiPolicyChangedCallback = null;
_wifiCallbackId = 0;
}
/// <summary>
/// The WifiHotspotPolicyChanged event is raised when the wifi hotspot policy is changed.
/// </summary>
/// <remarks>This event will be removed automatically when WifiPolicy is destroyed.</remarks>
/// <since_tizen> 6 </since_tizen>
[Obsolete("Deprecated since API level 11.")]
public event EventHandler<PolicyChangedEventArgs> WifiHotspotPolicyChanged
{
add
{
if (_wifiHotspotPolicyChanged == null)
{
AddWifiHotspotPolicyChangedCallback();
}
_wifiHotspotPolicyChanged += value;
}
remove
{
_wifiHotspotPolicyChanged -= value;
if (_wifiHotspotPolicyChanged == null)
{
RemoveWifiHotspotPolicyChangedCallback();
}
}
}
private void AddWifiHotspotPolicyChangedCallback()
{
if (_wifiHotspotStatePolicyChangedCallback == null)
{
_wifiHotspotStatePolicyChangedCallback = (string name, string state, IntPtr userData) =>
{
_wifiHotspotPolicyChanged?.Invoke(this, new PolicyChangedEventArgs(WifiHotspotPolicyName, state));
};
}
int ret = Interop.DevicePolicyManager.AddPolicyChangedCallback(_dpm.GetHandle(), _wifiHotspotPolicyName, _wifiHotspotStatePolicyChangedCallback, IntPtr.Zero, out _wifiHotspotCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to add policy changed callback, name " + _wifiHotspotPolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
}
private void RemoveWifiHotspotPolicyChangedCallback()
{
int ret = Interop.DevicePolicyManager.RemovePolicyChangedCallback(_dpm.GetHandle(), _wifiHotspotCallbackId);
if (ret != (int)Interop.DevicePolicyManager.ErrorCode.None)
{
Log.Error(Globals.LogTag, "Failed to remove policy changed callback, name " + _wifiHotspotPolicyName + ", ret : " + ret);
throw DevicePolicyManagerErrorFactory.CreateException(ret);
}
_wifiHotspotStatePolicyChangedCallback = null;
_wifiHotspotCallbackId = 0;
}
}
}