Exemple #1
0
        public void VendorFragmentTestParse()
        {
            var vendorFragmentParser = new VendorFragmentParser();

            foreach (var fixture in _fixtureData)
            {
                vendorFragmentParser.SetUserAgent(fixture.useragent);
                var result = vendorFragmentParser.Parse();
                result.Success.Should().BeTrue("Match should be with success");

                result.Match.Brand.Should().BeEquivalentTo(fixture.vendor, "Brands should be equal");
            }
        }
        /// <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;
            }
        }