public DeviceInfo BuildFromEventData(string userAgent) { var dd = new DeviceDetector(userAgent); dd.Parse(); var client = (BrowserMatchResult)dd.GetClient().Matches.First(r => r is BrowserMatchResult); var os = dd.GetOs(); if (!BrowserParser.GetBrowserFamily(client.ShortName, out var browserFamily)) { browserFamily = client.ShortName; } if (!OperatingSystemParser.GetOsFamily(os.Match.ShortName, out var osFamily) || osFamily == "GNU/Linux") { osFamily = os.Match.ShortName; } return(new DeviceInfo { Browser = new BrowserInfo() { Name = client.Name + " " + client.Version, Type = browserFamily }, DeviceType = dd.GetDeviceName(), OperatingSystem = new OsInfo() { Name = os.Match.Platform + " " + os.Match.Name + " " + os.Match.Version, Type = osFamily }, UserAgent = userAgent }); }
/// <summary> /// Returns if the parsed UA was identified as desktop device /// Desktop devices are all devices with an unknown type that are running a desktop os /// </summary> /// <returns></returns> public bool IsDesktop() { var osShort = os.Success ? os.Match.ShortName : string.Empty; if (string.IsNullOrEmpty(osShort) || UNKNOWN == osShort) { return(false); } // Check for browsers available for mobile devices only if (UsesMobileBrowser()) { return(false); } OperatingSystemParser.GetOsFamily(osShort, out string decodedFamily); return(Array.IndexOf(desktopOsArray, decodedFamily) > -1); }
/// <summary> /// Determines if <paramref name="userAgent"/> represents a mobile browser. /// </summary> /// <param name="userAgent"></param> /// <returns></returns> public static bool IsMobile(this string userAgent) { ParseResult <OsMatchResult> os = DetectOs(userAgent); var osShort = os.Success ? os.Match.ShortName : ""; if (string.IsNullOrEmpty(osShort) || UNKNOWN == osShort) { return(false); } OperatingSystemParser.GetOsFamily(osShort, out string decodedFamily); bool isDesktop = Array.IndexOf(desktopOsArray, decodedFamily) > -1; if (isDesktop) { return(false); } ParseResult <DeviceMatchResult> devResult = DetectDevice(userAgent); if (devResult.Success && devResult.Match.Type.HasValue) { if (MobileDeviceTypes.Contains(devResult.Match.Type.Value)) { return(true); } if (NonMobileDeviceTypes.Contains(devResult.Match.Type.Value)) { return(false); } } // Check for browsers available for mobile devices only if (IsMobileBrowser(userAgent)) { return(true); } //true? unknown? return(false); }
/// <summary> /// Parses a useragent and returns the detected data /// /// ATTENTION: Use that method only for testing or very small applications /// To get fast results from DeviceDetector you need to make your own implementation, /// that should use one of the caching mechanisms. See README.md for more information. /// /// </summary> /// <param name="ua">UserAgent to parse</param> /// <returns></returns> public static ParseResult <DeviceDetectorResult> GetInfoFromUserAgent(string ua) { var result = new ParseResult <DeviceDetectorResult>(); var deviceDetector = new DeviceDetector(ua); deviceDetector.Parse(); var match = new DeviceDetectorResult { UserAgent = deviceDetector.userAgent }; if (deviceDetector.IsBot()) { match.Bot = deviceDetector.bot.Match; } match.Os = deviceDetector.os.Match; match.Client = deviceDetector.client.Match; match.DeviceType = deviceDetector.GetDeviceName(); match.DeviceBrand = deviceDetector.brand; match.DeviceModel = deviceDetector.model; if (deviceDetector.os.Success) { OperatingSystemParser.GetOsFamily(deviceDetector.os.Match.ShortName, out var osFamily); match.OsFamily = osFamily; } if (!(deviceDetector.client.Match is BrowserMatchResult browserMatch)) { return(result.Add(match)); } BrowserParser.GetBrowserFamily(browserMatch.ShortName, out var browserFamily); match.BrowserFamily = browserFamily; return(result.Add(match)); }
/// <summary> /// @todo: refactory /// </summary> protected void ParseDevice() { foreach (var deviceParser in deviceParsers) { deviceParser.SetCache(cache); deviceParser.SetUserAgent(userAgent); if (deviceParser.ParserName == "tv") { var parser = (HbbTvParser)deviceParser; var result = parser.Parse(); if (result.Success) { device = result.Match.Type; model = result.Match.Name; brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "consoles") { var parser = (ConsoleParser)deviceParser; var result = parser.Parse(); if (result.Success) { device = result.Match.Type; model = result.Match.Name; brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "car browser") { var parser = (CarBrowserParser)deviceParser; var result = parser.Parse(); if (result.Success) { device = result.Match.Type; model = result.Match.Name; brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "camera") { var parser = (CameraParser)deviceParser; var result = parser.Parse(); if (result.Success) { device = result.Match.Type; model = result.Match.Name; brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "portablemediaplayer") { var parser = (PortableMediaPlayerParser)deviceParser; var result = parser.Parse(); if (result.Success) { device = result.Match.Type; model = result.Match.Name; brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "mobiles") { var parser = (MobileParser)deviceParser; var result = parser.Parse(); if (result.Success) { device = result.Match.Type; model = result.Match.Name; brand = result.Match.Brand; break; } } } //If no brand has been assigned try to match by known vendor fragments if (string.IsNullOrEmpty(brand)) { var vendorParser = new VendorFragmentParser(); vendorParser.SetUserAgent(userAgent); //vendorParser.SetYamlParser(); vendorParser.SetCache(cache); var result = vendorParser.Parse(); if (result.Success) { brand = result.Match.Brand; } } os = GetOs(); var osShortName = string.Empty; var osFamily = string.Empty; var osVersion = string.Empty; if (os.Success) { osShortName = os.Match.ShortName; OperatingSystemParser.GetOsFamily(osShortName, out osFamily); osVersion = os.Match.Version; if (!string.IsNullOrEmpty(osVersion)) { osVersion = !osVersion.Contains(".") ? osVersion + ".0" : osVersion; } } client = GetClient(); var clientName = client.Success ? client.Match.Name : string.Empty; //Assume all devices running iOS / Mac OS are from Apple if (string.IsNullOrEmpty(brand) && new [] { "ATV", "IOS", "MAC" }.Contains(osShortName)) { brand = "AP"; } //Chrome on Android passes the device type based on the keyword 'Mobile' //If it is present the device should be a smartphone, otherwise it's a tablet //See https://developer.chrome.com/multidevice/user-agent#chrome_for_android_user_agent //Note: We do not check for browser (family) here, as there might be mobile apps using Chrome, that won't have //a detected browser, but can still be detected. So we check the useragent for Chrome instead. if (!device.HasValue && osFamily == "Android" && IsMatchUserAgent(@"Chrome/[\.0-9]*")) { if (IsMatchUserAgent(@"Chrome/[\.0-9]* Mobile")) { device = DeviceType.DEVICE_TYPE_SMARTPHONE; } else if (IsMatchUserAgent(@"Chrome/[\.0-9]* (?!Mobile)")) { device = DeviceType.DEVICE_TYPE_TABLET; } } //Some user agents simply contain the fragment 'Android; Tablet;' or 'Opera Tablet', so we assume those devices as tablets if (!device.HasValue && (HasAndroidTableFragment() || IsMatchUserAgent("Opera Tablet"))) { device = DeviceType.DEVICE_TYPE_TABLET; } //Some user agents simply contain the fragment 'Android; Mobile;', so we assume those devices as smartphones if (!device.HasValue && HasAndroidMobileFragment()) { device = DeviceType.DEVICE_TYPE_SMARTPHONE; } //Android up to 3.0 was designed for smartphones only. But as 3.0, which was tablet only, was published // too late, there were a bunch of tablets running with 2.x // With 4.0 the two trees were merged and it is for smartphones and tablets // //So were are expecting that all devices running Android < 2 are smartphones // Devices running Android 3.X are tablets.Device type of Android 2.X and 4.X + are unknown if (!device.HasValue && osShortName == "AND" && osVersion != string.Empty) { if (System.Version.TryParse(osVersion, out _) && new System.Version(osVersion).CompareTo(new System.Version("2.0")) == -1) { device = DeviceType.DEVICE_TYPE_SMARTPHONE; } else if (System.Version.TryParse(osVersion, out _) && new System.Version(osVersion).CompareTo(new System.Version("3.0")) >= 0 && new System.Version(osVersion).CompareTo(new System.Version("4.0")) == -1) { device = DeviceType.DEVICE_TYPE_TABLET; } } //All detected feature phones running android are more likely a smartphone if (device == DeviceType.DEVICE_TYPE_FEATURE_PHONE && osFamily == "Android") { device = DeviceType.DEVICE_TYPE_SMARTPHONE; } //According to http://msdn.microsoft.com/en-us/library/ie/hh920767(v=vs.85).aspx //Internet Explorer 10 introduces the "Touch" UA string token. If this token is present at the end of the //UA string, the computer has touch capability, and is running Windows 8(or later). // // This UA string will be transmitted on a touch - enabled system running Windows 8(RT) // //As most touch enabled devices are tablets and only a smaller part are desktops / notebooks we assume that //all Windows 8 touch devices are tablets. if (!device.HasValue && (osShortName == "WRT" || (osShortName == "WIN" && System.Version.TryParse(osVersion, out _) && new System.Version(osVersion).CompareTo(new System.Version("8.0")) >= 0)) && IsTouchEnabled()) { device = DeviceType.DEVICE_TYPE_TABLET; } //All devices running Opera TV Store are assumed to be a tv if (IsMatchUserAgent("Opera TV Store")) { device = DeviceType.DEVICE_TYPE_TV; } //Devices running Kylo or Espital TV Browsers are assumed to be a TV if (!device.HasValue && (clientName == "Kylo" || clientName == "Espial TV Browser")) { device = DeviceType.DEVICE_TYPE_TV; } //set device type to desktop for all devices running a desktop os that were not detected as an other device type if (!device.HasValue && IsDesktop()) { device = DeviceType.DEVICE_TYPE_DESKTOP; } }
/// <summary> /// Parse Device. /// </summary> protected void ParseDevice() { foreach (var deviceParser in this.deviceParsers) { deviceParser.SetCache(this.cache); deviceParser.SetUserAgent(this.userAgent); if (deviceParser.ParserName == "tv") { var parser = (HbbTvParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "notebook") { var parser = (NotebookParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "consoles") { var parser = (ConsoleParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "car browser") { var parser = (CarBrowserParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "camera") { var parser = (CameraParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "portablemediaplayer") { var parser = (PortableMediaPlayerParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } if (deviceParser.ParserName == "mobiles") { var parser = (MobileParser)deviceParser; var result = parser.Parse(); if (result.Success) { this.device = result.Match.Type; this.model = result.Match.Name; this.brand = result.Match.Brand; break; } } } if (string.IsNullOrEmpty(this.brand)) { var vendorParser = new VendorFragmentParser(); vendorParser.SetUserAgent(this.userAgent); vendorParser.SetCache(this.cache); var result = vendorParser.Parse(); if (result.Success) { this.brand = result.Match.Brand; } } this.os = this.GetOs(); var osShortName = string.Empty; var osFamily = string.Empty; var osVersion = string.Empty; if (this.os.Success) { osShortName = this.os.Match.ShortName; OperatingSystemParser.GetOsFamily(osShortName, out osFamily); osVersion = this.os.Match.Version; if (!string.IsNullOrEmpty(osVersion)) { osVersion = !osVersion.Contains(".") ? osVersion + ".0" : osVersion; } } this.client = this.GetClient(); var clientName = this.client.Success ? this.client.Match.Name : string.Empty; if (string.IsNullOrEmpty(this.brand) && new[] { "ATV", "IOS", "MAC" }.Contains(osShortName)) { this.brand = "AP"; } if (!this.device.HasValue && osFamily == "Android" && (clientName == "Chrome" || clientName == "Chrome Mobile")) { if (this.IsMatchUserAgent(@"Chrome/[\.0-9]* Mobile")) { this.device = DeviceType.SMARTPHONE; } else if (this.IsMatchUserAgent(@"Chrome/[\.0-9]* (?!Mobile)")) { this.device = DeviceType.TABLET; } } if (!this.device.HasValue && (this.HasAndroidTableFragment() || this.IsMatchUserAgent("Opera Tablet"))) { this.device = DeviceType.TABLET; } if (!this.device.HasValue && this.HasAndroidMobileFragment()) { this.device = DeviceType.SMARTPHONE; } if (!this.device.HasValue && osShortName == "AND" && osVersion != string.Empty) { if (System.Version.TryParse(osVersion, out _) && new System.Version(osVersion).CompareTo(new System.Version("2.0")) == -1) { this.device = DeviceType.SMARTPHONE; } else if (System.Version.TryParse(osVersion, out _) && new System.Version(osVersion).CompareTo(new System.Version("3.0")) >= 0 && new System.Version(osVersion).CompareTo(new System.Version("4.0")) == -1) { this.device = DeviceType.TABLET; } } if (this.device == DeviceType.FEATUREPHONE && osFamily == "Android") { this.device = DeviceType.SMARTPHONE; } if (!this.device.HasValue && (osShortName == "WRT" || (osShortName == "WIN" && System.Version.TryParse(osVersion, out _) && new System.Version(osVersion).CompareTo(new System.Version("8.0")) >= 0)) && this.IsTouchEnabled()) { this.device = DeviceType.TABLET; } if (this.IsMatchUserAgent("Opera TV Store")) { this.device = DeviceType.TV; } if (!this.device.HasValue && (clientName == "Kylo" || clientName == "Espial TV Browser")) { this.device = DeviceType.TV; } if (!this.device.HasValue && this.IsDesktop()) { this.device = DeviceType.DESKTOP; } }