Extend user agent by adding device manufacturer and model to the beginning.
This commit is contained in:
parent
e98bfe5c9b
commit
b2e7cc2208
@ -17,10 +17,11 @@
|
|||||||
package org.matrix.android.sdk.internal.network
|
package org.matrix.android.sdk.internal.network
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
import org.matrix.android.sdk.BuildConfig
|
import org.matrix.android.sdk.BuildConfig
|
||||||
import org.matrix.android.sdk.api.MatrixConfiguration
|
import org.matrix.android.sdk.api.MatrixConfiguration
|
||||||
|
import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||||
import org.matrix.android.sdk.internal.di.MatrixScope
|
import org.matrix.android.sdk.internal.di.MatrixScope
|
||||||
import timber.log.Timber
|
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
@MatrixScope
|
@MatrixScope
|
||||||
@ -38,51 +39,55 @@ internal class UserAgentHolder @Inject constructor(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an user agent with the application version.
|
* Create an user agent with the application version.
|
||||||
* Ex: Element/1.0.0 (Linux; U; Android 6.0.1; SM-A510F Build/MMB29; Flavour GPlay; MatrixAndroidSdk2 1.0)
|
* Ex: Element/1.5.0 (Xiaomi; Mi 9T; Android 11; RKQ1.200826.002; Flavour GooglePlay; MatrixAndroidSdk2 1.5.0)
|
||||||
*
|
*
|
||||||
* @param flavorDescription the flavor description
|
* @param flavorDescription the flavor description
|
||||||
*/
|
*/
|
||||||
private fun setApplicationFlavor(flavorDescription: String) {
|
private fun setApplicationFlavor(flavorDescription: String) {
|
||||||
var appName = ""
|
val appPackageName = context.applicationContext.packageName
|
||||||
var appVersion = ""
|
val pm = context.packageManager
|
||||||
|
|
||||||
try {
|
val appName = tryOrNull { pm.getApplicationLabel(pm.getApplicationInfo(appPackageName, 0)).toString() }
|
||||||
val appPackageName = context.applicationContext.packageName
|
?.takeIf {
|
||||||
val pm = context.packageManager
|
it.matches("\\A\\p{ASCII}*\\z".toRegex())
|
||||||
val appInfo = pm.getApplicationInfo(appPackageName, 0)
|
}
|
||||||
appName = pm.getApplicationLabel(appInfo).toString()
|
?: run {
|
||||||
|
// Use appPackageName instead of appName if appName contains any non-ASCII character
|
||||||
|
appPackageName
|
||||||
|
}
|
||||||
|
val appVersion = tryOrNull { pm.getPackageInfo(context.applicationContext.packageName, 0).versionName }
|
||||||
|
|
||||||
val pkgInfo = pm.getPackageInfo(context.applicationContext.packageName, 0)
|
val deviceManufacturer = Build.MANUFACTURER
|
||||||
appVersion = pkgInfo.versionName ?: ""
|
val deviceModel = Build.MODEL
|
||||||
|
val androidVersion = Build.VERSION.RELEASE
|
||||||
|
val deviceBuildId = Build.DISPLAY
|
||||||
|
val matrixSdkVersion = BuildConfig.SDK_VERSION
|
||||||
|
|
||||||
// Use appPackageName instead of appName if appName contains any non-ASCII character
|
if (appName.isNullOrEmpty() || appVersion.isNullOrEmpty()) {
|
||||||
if (!appName.matches("\\A\\p{ASCII}*\\z".toRegex())) {
|
userAgent = tryOrNull { System.getProperty("http.agent") } ?: ("Java" + System.getProperty("java.version"))
|
||||||
appName = appPackageName
|
|
||||||
}
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Timber.e(e, "## initUserAgent() : failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
val systemUserAgent = System.getProperty("http.agent")
|
|
||||||
|
|
||||||
// cannot retrieve the application version
|
|
||||||
if (appName.isEmpty() || appVersion.isEmpty()) {
|
|
||||||
if (null == systemUserAgent) {
|
|
||||||
userAgent = "Java" + System.getProperty("java.version")
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there is no user agent or cannot parse it
|
userAgent = buildString {
|
||||||
if (null == systemUserAgent || systemUserAgent.lastIndexOf(")") == -1 || !systemUserAgent.contains("(")) {
|
append(appName)
|
||||||
userAgent = (appName + "/" + appVersion + " ( Flavour " + flavorDescription +
|
append("/")
|
||||||
"; MatrixAndroidSdk2 " + BuildConfig.SDK_VERSION + ")")
|
append(appVersion)
|
||||||
} else {
|
append(" (")
|
||||||
// update
|
append(deviceManufacturer)
|
||||||
userAgent = appName + "/" + appVersion + " " +
|
append("; ")
|
||||||
systemUserAgent.substring(systemUserAgent.indexOf("("), systemUserAgent.lastIndexOf(")") - 1) +
|
append(deviceModel)
|
||||||
"; Flavour " + flavorDescription +
|
append("; ")
|
||||||
"; MatrixAndroidSdk2 " + BuildConfig.SDK_VERSION + ")"
|
append("Android ")
|
||||||
|
append(androidVersion)
|
||||||
|
append("; ")
|
||||||
|
append(deviceBuildId)
|
||||||
|
append("; ")
|
||||||
|
append("Flavour ")
|
||||||
|
append(flavorDescription)
|
||||||
|
append("; ")
|
||||||
|
append("MatrixAndroidSdk2 ")
|
||||||
|
append(matrixSdkVersion)
|
||||||
|
append(")")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user