Skip to content

Commit

Permalink
Detect Samsung devices. (#496)
Browse files Browse the repository at this point in the history
Close #494.
  • Loading branch information
fnando committed Oct 3, 2020
1 parent f9f6908 commit 40f5900
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add Samsung device detection.
- Delay parsing `Accept-Language` until `Browser::Base#accept_language` is
called for the first time.
- Bump up default size limit for `Accept-Language` and `User-Agent` to 2048
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ browser.device.tv?
browser.device.vita?
browser.device.wii?
browser.device.wiiu?
browser.device.samsung?
browser.device.switch?
browser.device.xbox?
browser.device.xbox_360?
Expand Down
7 changes: 7 additions & 0 deletions lib/browser/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_relative "device/tv"
require_relative "device/xbox_one"
require_relative "device/xbox_360"
require_relative "device/samsung"

module Browser
class Device
Expand All @@ -29,6 +30,7 @@ class Device
# Order is important.
def self.matchers
@matchers ||= [
Samsung,
XboxOne,
Xbox360,
Surface,
Expand Down Expand Up @@ -191,6 +193,11 @@ def console?
xbox? || playstation? || nintendo?
end

# Detect if device is a Samsung.
def samsung?
id == :samsung
end

# Regex taken from http://detectmobilebrowsers.com
# rubocop:disable Layout/LineLength
private def detect_mobile?
Expand Down
33 changes: 33 additions & 0 deletions lib/browser/device/samsung.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Browser
class Device
class Samsung < Base
REGEX = /\(Linux.*?; Android.*?; (SAMSUNG )?(SM-[A-Z0-9]+).*?\)/i.freeze

def self.names
@names ||= YAML.load_file(Browser.root.join("samsung.yml").to_s)
end

def id
:samsung
end

def name
"Samsung #{self.class.names[code] || code}"
end

def code
matches && matches[2]
end

def matches
@matches ||= ua.match(REGEX)
end

def match?
!!matches
end
end
end
end
138 changes: 138 additions & 0 deletions samsung.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Source: https://en.wikipedia.org/wiki/Samsung_Galaxy
SM-G900H: Galaxy S5 Exynos
SM-A320FL: Galaxy A3
SM-G780F: Galaxy S20 Fan Edition
SM-G781B: Galaxy S20 Fan Edition
SM-F196B: Galaxy Z Fold2 5G
SM-G928A: Galaxy S6 Edge+
SM-G928AZ: Galaxy S6 Edge+
SM-G928D: Galaxy S6 Edge+
SM-G928F: Galaxy S6 Edge+
SM-G928FD: Galaxy S6 Edge+
SM-G928I: Galaxy S6 Edge+
SM-G928K: Galaxy S6 Edge+
SM-G928L: Galaxy S6 Edge+
SM-G928P: Galaxy S6 Edge+
SM-G928PZ: Galaxy S6 Edge+
SM-G928R4: Galaxy S6 Edge+
SM-G928R7: Galaxy S6 Edge+
SM-G928S: Galaxy S6 Edge+
SM-G928T: Galaxy S6 Edge+
SM-G928T1: Galaxy S6 Edge+
SM-G928TR: Galaxy S6 Edge+
SM-G928V: Galaxy S6 Edge+
SM-G9280: Galaxy S6 Edge+
SM-G9288: Galaxy S6 Edge+
SM-G9289: Galaxy S6 Edge+
SM-A8000: Galaxy A8
SM-A800F: Galaxy A8
SM-A800I: Galaxy A8
SM-A800S: Galaxy A8
SM-A800Y: Galaxy A8
SM-N9200: Galaxy Note 5
SM-N920C: Galaxy Note 5
SM-N920T: Galaxy Note 5
SM-N920A: Galaxy Note 5
SM-N920I: Galaxy Note 5
SM-N9208: Galaxy Note 5
SM-G903FP: Galaxy S5 Neo
SM-G903WP: Galaxy S5 Neo
SM-G925A: Galaxy S6 Edge
SM-G925AZ: Galaxy S6 Edge
SM-G925F: Galaxy S6 Edge
SM-G925I: Galaxy S6 Edge
SM-G925K: Galaxy S6 Edge
SM-G925L: Galaxy S6 Edge
SM-G925P: Galaxy S6 Edge
SM-G925PZ: Galaxy S6 Edge
SM-G925R4: Galaxy S6 Edge
SM-G925R7: Galaxy S6 Edge
SM-G925S: Galaxy S6 Edge
SM-G925T: Galaxy S6 Edge
SM-G925T1: Galaxy S6 Edge
SM-G925TR: Galaxy S6 Edge
SM-G925V: Galaxy S6 Edge
SM-G9250: Galaxy S6 Edge
SM-G9258: Galaxy S6 Edge
SM-G9259: Galaxy S6 Edge
SM-G920A: Galaxy S6
SM-G920AZ: Galaxy S6
SM-G920D: Galaxy S6
SM-G920F: Galaxy S6
SM-G920FD: Galaxy S6
SM-G920I: Galaxy S6
SM-G920K: Galaxy S6
SM-G920L: Galaxy S6
SM-G920P: Galaxy S6
SM-G920PZ: Galaxy S6
SM-G920R4: Galaxy S6
SM-G920R7: Galaxy S6
SM-G920S: Galaxy S6
SM-G920T: Galaxy S6
SM-G920T1: Galaxy S6
SM-G920TR: Galaxy S6
SM-G920V: Galaxy S6
SM-G9200: Galaxy S6
SM-G9208: Galaxy S6
SM-G9209: Galaxy S6
SM-A700F: Galaxy A7
SM-A700FD: Galaxy A7
SM-A700FQ: Galaxy A7
SM-A700H: Galaxy A7
SM-A700K: Galaxy A7
SM-A700L: Galaxy A7
SM-A700M: Galaxy A7
SM-A700S: Galaxy A7
SM-A700X: Galaxy A7
SM-A700YD: Galaxy A7
SM-A700YZ: Galaxy A7
SM-A7000: Galaxy A7
SM-A7009: Galaxy A7
SM-A7009W: Galaxy A7
SM-G530BT: Galaxy Grand Prime
SM-G530F: Galaxy Grand Prime
SM-G530FQ: Galaxy Grand Prime
SM-G530FZ: Galaxy Grand Prime
SM-G530H: Galaxy Grand Prime
SM-G530M: Galaxy Grand Prime
SM-G530MU: Galaxy Grand Prime
SM-G530P: Galaxy Grand Prime
SM-G530R4: Galaxy Grand Prime
SM-G530R7: Galaxy Grand Prime
SM-G530T: Galaxy Grand Prime
SM-G530W: Galaxy Grand Prime
SM-G530Y: Galaxy Grand Prime
SM-G5306W: Galaxy Grand Prime
SM-G5308W: Galaxy Grand Prime
SM-G5309W: Galaxy Grand Prime
SM-J7109: Galaxy J7
SM-J710F: Galaxy J7
SM-J710FN: Galaxy J7
SM-J710H: Galaxy J7
SM-J710MN: Galaxy J7
SM-J710FQ: Galaxy J7
SM-J710K: Galaxy J7
SM-J710K: Galaxy J7
SM-J710GN: Galaxy J7
SM-J5109: Galaxy J5
SM-J510F: Galaxy J5
SM-J510FN: Galaxy J5
SM-J510H: Galaxy J5
SM-J510G: Galaxy J5
SM-J510MN: Galaxy J5
SM-J510Y: Galaxy J5
SM-J5108: Galaxy J5
SM-J510K: Galaxy J5
SM-J510L: Galaxy J5
SM-J510S: Galaxy J5
SM-J510UN: Galaxy J5
SM-G570F: Galaxy J2 Prime
SM-G570M: Galaxy J2 Prime
SM-G532F: Galaxy J2 Prime
SM-G532M: Galaxy J2 Prime
SM-G532G: Galaxy J2 Prime
SM-G610F: Galaxy J7 Prime
SM-G610M: Galaxy J7 Prime
SM-G975F: Galaxy S10+
SM-G960F: Galaxy S9
SM-F700F: Galaxy Z Flip
4 changes: 4 additions & 0 deletions test/ua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ SAFARI_IPHONE_WEBAPP_MODE: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_2 like Mac O
SAMSUNG: "Mozilla/5.0 (Linux; U; Android 4.0.4; en-us; SAMSUNG-SGH-I497 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30"
SAMSUNG_BROWSER: "Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-N960U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/11.1 Chrome/75.0.3770.143 Mobile Safari/537.36"
SAMSUNG_CHROME: "Mozilla/5.0 (Linux; Android 4.4.2; en-gb; SAMSUNG GT-I9195/I9195XXUCNEA Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/1.5 Chrome/28.0.1500.94 Mobile Safari/537.36"
SAMSUNG_SM-G975F: "Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.101 Mobile Safari/537.36"
SAMSUNG_SM-G960F: "Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Mobile Safari/537.36"
SAMSUNG_SM-F700F: "Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-F700F) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/11.1 Chrome/75.0.3770.143 Mobile Safari/537.36"
SAMSUNG_SM-FAKE: "Mozilla/5.0 (Linux; Android 8.0.0; SM-0000) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Mobile Safari/537.36"
SMART_TV: "Mozilla/5.0 (SmartHub; SMART-TV; U; Linux/SmartTV) AppleWebKit/531.2+ (KHTML, like Gecko) WebBrowser/1.0 SmartTV Safari/531.2+"
SNAPCHAT: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Snapchat/10.69.5.72 (iPhone10,3; iOS 13.2.2; gzip)
SNAPCHAT_EMPTY_STRING_VERSION: "Mozilla/5.0 (Linux; Android 9; SM-N960U Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.96 Mobile Safari/537.36Snapchat10.70.0.0 (SM-N960U; Android 9#N960USQS3CSJ2#28; gzip)"
Expand Down
27 changes: 27 additions & 0 deletions test/unit/device_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,31 @@ def id
assert_equal name, device.name
end
end

test "detect samsung devices" do
device = Browser::Device.new(Browser["SAMSUNG_SM-G975F"])

assert device.samsung?
assert_equal :samsung, device.id
assert_equal "Samsung Galaxy S10+", device.name
end

test "detect generic samsung devices" do
device = Browser::Device.new(Browser["SAMSUNG_SM-FAKE"])

assert device.samsung?
assert_equal :samsung, device.id
assert_equal "Samsung SM-0000", device.name
end

{
"SAMSUNG_SM-G975F" => "Samsung Galaxy S10+",
"SAMSUNG_SM-G960F" => "Samsung Galaxy S9",
"SAMSUNG_SM-F700F" => "Samsung Galaxy Z Flip"
}.each do |key, name|
test "detect device name of #{key} as #{name}" do
device = Browser::Device.new(Browser[key])
assert_equal name, device.name
end
end
end

0 comments on commit 40f5900

Please sign in to comment.