Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit fabe483

Browse files
Update UserAgent to handle initialization on a background thread
1 parent e88dbd6 commit fabe483

File tree

1 file changed

+42
-12
lines changed
  • fluxc/src/main/java/org/wordpress/android/fluxc/network

1 file changed

+42
-12
lines changed

fluxc/src/main/java/org/wordpress/android/fluxc/network/UserAgent.kt

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,43 @@ package org.wordpress.android.fluxc.network
22

33
import android.content.Context
44
import android.webkit.WebSettings
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.launch
8+
import org.wordpress.android.util.AppLog
59
import org.wordpress.android.util.PackageUtils
610

7-
@SuppressWarnings("SwallowedException", "TooGenericExceptionCaught", "MemberNameEqualsClassName")
8-
class UserAgent(appContext: Context?, appName: String) {
9-
val userAgent: String
11+
@Suppress("MemberNameEqualsClassName")
12+
class UserAgent(
13+
private val appContext: Context?,
14+
private val appName: String
15+
) {
16+
/**
17+
* User-Agent string when making HTTP connections, for both API traffic and WebViews.
18+
* Appends "[appName]/version" to WebView's default User-Agent string for the webservers
19+
* to get the full feature list of the browser and serve content accordingly, e.g.:
20+
* "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86_64 Build/MASTER; wv)
21+
* AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36
22+
* wp-android/4.7"
23+
*/
24+
var userAgent: String = getAppNameVersion()
25+
private set
26+
27+
private val coroutineScope = CoroutineScope(Dispatchers.Default)
1028

1129
init {
30+
coroutineScope.launch {
31+
initUserAgent()
32+
}
33+
}
34+
35+
/**
36+
* Initializes the User-Agent string.
37+
* This method will be called asynchronously to avoid blocking the main thread,
38+
* because `WebSettings.getDefaultUserAgent()` can be slow.
39+
*/
40+
@Suppress("TooGenericExceptionCaught", "SwallowedException")
41+
private fun initUserAgent() {
1242
// Device's default User-Agent string.
1343
// E.g.:
1444
// "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86_64 Build/MASTER; wv)
@@ -18,17 +48,17 @@ class UserAgent(appContext: Context?, appName: String) {
1848
} catch (e: RuntimeException) {
1949
// `getDefaultUserAgent()` can throw an Exception
2050
// see: https://github.com/wordpress-mobile/WordPress-Android/issues/20147#issuecomment-1961238187
21-
""
51+
AppLog.e(
52+
AppLog.T.UTILS,
53+
"Error getting the user's default User-Agent, ${e.stackTraceToString()}"
54+
)
55+
return
2256
}
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
57+
58+
userAgent = "$defaultUserAgent ${getAppNameVersion()}"
3159
}
3260

61+
private fun getAppNameVersion() = "$appName/${PackageUtils.getVersionName(appContext)}"
62+
3363
override fun toString(): String = userAgent
3464
}

0 commit comments

Comments
 (0)