mirror of
https://github.com/element-hq/element-android
synced 2024-11-27 03:48:12 +03:00
Create use case to compute user agent.
This commit is contained in:
parent
c37a6842fe
commit
c7108f3ac3
2 changed files with 84 additions and 60 deletions
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.internal.network
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import org.matrix.android.sdk.BuildConfig
|
||||
import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||
import javax.inject.Inject
|
||||
|
||||
class ComputeUserAgentUseCase @Inject constructor() {
|
||||
|
||||
/**
|
||||
* Create an user agent with the application version.
|
||||
* Ex: Element/1.5.0 (Xiaomi Mi 9T; Android 11; RKQ1.200826.002; Flavour GooglePlay; MatrixAndroidSdk2 1.5.0)
|
||||
*
|
||||
* @param context the context
|
||||
* @param flavorDescription the flavor description
|
||||
*/
|
||||
fun execute(context: Context, flavorDescription: String): String {
|
||||
val appPackageName = context.applicationContext.packageName
|
||||
val pm = context.packageManager
|
||||
|
||||
val appName = tryOrNull { pm.getApplicationLabel(pm.getApplicationInfo(appPackageName, 0)).toString() }
|
||||
?.takeIf {
|
||||
it.matches("\\A\\p{ASCII}*\\z".toRegex())
|
||||
}
|
||||
?: run {
|
||||
// Use appPackageName instead of appName if appName contains any non-ASCII character
|
||||
appPackageName
|
||||
}
|
||||
val appVersion = tryOrNull { pm.getPackageInfo(context.applicationContext.packageName, 0).versionName }
|
||||
|
||||
if (appName.isNullOrEmpty() || appVersion.isNullOrEmpty()) {
|
||||
return tryOrNull { System.getProperty("http.agent") } ?: ("Java" + System.getProperty("java.version"))
|
||||
}
|
||||
|
||||
val deviceManufacturer = Build.MANUFACTURER
|
||||
val deviceModel = Build.MODEL
|
||||
val androidVersion = Build.VERSION.RELEASE
|
||||
val deviceBuildId = Build.DISPLAY
|
||||
val matrixSdkVersion = BuildConfig.SDK_VERSION
|
||||
|
||||
return buildString {
|
||||
append(appName)
|
||||
append("/")
|
||||
append(appVersion)
|
||||
append(" (")
|
||||
append(deviceManufacturer)
|
||||
append(" ")
|
||||
append(deviceModel)
|
||||
append("; ")
|
||||
append("Android ")
|
||||
append(androidVersion)
|
||||
append("; ")
|
||||
append(deviceBuildId)
|
||||
append("; ")
|
||||
append("Flavour ")
|
||||
append(flavorDescription)
|
||||
append("; ")
|
||||
append("MatrixAndroidSdk2 ")
|
||||
append(matrixSdkVersion)
|
||||
append(")")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,77 +17,21 @@
|
|||
package org.matrix.android.sdk.internal.network
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import org.matrix.android.sdk.BuildConfig
|
||||
import org.matrix.android.sdk.api.MatrixConfiguration
|
||||
import org.matrix.android.sdk.api.extensions.tryOrNull
|
||||
import org.matrix.android.sdk.internal.di.MatrixScope
|
||||
import javax.inject.Inject
|
||||
|
||||
@MatrixScope
|
||||
internal class UserAgentHolder @Inject constructor(
|
||||
private val context: Context,
|
||||
matrixConfiguration: MatrixConfiguration
|
||||
context: Context,
|
||||
matrixConfiguration: MatrixConfiguration,
|
||||
computeUserAgentUseCase: ComputeUserAgentUseCase,
|
||||
) {
|
||||
|
||||
var userAgent: String = ""
|
||||
private set
|
||||
|
||||
init {
|
||||
setApplicationFlavor(matrixConfiguration.applicationFlavor)
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an user agent with the application version.
|
||||
* Ex: Element/1.5.0 (Xiaomi Mi 9T; Android 11; RKQ1.200826.002; Flavour GooglePlay; MatrixAndroidSdk2 1.5.0)
|
||||
*
|
||||
* @param flavorDescription the flavor description
|
||||
*/
|
||||
private fun setApplicationFlavor(flavorDescription: String) {
|
||||
val appPackageName = context.applicationContext.packageName
|
||||
val pm = context.packageManager
|
||||
|
||||
val appName = tryOrNull { pm.getApplicationLabel(pm.getApplicationInfo(appPackageName, 0)).toString() }
|
||||
?.takeIf {
|
||||
it.matches("\\A\\p{ASCII}*\\z".toRegex())
|
||||
}
|
||||
?: run {
|
||||
// Use appPackageName instead of appName if appName contains any non-ASCII character
|
||||
appPackageName
|
||||
}
|
||||
val appVersion = tryOrNull { pm.getPackageInfo(context.applicationContext.packageName, 0).versionName }
|
||||
|
||||
if (appName.isNullOrEmpty() || appVersion.isNullOrEmpty()) {
|
||||
userAgent = tryOrNull { System.getProperty("http.agent") } ?: ("Java" + System.getProperty("java.version"))
|
||||
return
|
||||
}
|
||||
|
||||
val deviceManufacturer = Build.MANUFACTURER
|
||||
val deviceModel = Build.MODEL
|
||||
val androidVersion = Build.VERSION.RELEASE
|
||||
val deviceBuildId = Build.DISPLAY
|
||||
val matrixSdkVersion = BuildConfig.SDK_VERSION
|
||||
|
||||
userAgent = buildString {
|
||||
append(appName)
|
||||
append("/")
|
||||
append(appVersion)
|
||||
append(" (")
|
||||
append(deviceManufacturer)
|
||||
append(" ")
|
||||
append(deviceModel)
|
||||
append("; ")
|
||||
append("Android ")
|
||||
append(androidVersion)
|
||||
append("; ")
|
||||
append(deviceBuildId)
|
||||
append("; ")
|
||||
append("Flavour ")
|
||||
append(flavorDescription)
|
||||
append("; ")
|
||||
append("MatrixAndroidSdk2 ")
|
||||
append(matrixSdkVersion)
|
||||
append(")")
|
||||
}
|
||||
userAgent = computeUserAgentUseCase.execute(context, matrixConfiguration.applicationFlavor)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue