forked from microsoft/Windows-universal-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetConnectionProfiles.xaml.cs
488 lines (438 loc) · 19.8 KB
/
GetConnectionProfiles.xaml.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using SDKTemplate;
using System;
using Windows.Networking.NetworkOperators;
using System.Collections.Generic;
using Windows.Networking.Connectivity;
namespace MobileBroadband
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class GetConnectionProfiles : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
MainPage rootPage = MainPage.Current;
private IReadOnlyList<string> deviceAccountId = null;
public GetConnectionProfiles()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
PrepareScenario();
}
/// <summary>
/// This is the click handler for the 'Default' button. You would replace this with your own handler
/// if you have a button or buttons on this page.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GetConnectionProfilesButton_Click(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
if (b != null)
{
try
{
// For the sake of simplicity, we only get the ConnectionProfiles from the first NetworkAccountId
var mobileBroadbandAccount = MobileBroadbandAccount.CreateFromNetworkAccountId(deviceAccountId[0]);
PrintConnectionProfiles(mobileBroadbandAccount.GetConnectionProfiles());
}
catch (Exception ex)
{
rootPage.NotifyUser("Error:" + ex.Message, NotifyType.ErrorMessage);
}
}
}
void PrepareScenario()
{
rootPage.NotifyUser("", NotifyType.StatusMessage);
try
{
deviceAccountId = MobileBroadbandAccount.AvailableNetworkAccountIds;
if (deviceAccountId.Count != 0)
{
GetConnectionProfilesButton.Content = "Get Connection Profiles";
GetConnectionProfilesButton.IsEnabled = true;
}
else
{
GetConnectionProfilesButton.Content = "No available accounts detected";
GetConnectionProfilesButton.IsEnabled = false;
}
}
catch (Exception ex)
{
rootPage.NotifyUser("Error:" + ex.Message, NotifyType.ErrorMessage);
}
}
private void PrintConnectionProfiles(IReadOnlyList<ConnectionProfile> connectionProfiles)
{
string connectionProfileList = string.Empty;
try
{
foreach (var connectionProfile in connectionProfiles)
{
//Display Profile information for each of the connection profiles
connectionProfileList += GetConnectionProfile(connectionProfile);
connectionProfileList += "--------------------------------------------------------------------\n";
}
if (connectionProfiles.Count == 0)
{
connectionProfileList += "No Connection Profiles found.\n";
}
rootPage.NotifyUser(connectionProfileList, NotifyType.StatusMessage);
}
catch (Exception ex)
{
rootPage.NotifyUser("Unexpected exception occurred: " + ex.ToString(), NotifyType.ErrorMessage);
}
}
//
//Get Connection Profile name and cost information
//
string GetConnectionProfile(ConnectionProfile connectionProfile)
{
string connectionProfileInfo = string.Empty;
if (connectionProfile != null)
{
connectionProfileInfo = "Profile Name : " + connectionProfile.ProfileName + "\n";
switch (connectionProfile.GetNetworkConnectivityLevel())
{
case NetworkConnectivityLevel.None:
connectionProfileInfo += "Connectivity Level : None\n";
break;
case NetworkConnectivityLevel.LocalAccess:
connectionProfileInfo += "Connectivity Level : Local Access\n";
break;
case NetworkConnectivityLevel.ConstrainedInternetAccess:
connectionProfileInfo += "Connectivity Level : Constrained Internet Access\n";
break;
case NetworkConnectivityLevel.InternetAccess:
connectionProfileInfo += "Connectivity Level : Internet Access\n";
break;
}
switch (connectionProfile.GetDomainConnectivityLevel())
{
case DomainConnectivityLevel.None:
connectionProfileInfo += "Domain Connectivity Level : None\n";
break;
case DomainConnectivityLevel.Unauthenticated:
connectionProfileInfo += "Domain Connectivity Level : Unauthenticated\n";
break;
case DomainConnectivityLevel.Authenticated:
connectionProfileInfo += "Domain Connectivity Level : Authenticated\n";
break;
}
//Get Connection Cost information
ConnectionCost connectionCost = connectionProfile.GetConnectionCost();
connectionProfileInfo += GetConnectionCostInfo(connectionCost);
//Get Dataplan Status information
DataPlanStatus dataPlanStatus = connectionProfile.GetDataPlanStatus();
connectionProfileInfo += GetDataPlanStatusInfo(dataPlanStatus);
//Get Network Security Settings
NetworkSecuritySettings netSecuritySettings = connectionProfile.NetworkSecuritySettings;
connectionProfileInfo += GetNetworkSecuritySettingsInfo(netSecuritySettings);
//Get Wlan Connection Profile Details if this is a Wlan ConnectionProfile
if (connectionProfile.IsWlanConnectionProfile)
{
WlanConnectionProfileDetails wlanConnectionProfileDetails = connectionProfile.WlanConnectionProfileDetails;
connectionProfileInfo += GetWlanConnectionProfileDetailsInfo(wlanConnectionProfileDetails);
}
//Get Wwan Connection Profile Details if this is a Wwan ConnectionProfile
if (connectionProfile.IsWwanConnectionProfile)
{
WwanConnectionProfileDetails wwanConnectionProfileDetails = connectionProfile.WwanConnectionProfileDetails;
connectionProfileInfo += GetWwanConnectionProfileDetailsInfo(wwanConnectionProfileDetails);
}
//Get the Service Provider GUID
if (connectionProfile.ServiceProviderGuid.HasValue)
{
connectionProfileInfo += "====================\n";
connectionProfileInfo += "Service Provider GUID: " + connectionProfile.ServiceProviderGuid + "\n";
}
//Get the number of signal bars
if (connectionProfile.GetSignalBars().HasValue)
{
connectionProfileInfo += "====================\n";
connectionProfileInfo += "Signal Bars: " + connectionProfile.GetSignalBars() + "\n";
}
}
return connectionProfileInfo;
}
//
//Get Profile Connection cost
//
string GetConnectionCostInfo(ConnectionCost connectionCost)
{
string cost = string.Empty;
cost += "Connection Cost Information: \n";
cost += "====================\n";
if (connectionCost == null)
{
cost += "Connection Cost not available\n";
return cost;
}
switch (connectionCost.NetworkCostType)
{
case NetworkCostType.Unrestricted:
cost += "Cost: Unrestricted";
break;
case NetworkCostType.Fixed:
cost += "Cost: Fixed";
break;
case NetworkCostType.Variable:
cost += "Cost: Variable";
break;
case NetworkCostType.Unknown:
cost += "Cost: Unknown";
break;
default:
cost += "Cost: Error";
break;
}
cost += "\n";
cost += "Roaming: " + connectionCost.Roaming + "\n";
cost += "Over Data Limit: " + connectionCost.OverDataLimit + "\n";
cost += "Approaching Data Limit : " + connectionCost.ApproachingDataLimit + "\n";
//Display cost based suggestions to the user
cost += CostBasedSuggestions(connectionCost);
return cost;
}
//
//Display Cost based suggestions to the user
//
string CostBasedSuggestions(ConnectionCost connectionCost)
{
string costBasedSuggestions = string.Empty;
costBasedSuggestions += "Cost Based Suggestions: \n";
costBasedSuggestions += "====================\n";
if (connectionCost.Roaming)
{
costBasedSuggestions += "Connection is out of MNO's network, using the connection may result in additional charge. Application can implement High Cost behavior in this scenario\n";
}
else if (connectionCost.NetworkCostType == NetworkCostType.Variable)
{
costBasedSuggestions += "Connection cost is variable, and the connection is charged based on usage, so application can implement the Conservative behavior\n";
}
else if (connectionCost.NetworkCostType == NetworkCostType.Fixed)
{
if (connectionCost.OverDataLimit || connectionCost.ApproachingDataLimit)
{
costBasedSuggestions += "Connection has exceeded the usage cap limit or is approaching the datalimit, and the application can implement High Cost behavior in this scenario\n";
}
else
{
costBasedSuggestions += "Application can implemement the Conservative behavior\n";
}
}
else
{
costBasedSuggestions += "Application can implement the Standard behavior\n";
}
return costBasedSuggestions;
}
//
//Display Profile Dataplan Status information
//
string GetDataPlanStatusInfo(DataPlanStatus dataPlan)
{
string dataplanStatusInfo = string.Empty;
dataplanStatusInfo = "Dataplan Status Information:\n";
dataplanStatusInfo += "====================\n";
if (dataPlan == null)
{
dataplanStatusInfo += "Dataplan Status not available\n";
return dataplanStatusInfo;
}
if (dataPlan.DataPlanUsage != null)
{
dataplanStatusInfo += "Usage In Megabytes : " + dataPlan.DataPlanUsage.MegabytesUsed + "\n";
dataplanStatusInfo += "Last Sync Time : " + dataPlan.DataPlanUsage.LastSyncTime + "\n";
}
else
{
dataplanStatusInfo += "Usage In Megabytes : Not Defined\n";
}
ulong? inboundBandwidth = dataPlan.InboundBitsPerSecond;
if (inboundBandwidth.HasValue)
{
dataplanStatusInfo += "InboundBitsPerSecond : " + inboundBandwidth + "\n";
}
else
{
dataplanStatusInfo += "InboundBitsPerSecond : Not Defined\n";
}
ulong? outboundBandwidth = dataPlan.OutboundBitsPerSecond;
if (outboundBandwidth.HasValue)
{
dataplanStatusInfo += "OutboundBitsPerSecond : " + outboundBandwidth + "\n";
}
else
{
dataplanStatusInfo += "OutboundBitsPerSecond : Not Defined\n";
}
uint? dataLimit = dataPlan.DataLimitInMegabytes;
if (dataLimit.HasValue)
{
dataplanStatusInfo += "DataLimitInMegabytes : " + dataLimit + "\n";
}
else
{
dataplanStatusInfo += "DataLimitInMegabytes : Not Defined\n";
}
System.DateTimeOffset? nextBillingCycle = dataPlan.NextBillingCycle;
if (nextBillingCycle.HasValue)
{
dataplanStatusInfo += "NextBillingCycle : " + nextBillingCycle + "\n";
}
else
{
dataplanStatusInfo += "NextBillingCycle : Not Defined\n";
}
uint? maxTransferSize = dataPlan.MaxTransferSizeInMegabytes;
if (maxTransferSize.HasValue)
{
dataplanStatusInfo += "MaxTransferSizeInMegabytes : " + maxTransferSize + "\n";
}
else
{
dataplanStatusInfo += "MaxTransferSizeInMegabytes : Not Defined\n";
}
return dataplanStatusInfo;
}
//
//Get Network Security Settings information
//
string GetNetworkSecuritySettingsInfo(NetworkSecuritySettings netSecuritySettings)
{
string networkSecurity = string.Empty;
networkSecurity += "Network Security Settings: \n";
networkSecurity += "====================\n";
if (netSecuritySettings == null)
{
networkSecurity += "Network Security Settings not available\n";
return networkSecurity;
}
//NetworkAuthenticationType
switch (netSecuritySettings.NetworkAuthenticationType)
{
case NetworkAuthenticationType.None:
networkSecurity += "NetworkAuthenticationType: None";
break;
case NetworkAuthenticationType.Unknown:
networkSecurity += "NetworkAuthenticationType: Unknown";
break;
case NetworkAuthenticationType.Open80211:
networkSecurity += "NetworkAuthenticationType: Open80211";
break;
case NetworkAuthenticationType.SharedKey80211:
networkSecurity += "NetworkAuthenticationType: SharedKey80211";
break;
case NetworkAuthenticationType.Wpa:
networkSecurity += "NetworkAuthenticationType: Wpa";
break;
case NetworkAuthenticationType.WpaPsk:
networkSecurity += "NetworkAuthenticationType: WpaPsk";
break;
case NetworkAuthenticationType.WpaNone:
networkSecurity += "NetworkAuthenticationType: WpaNone";
break;
case NetworkAuthenticationType.Rsna:
networkSecurity += "NetworkAuthenticationType: Rsna";
break;
case NetworkAuthenticationType.RsnaPsk:
networkSecurity += "NetworkAuthenticationType: RsnaPsk";
break;
default:
networkSecurity += "NetworkAuthenticationType: Error";
break;
}
networkSecurity += "\n";
//NetworkEncryptionType
switch (netSecuritySettings.NetworkEncryptionType)
{
case NetworkEncryptionType.None:
networkSecurity += "NetworkEncryptionType: None";
break;
case NetworkEncryptionType.Unknown:
networkSecurity += "NetworkEncryptionType: Unknown";
break;
case NetworkEncryptionType.Wep:
networkSecurity += "NetworkEncryptionType: Wep";
break;
case NetworkEncryptionType.Wep40:
networkSecurity += "NetworkEncryptionType: Wep40";
break;
case NetworkEncryptionType.Wep104:
networkSecurity += "NetworkEncryptionType: Wep104";
break;
case NetworkEncryptionType.Tkip:
networkSecurity += "NetworkEncryptionType: Tkip";
break;
case NetworkEncryptionType.Ccmp:
networkSecurity += "NetworkEncryptionType: Ccmp";
break;
case NetworkEncryptionType.WpaUseGroup:
networkSecurity += "NetworkEncryptionType: WpaUseGroup";
break;
case NetworkEncryptionType.RsnUseGroup:
networkSecurity += "NetworkEncryptionType: RsnUseGroup";
break;
default:
networkSecurity += "NetworkEncryptionType: Error";
break;
}
networkSecurity += "\n";
return networkSecurity;
}
string GetWlanConnectionProfileDetailsInfo(WlanConnectionProfileDetails wlanConnectionProfileDetails)
{
string wlanDetails = string.Empty;
wlanDetails += "Wireless LAN Connection Profile Details:\n";
wlanDetails += "====================\n";
if (wlanConnectionProfileDetails == null)
{
wlanDetails += "Wireless LAN connection profile details unavailable.\n";
return wlanDetails;
}
wlanDetails += "Connected SSID: " + wlanConnectionProfileDetails.GetConnectedSsid() + "\n";
return wlanDetails;
}
string GetWwanConnectionProfileDetailsInfo(WwanConnectionProfileDetails wwanConnectionProfileDetails)
{
string wwanDetails = string.Empty;
wwanDetails += "Wireless WAN Connection Profile Details:\n";
wwanDetails += "====================\n";
if (wwanConnectionProfileDetails == null)
{
wwanDetails += "Wireless WAN connection profile details unavailable.\n";
return wwanDetails;
}
wwanDetails += "Home Provider ID: " + wwanConnectionProfileDetails.HomeProviderId + "\n";
wwanDetails += "Access Point Name: " + wwanConnectionProfileDetails.AccessPointName + "\n";
wwanDetails += "Network Registration State: " + wwanConnectionProfileDetails.GetNetworkRegistrationState() + "\n";
wwanDetails += "Current Data Class: " + wwanConnectionProfileDetails.GetCurrentDataClass() + "\n";
return wwanDetails;
}
}
}