-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathDataProvider.cs
423 lines (385 loc) · 16.3 KB
/
DataProvider.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
/* *********************************************************************
* This Source Code Form is copyright of 51Degrees Mobile Experts Limited.
* Copyright © 2015 51Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY
*
* This Source Code Form is the subject of the following patent
* applications, owned by 51Degrees Mobile Experts Limited of 5 Charlotte
* Close, Caversham, Reading, Berkshire, United Kingdom RG4 7BY:
* European Patent Application No. 13192291.6; and
* United States Patent Application Nos. 14/085,223 and 14/085,301.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0.
*
* If a copy of the MPL was not distributed with this file, You can obtain
* one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”, as
* defined by the Mozilla Public License, v. 2.0.
* ********************************************************************* */
using System;
using System.Collections.Generic;
using FiftyOne.Foundation.Mobile.Detection.Entities;
using System.Linq;
using FiftyOne.Foundation.Mobile.Detection;
using System.Web;
namespace FiftyOne.Foundation.UI
{
// Disable obsolete warnings.
#pragma warning disable 0618
/// <summary>
/// Static class providing UI optimised methods for accessing device data.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static class DataProvider
{
#region Fields
// Lock used when loading the vendors.
private static readonly object _lock = new object();
// A list of hardware vendors and the number of devices assigned to each.
private static SortedList<Value, List<Device>> _vendors = null;
private static List<Device> _devices;
#endregion
#region Properties
/// <summary>
/// Returns the active provider for the factory.
/// </summary>
[Obsolete("Use WebProvider ActiveProvider property")]
public static Provider Provider
{
get { return WebProvider.ActiveProvider; }
}
/// <summary>
/// Returns a list of those properties that relate to the device hardware.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static IList<Property> HardwareProperties
{
get
{
return Provider.DataSet.Hardware.Properties.Where(i =>
i.Values != null &&
i.Values.Count > 0).OrderBy(i => i.Name).ToList();
}
}
/// <summary>
/// Returns a list of all the available properties.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static IList<Property> Properties
{
get
{
return Provider.DataSet.Properties.Where(i =>
i.Values != null &&
i.Values.Count > 0).OrderBy(i => i.Name).ToList();
}
}
/// <summary>
/// Returns a list of the properties that relate to the software.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static IList<Property> SoftwareProperties
{
get
{
return Provider.DataSet.Software.Properties.Where(i =>
i.Values != null &&
i.Values.Count > 0).OrderBy(i => i.Name).ToList();
}
}
/// <summary>
/// Returns a list of the properties that relate to the browser.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static IList<Property> BrowserProperties
{
get
{
return Provider.DataSet.Browsers.Properties.Where(i =>
i.Values != null &&
i.Values.Count > 0).OrderBy(i => i.Name).ToList();
}
}
/// <summary>
/// Returns a list of the properties that relate to the content.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static IList<Property> ContentProperties
{
get
{
return Provider.DataSet.Properties.Where(i =>
Constants.Content.Contains(i.Name) &&
i.Values != null &&
i.Values.Count > 0).OrderBy(i => i.Name).ToList();
}
}
/// <summary>
/// Returns true if CMS data is being used by the active provider.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static bool IsCms
{
get
{
// Try the new data set name property first.
if (Provider.DataSet.Name.Equals("CMS"))
return true;
return IsPremium == false &&
(from i in Provider.DataSet.Properties select i.Name).Intersect(UI.Constants.CMS).Count() == UI.Constants.CMS.Length;
}
}
/// <summary>
/// Returns true if premium data is being used by the active provider. The number
/// of available properties is used to determine if this is true.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static bool IsPremium
{
get
{
return Provider.DataSet.Name == "Premium" || Provider.DataSet.Name == "Ultimate";
}
}
/// <summary>
/// Returns a list of all the devices.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static List<Device> Devices
{
get
{
if (_devices == null)
{
lock (_lock)
{
if (_devices == null)
{
_devices = new List<Device>();
foreach (KeyValuePair<Value, List<Device>> vendor in Vendors)
_devices.AddRange(vendor.Value);
}
}
}
return _devices;
}
}
/// <summary>
/// Returns a list of the available hardware vendors.
/// </summary>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static SortedList<Value, List<Device>> Vendors
{
get
{
if (_vendors == null)
{
lock (_lock)
{
if (_vendors == null)
{
_vendors = new SortedList<Value, List<Device>>();
var property = Provider.DataSet.Hardware.Properties.FirstOrDefault(i =>
i.Name == "HardwareVendor");
if (property != null)
{
foreach (var value in property.Values.Where(i =>
i.Name != "Unknown" &&
i.Signatures.Length > 0))
_vendors.Add(value, GetVendorDevices(value));
}
}
}
}
return _vendors;
}
}
#endregion
#region Public Methods
/// <summary>
/// Returns true if the property is only available in the premium data.
/// </summary>
/// <param name="property"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static bool GetIsPremium(Property property)
{
return "Lite".Equals(WebProvider.ActiveProvider.DataSet.Name) == false;
}
/// <summary>
/// Returns the device from the user agent.
/// </summary>
/// <param name="userAgent"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static Device GetDevice(string userAgent)
{
var match = Provider.Match(userAgent);
if (match != null)
{
var profile = match.Profiles.FirstOrDefault(i =>
i.Component.ComponentId == i.DataSet.Hardware.ComponentId);
if (profile != null)
return new Device(profile);
}
return null;
}
/// <summary>
/// Returns the device ID for the device matching the user agent provided.
/// </summary>
/// <param name="userAgent"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static string GetDeviceID(string userAgent)
{
var match = Provider.Match(userAgent);
if (match != null)
return match.DeviceId;
return null;
}
/// <summary>
/// Get the property for the name provided.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static Property GetProperty(string name)
{
return Provider.DataSet.Properties.FirstOrDefault(i =>
i.Name == name);
}
/// <summary>
/// Returns properties based on the unique ID of the device.
/// </summary>
/// <param name="deviceID"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static Device GetDeviceFromDeviceID(string deviceID)
{
var device = Provider.DataSet.Signatures.FirstOrDefault(i => deviceID.Equals(i.DeviceId));
if (device != null)
return new Device(device);
return null;
}
/// <summary>
/// Returns the first device which contains the profile ID passed.
/// </summary>
/// <param name="profileID"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static Device GetDeviceFromProfileID(string profileID)
{
int profileValue;
if (int.TryParse(profileID, out profileValue))
{
// Try the profiles without unknown software and browsers first.
var profile = Provider.DataSet.FindProfile(profileValue);
// If not found drop the requirement for no unknown browsers or software.
if (profile == null)
return new Device(profile);
}
return null;
}
/// <summary>
/// Returns a device based on the hardware vendor and model seperated by a pipe sign.
/// </summary>
/// <param name="vendor"></param>
/// <param name="model"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static Device GetDeviceFromModel(string vendor, string model)
{
// Try the profiles without unknown software and browsers first.
Profile profile = Provider.DataSet.Hardware.Profiles.FirstOrDefault(i =>
i["HardwareVendor"] != null && i["HardwareVendor"].Equals(vendor) &&
i["HardwareModel"] != null && i["HardwareModel"].Equals(model));
// The device does not exist.
if (profile != null)
return new Device(profile);
return null;
}
/// <summary>
/// Get any device which has the same hardware model as the one provided.
/// </summary>
/// <param name="device">The device which others should relate to.</param>
/// <returns>A list of related devices.</returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static List<Device> GetRelatedInfo(Device device)
{
List<Device> list = null;
if (device.HardwareModel != null)
{
var profile = Provider.DataSet.Hardware.Profiles.FirstOrDefault(i =>
i["HardwareModel"] != null && i["HardwareModel"].Equals(device.HardwareModel));
if (profile != null)
{
list = profile.Signatures.Select(i => new Device(i)).ToList();
}
}
list.Sort(CompareDeviceNames);
return list;
}
/// <summary>
/// Returns a list of devices that match the search value provided.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static List<Device> FindDevices(string value)
{
return Devices.Where(i =>
(i.HardwareVendor != null && i.HardwareVendor.IndexOf(value, StringComparison.InvariantCultureIgnoreCase) >= 0) ||
(i.HardwareModel != null && i.HardwareModel.IndexOf(value, StringComparison.InvariantCultureIgnoreCase) >= 0) ||
(i.HardwareName != null && i.HardwareName.IndexOf(value, StringComparison.InvariantCultureIgnoreCase) >= 0) ||
(i.SoftwareCaption != null && i.SoftwareCaption.IndexOf(value, StringComparison.InvariantCultureIgnoreCase) >= 0) ||
(i.BrowserCaption != null && i.BrowserCaption.IndexOf(value, StringComparison.InvariantCultureIgnoreCase) >= 0)).ToList<Device>();
}
/// <summary>
/// Determines if the two devices are identical.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
private static bool CompareRelatedDevices(Device x, Device y)
{
return x.HardwareVendor == y.HardwareVendor &&
x.IsUnknown == false &&
String.IsNullOrEmpty(x.SoftwareBrowserCaption) == false &&
x.DeviceID != y.DeviceID;
}
/// <summary>
/// Compares two different devices by name and returns the sort order
/// for the list.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
[Obsolete("DataProvider is deprecated. All data access should use DataSet.")]
public static int CompareDeviceNames(Device x, Device y)
{
return x.SoftwareBrowserCaption.CompareTo(y.SoftwareBrowserCaption);
}
#endregion
#region Private Methods
/// <summary>
/// Returns a list of the hardware devices for the vendor requested.
/// </summary>
/// <param name="vendor"></param>
/// <returns></returns>
private static List<Device> GetVendorDevices(Value vendor)
{
var list = new List<Device>();
foreach (var signature in vendor.Signatures)
{
var device = new Device(signature);
if (list.FirstOrDefault(i =>
i.HardwareModel == device.HardwareModel) == null)
list.Add(device);
}
return list.OrderBy(i => i.HardwareModel).ToList();
}
#endregion
}
}