From ea8dc45796413399b3af06b67db9ef7eecfaf2ff Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Fri, 30 Sep 2022 17:08:35 +0300 Subject: [PATCH] Parse browser version. --- .../settings/devices/v2/DeviceExtendedInfo.kt | 4 -- .../devices/v2/ParseDeviceUserAgentUseCase.kt | 44 +++++++++++++++---- .../v2/ParseDeviceUserAgentUseCaseTest.kt | 22 +++++----- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DeviceExtendedInfo.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DeviceExtendedInfo.kt index c9d27d093b..24e4606ca7 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/DeviceExtendedInfo.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/DeviceExtendedInfo.kt @@ -39,8 +39,4 @@ data class DeviceExtendedInfo( * i.e. 1.5.0. */ val clientVersion: String? = null, - /** - * i.e. Chrome. - */ - val browser: String? = null, ) diff --git a/vector/src/main/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCase.kt b/vector/src/main/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCase.kt index 82c6edcbdf..f5f1782d82 100644 --- a/vector/src/main/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCase.kt @@ -74,21 +74,38 @@ class ParseDeviceUserAgentUseCase @Inject constructor() { private fun parseDesktopUserAgent(userAgent: String): DeviceExtendedInfo { val browserSegments = userAgent.split(" ") - val browserName = when { + val (browserName, browserVersion) = when { isFirefox(browserSegments) -> { - "Firefox" + Pair("Firefox", getBrowserVersion(browserSegments, "Firefox")) } isEdge(browserSegments) -> { - "Edge" + Pair("Edge", getBrowserVersion(browserSegments, "Edge")) } isMobile(browserSegments) -> { - getMobileBrowserName(browserSegments) + when (val name = getMobileBrowserName(browserSegments)) { + null -> { + Pair(null, null) + } + "Safari" -> { + Pair(name, getBrowserVersion(browserSegments, "Version")) + } + else -> { + Pair(name, getBrowserVersion(browserSegments, name)) + } + } } isSafari(browserSegments) -> { - "Safari" + Pair("Safari", getBrowserVersion(browserSegments, "Version")) } else -> { - getRegularBrowserName(browserSegments) + when (val name = getRegularBrowserName(browserSegments)) { + null -> { + Pair(null, null) + } + else -> { + Pair(name, getBrowserVersion(browserSegments, name)) + } + } } } @@ -102,9 +119,8 @@ class ParseDeviceUserAgentUseCase @Inject constructor() { deviceType = DeviceType.DESKTOP, deviceModel = null, deviceOperatingSystem = deviceOperatingSystem, - clientName = null, - clientVersion = null, - browser = browserName + clientName = browserName, + clientVersion = browserVersion, ) } @@ -122,6 +138,16 @@ class ParseDeviceUserAgentUseCase @Inject constructor() { return browserSegments.lastOrNull()?.startsWith("Firefox").orFalse() } + private fun getBrowserVersion(browserSegments: List, browserName: String): String? { + // Chrome/104.0.3497.100 -> 104 + return browserSegments + .find { it.startsWith(browserName) } + ?.split("/") + ?.getOrNull(1) + ?.split(".") + ?.firstOrNull() + } + private fun isEdge(browserSegments: List): Boolean { return browserSegments.lastOrNull()?.startsWith("Edge").orFalse() } diff --git a/vector/src/test/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCaseTest.kt index 11f319f472..22a5a7614f 100644 --- a/vector/src/test/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/settings/devices/v2/ParseDeviceUserAgentUseCaseTest.kt @@ -61,8 +61,8 @@ private val A_USER_AGENT_LIST_FOR_DESKTOP = listOf( "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) ElementNightly/2022091301 Chrome/104.0.5112.102 Electron/20.1.1 Safari/537.36", ) private val AN_EXPECTED_RESULT_LIST_FOR_DESKTOP = listOf( - DeviceExtendedInfo(DeviceType.DESKTOP, null, "Macintosh", null, null, "Electron"), - DeviceExtendedInfo(DeviceType.DESKTOP, null, "Windows NT 10.0", null, null, "Electron"), + DeviceExtendedInfo(DeviceType.DESKTOP, null, "Macintosh", "Electron", "20"), + DeviceExtendedInfo(DeviceType.DESKTOP, null, "Windows NT 10.0", "Electron", "20"), ) private val A_USER_AGENT_LIST_FOR_WEB = listOf( @@ -77,15 +77,15 @@ private val A_USER_AGENT_LIST_FOR_WEB = listOf( "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( - DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", null, null, "Chrome"), - DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", null, null, "Chrome"), - DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", null, null, "Firefox"), - DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", null, null, "Safari"), - DeviceExtendedInfo(DeviceType.WEB, null, "Android 9", null, null, "Chrome"), - DeviceExtendedInfo(DeviceType.WEB, null, "iPad", null, null, "Safari"), - DeviceExtendedInfo(DeviceType.WEB, null, "iPhone", null, null, "Safari"), - DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 6.0", null, null, "Firefox"), - DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", null, null, "Edge"), + DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", "Chrome", "104"), + DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", "Chrome", "104"), + DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", "Firefox", "39"), + DeviceExtendedInfo(DeviceType.WEB, null, "Macintosh", "Safari", "8"), + DeviceExtendedInfo(DeviceType.WEB, null, "Android 9", "Chrome", "69"), + DeviceExtendedInfo(DeviceType.WEB, null, "iPad", "Safari", "8"), + DeviceExtendedInfo(DeviceType.WEB, null, "iPhone", "Safari", "8"), + DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 6.0", "Firefox", "40"), + DeviceExtendedInfo(DeviceType.WEB, null, "Windows NT 10.0", "Edge", "12"), ) private val AN_UNKNOWN_USER_AGENT_LIST = listOf(