public static DeviceInfo CheckDevice(string userAgent) { DeviceInfo deviceInfo = null; if (userAgent.StartsWith("PF-Device", StringComparison.OrdinalIgnoreCase)) { deviceInfo = new DeviceInfo(); string[] tokens = userAgent.Split(new char[] { ',' }); if (tokens.Length >= 4) { deviceInfo.Platform = tokens[1]; deviceInfo.Model = tokens[2]; deviceInfo.OperatingSystem = tokens[3]; } else { deviceInfo.Platform = string.Empty; deviceInfo.Model = string.Empty; deviceInfo.OperatingSystem = string.Empty; } return deviceInfo; } foreach (var item in DeviceInfo.DeviceCache) { Regex regex = new Regex(item.OSRegx, RegexOptions.IgnoreCase); var result = regex.Match(userAgent); if (result.Success) { deviceInfo = GenerateDeviceInfo(item, result.Value, userAgent); break; } } if (deviceInfo == null) { deviceInfo = DeviceInfo.UnknownDevice; } return deviceInfo; }
private static DeviceInfo Copy(DeviceInfo info) { return new DeviceInfo() { Manufacturer = info.Manufacturer, Model = info.Model, DeviceType = info.DeviceType, Platform = info.Platform, OperatingSystem = info.OperatingSystem, OSRegx = info.OSRegx }; }
private static DeviceInfo GenerateDeviceInfo(DeviceInfo info, string matchResult, string userAgent) { DeviceInfo device = DeviceInfo.Copy(info); Regex regex; Match match; switch (info.OperatingSystem) { case "iOS": regex = new Regex("OS [0-9]*"); match = regex.Match(userAgent); if (match.Success) { device.OperatingSystem = info.OperatingSystem + " " + match.Value.Substring(3); } break; case "Mac OS": regex = new Regex("Mac OS X [0-9]*", RegexOptions.IgnoreCase); match = regex.Match(userAgent); if (match.Success) device.OperatingSystem = match.Value; break; case "Android": device.OperatingSystem = matchResult; break; default: break; } if (info.Model.EqualsIgnoreCase("Android")) { device.Model = userAgent.Substring(userAgent.LastIndexOf(';') + 2, userAgent.IndexOf("Build/") - userAgent.LastIndexOf(';') - 3); } if (userAgent.ContainsIgnoreCase("Mobile")) device.DeviceType = "Mobile"; return device; }
public static void Init(XmlDocument doc) { XmlNodeList devices = doc.SelectNodes("UserAgent/Devices/Device"); foreach (XmlNode item in devices) { var device = new DeviceInfo() { Manufacturer = item.Attributes["Manufacturer"].Value, OperatingSystem = item.Attributes["OperatingSystem"].Value, DeviceType = item.Attributes["DeviceType"].Value, Model = item.Attributes["Model"].Value, Platform = item.Attributes["Platform"].Value, OSRegx = item.Attributes["OSRegx"].Value }; DeviceCache.Add(device); } }