@@ -2,45 +2,13 @@ package org.wordpress.android.fluxc.network
2
2
3
3
import android.content.Context
4
4
import android.webkit.WebSettings
5
- import kotlinx.coroutines.CoroutineDispatcher
6
- import kotlinx.coroutines.CoroutineScope
7
- import kotlinx.coroutines.Dispatchers
8
- import kotlinx.coroutines.launch
9
- import org.wordpress.android.util.AppLog
10
5
import org.wordpress.android.util.PackageUtils
11
6
12
- @Suppress(" MemberNameEqualsClassName" )
13
- class UserAgent @JvmOverloads constructor(
14
- private val appContext : Context ? ,
15
- private val appName : String ,
16
- bgDispatcher : CoroutineDispatcher = Dispatchers .Default
17
- ) {
18
- /* *
19
- * User-Agent string when making HTTP connections, for both API traffic and WebViews.
20
- * Appends "[appName]/version" to WebView's default User-Agent string for the webservers
21
- * to get the full feature list of the browser and serve content accordingly, e.g.:
22
- * "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86_64 Build/MASTER; wv)
23
- * AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36
24
- * wp-android/4.7"
25
- */
26
- var userAgent: String = getAppNameVersion()
27
- private set
28
-
29
- private val coroutineScope = CoroutineScope (bgDispatcher)
7
+ @SuppressWarnings(" SwallowedException" , " TooGenericExceptionCaught" , " MemberNameEqualsClassName" )
8
+ class UserAgent (appContext : Context ? , appName : String ) {
9
+ val userAgent: String
30
10
31
11
init {
32
- coroutineScope.launch {
33
- initUserAgent()
34
- }
35
- }
36
-
37
- /* *
38
- * Initializes the User-Agent string.
39
- * This method will be called asynchronously to avoid blocking the main thread,
40
- * because `WebSettings.getDefaultUserAgent()` can be slow.
41
- */
42
- @Suppress(" TooGenericExceptionCaught" , " SwallowedException" )
43
- private fun initUserAgent () {
44
12
// Device's default User-Agent string.
45
13
// E.g.:
46
14
// "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86_64 Build/MASTER; wv)
@@ -50,17 +18,17 @@ class UserAgent @JvmOverloads constructor(
50
18
} catch (e: RuntimeException ) {
51
19
// `getDefaultUserAgent()` can throw an Exception
52
20
// see: https://github.com/wordpress-mobile/WordPress-Android/issues/20147#issuecomment-1961238187
53
- AppLog .e(
54
- AppLog .T .UTILS ,
55
- " Error getting the user's default User-Agent, ${e.stackTraceToString()} "
56
- )
57
- return
21
+ " "
58
22
}
59
-
60
- userAgent = " $defaultUserAgent ${getAppNameVersion()} "
23
+ // User-Agent string when making HTTP connections, for both API traffic and WebViews.
24
+ // Appends "wp-android/version" to WebView's default User-Agent string for the webservers
25
+ // to get the full feature list of the browser and serve content accordingly, e.g.:
26
+ // "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86_64 Build/MASTER; wv)
27
+ // AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36
28
+ // wp-android/4.7"
29
+ val appWithVersion = " $appName /${PackageUtils .getVersionName(appContext)} "
30
+ userAgent = if (defaultUserAgent.isNotEmpty()) " $defaultUserAgent $appWithVersion " else appWithVersion
61
31
}
62
32
63
- private fun getAppNameVersion () = " $appName /${PackageUtils .getVersionName(appContext)} "
64
-
65
33
override fun toString (): String = userAgent
66
34
}
0 commit comments