Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ANR getting default user agent #21681

Draft
wants to merge 13 commits into
base: trunk
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplified into single routine
nbradbury committed Feb 12, 2025
commit 6c9d8740f6cc8e5a4b4be21575790f5edab0880e
Original file line number Diff line number Diff line change
@@ -5,10 +5,8 @@ import android.webkit.WebSettings
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.wordpress.android.util.PackageUtils

@SuppressWarnings("SwallowedException", "TooGenericExceptionCaught")
class UserAgent(appContext: Context, appName: String) {
var userAgent: String

@@ -20,22 +18,16 @@ class UserAgent(appContext: Context, appName: String) {
// then get the default user agent on a separate thread - this can cause an ANR on the main thread
// see: https://github.com/wordpress-mobile/WordPress-Android/issues/21679#issuecomment-2654510229
CoroutineScope(Dispatchers.Default).launch {
getDefaultUserAgent(appContext)?.let {
userAgent = "$it $appWithVersion"
}
}
}

private suspend fun getDefaultUserAgent(appContext: Context): String? {
withContext(Dispatchers.IO) {
try {
return@withContext WebSettings.getDefaultUserAgent(appContext)
val defaultUserAgent = WebSettings.getDefaultUserAgent(appContext)
if (defaultUserAgent.isNotEmpty()) {
userAgent = "$defaultUserAgent $appWithVersion"
}
} catch (e: RuntimeException) {
// `getDefaultUserAgent()` can throw an Exception
// see: https://github.com/wordpress-mobile/WordPress-Android/issues/20147#issuecomment-1961238187
}
}
return null
}

override fun toString(): String = userAgent