Add more user agent support.

This commit is contained in:
Onuray Sahin 2022-09-29 11:36:38 +03:00
parent 7a36b10d40
commit c16b5d674c
2 changed files with 26 additions and 3 deletions

View file

@ -37,7 +37,7 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
private fun parseAndroidUserAgent(userAgent: String): DeviceUserAgent {
val appName = userAgent.substringBefore("/")
val appVersion = userAgent.substringAfter("/").substringBefore(" (")
val deviceInfoSegments = userAgent.substringAfter("(").substringBefore(")").split("; ")
val deviceInfoSegments = userAgent.substringAfter("(").substringBeforeLast(")").split("; ")
val deviceModel: String?
val deviceOperatingSystem: String?
if (deviceInfoSegments.firstOrNull() == "Linux") {
@ -54,7 +54,7 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
private fun parseIosUserAgent(userAgent: String): DeviceUserAgent {
val appName = userAgent.substringBefore("/")
val appVersion = userAgent.substringAfter("/").substringBefore(" (")
val deviceInfoSegments = userAgent.substringAfter("(").substringBefore(")").split("; ")
val deviceInfoSegments = userAgent.substringAfter("(").substringBeforeLast(")").split("; ")
val deviceModel = deviceInfoSegments.getOrNull(0)
val deviceOperatingSystem = deviceInfoSegments.getOrNull(1)
return DeviceUserAgent(DeviceType.MOBILE, deviceModel, deviceOperatingSystem, appName, appVersion)
@ -66,6 +66,9 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
isFirefox(browserSegments) -> {
"Firefox"
}
isEdge(browserSegments) -> {
"Edge"
}
isMobile(browserSegments) -> {
getMobileBrowserName(browserSegments)
}
@ -100,6 +103,10 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
return browserSegments.lastOrNull()?.startsWith("Firefox").orFalse()
}
private fun isEdge(browserSegments: List<String>): Boolean {
return browserSegments.lastOrNull()?.startsWith("Edge").orFalse()
}
private fun isSafari(browserSegments: List<String>): Boolean {
return browserSegments.lastOrNull()?.startsWith("Safari").orFalse() &&
browserSegments.getOrNull(browserSegments.size - 2)?.startsWith("Version").orFalse()
@ -107,7 +114,7 @@ class ParseDeviceUserAgentUseCase @Inject constructor() {
private fun isMobile(browserSegments: List<String>): Boolean {
return browserSegments.lastOrNull()?.startsWith("Safari").orFalse() &&
browserSegments.getOrNull(browserSegments.size - 2) == "Mobile"
browserSegments.getOrNull(browserSegments.size - 2)?.startsWith("Mobile").orFalse()
}
private fun getMobileBrowserName(browserSegments: List<String>): String? {

View file

@ -25,6 +25,8 @@ private val A_USER_AGENT_LIST_FOR_ANDROID = listOf(
"Element dbg/1.5.0-dev (Xiaomi Mi 9T; Android 11; RKQ1.200826.002 test-keys; Flavour GooglePlay; MatrixAndroidSdk2 1.5.2)",
"Element/1.5.0 (Samsung SM-G960F; Android 6.0.1; RKQ1.200826.002; Flavour FDroid; MatrixAndroidSdk2 1.5.2)",
"Element/1.5.0 (Google Nexus 5; Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)",
"Element/1.5.0 (Google (Nexus) 5; Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)",
"Element/1.5.0 (Google (Nexus) (5); Android 7.0; RKQ1.200826.002 test test; Flavour FDroid; MatrixAndroidSdk2 1.5.2)",
// Legacy User Agent Implementation
"Element/1.0.0 (Linux; U; Android 6.0.1; SM-A510F Build/MMB29; Flavour GPlay; MatrixAndroidSdk2 1.0)",
"Element/1.0.0 (Linux; Android 7.0; SM-G610M Build/NRD90M; Flavour GPlay; MatrixAndroidSdk2 1.0)",
@ -33,6 +35,8 @@ private val AN_EXPECTED_RESULT_LIST_FOR_ANDROID = listOf(
DeviceUserAgent(DeviceType.MOBILE, "Xiaomi Mi 9T", "Android 11", "Element dbg", "1.5.0-dev"),
DeviceUserAgent(DeviceType.MOBILE, "Samsung SM-G960F", "Android 6.0.1", "Element", "1.5.0"),
DeviceUserAgent(DeviceType.MOBILE, "Google Nexus 5", "Android 7.0", "Element", "1.5.0"),
DeviceUserAgent(DeviceType.MOBILE, "Google (Nexus) 5", "Android 7.0", "Element", "1.5.0"),
DeviceUserAgent(DeviceType.MOBILE, "Google (Nexus) (5)", "Android 7.0", "Element", "1.5.0"),
DeviceUserAgent(DeviceType.MOBILE, "SM-A510F Build/MMB29", "Android 6.0.1", "Element", "1.0.0"),
DeviceUserAgent(DeviceType.MOBILE, "SM-G610M Build/NRD90M", "Android 7.0", "Element", "1.0.0"),
)
@ -40,10 +44,14 @@ private val AN_EXPECTED_RESULT_LIST_FOR_ANDROID = listOf(
private val A_USER_AGENT_LIST_FOR_IOS = listOf(
"Element/1.8.21 (iPhone; iOS 15.2; Scale/3.00)",
"Element/1.8.21 (iPhone XS Max; iOS 15.2; Scale/3.00)",
"Element/1.8.21 (iPad Pro (11-inch); iOS 15.2; Scale/3.00)",
"Element/1.8.21 (iPad Pro (12.9-inch) (3rd generation); iOS 15.2; Scale/3.00)",
)
private val AN_EXPECTED_RESULT_LIST_FOR_IOS = listOf(
DeviceUserAgent(DeviceType.MOBILE, "iPhone", "iOS 15.2", "Element", "1.8.21"),
DeviceUserAgent(DeviceType.MOBILE, "iPhone XS Max", "iOS 15.2", "Element", "1.8.21"),
DeviceUserAgent(DeviceType.MOBILE, "iPad Pro (11-inch)", "iOS 15.2", "Element", "1.8.21"),
DeviceUserAgent(DeviceType.MOBILE, "iPad Pro (12.9-inch) (3rd generation)", "iOS 15.2", "Element", "1.8.21"),
)
private val A_USER_AGENT_LIST_FOR_DESKTOP = listOf(
@ -61,6 +69,10 @@ private val A_USER_AGENT_LIST_FOR_WEB = listOf(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/600.3.18 (KHTML, like Gecko) Version/8.0.3 Safari/600.3.18",
"Mozilla/5.0 (Linux; Android 9; SM-G973U Build/PPR1.180610.011) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36",
"Mozilla/5.0 (iPad; CPU OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4",
"Mozilla/5.0 (iPhone; CPU iPhone OS 8_4_1 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H321 Safari/600.1.4",
"Mozilla/5.0 (Windows NT 6.0; rv:40.0) Gecko/20100101 Firefox/40.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246",
)
private val AN_EXPECTED_RESULT_LIST_FOR_WEB = listOf(
DeviceUserAgent(DeviceType.WEB, "Chrome", "Macintosh", null, null),
@ -68,6 +80,10 @@ private val AN_EXPECTED_RESULT_LIST_FOR_WEB = listOf(
DeviceUserAgent(DeviceType.WEB, "Firefox", "Macintosh", null, null),
DeviceUserAgent(DeviceType.WEB, "Safari", "Macintosh", null, null),
DeviceUserAgent(DeviceType.WEB, "Chrome", "Android 9", null, null),
DeviceUserAgent(DeviceType.WEB, "Safari", "iPad", null, null),
DeviceUserAgent(DeviceType.WEB, "Safari", "iPhone", null, null),
DeviceUserAgent(DeviceType.WEB, "Firefox", "Windows NT 6.0", null, null),
DeviceUserAgent(DeviceType.WEB, "Edge", "Windows NT 10.0", null, null),
)
class ParseDeviceUserAgentUseCaseTest {