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

Add User-Agent with the App Data #1192

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ internal class CoordinatorConnectionModule(
}

// API
override val http: OkHttpClient = OkHttpClient.Builder().addInterceptor(HeadersInterceptor())
override val http: OkHttpClient = OkHttpClient.Builder().addInterceptor(
HeadersInterceptor(context),
)
.addInterceptor(authInterceptor).addInterceptor(
HttpLoggingInterceptor().apply {
level = loggingLevel.httpLoggingLevel.level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,71 @@

package io.getstream.video.android.core.internal.module

import android.content.Context
import android.os.Build
import io.getstream.video.android.core.StreamVideo
import okhttp3.Interceptor
import okhttp3.Response
import java.text.Normalizer

internal class HeadersInterceptor(context: Context) : Interceptor {

private val userAgent = buildUserAgent(context)

internal class HeadersInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
.newBuilder()
.addHeader("User-Agent", userAgent)
.addHeader("X-Stream-Client", StreamVideo.buildSdkTrackingHeaders())
.build()
return chain.proceed(request)
}

private fun buildUserAgent(context: Context): String {
with(context.packageManager) {
val versionName = runCatching {
getPackageInfo(context.packageName, 0).versionName
}.getOrNull() ?: "nameNotFound"
val versionCode = runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getPackageInfo(context.packageName, 0).longVersionCode.toString()
} else {
getPackageInfo(context.packageName, 0).versionCode.toString()
}
}.getOrNull() ?: "versionCodeNotFound"

val applicationInfo = context.applicationInfo
val stringId = applicationInfo.labelRes
val appName =
if (stringId == 0) {
applicationInfo.nonLocalizedLabel.toString()
} else {
context.getString(stringId)
}

val manufacturer = Build.MANUFACTURER
val model = Build.MODEL
val version = Build.VERSION.SDK_INT
val versionRelease = Build.VERSION.RELEASE

val installerName = runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getInstallSourceInfo(context.packageName).installingPackageName
} else {
getInstallerPackageName(context.packageName)
}
}.getOrNull() ?: "StandAloneInstall"

return (
"$appName / $versionName($versionCode); $installerName; ($manufacturer; " +
"$model; SDK $version; Android $versionRelease)"
)
.sanitize()
}
}

private fun String.sanitize(): String {
return Normalizer.normalize(this, Normalizer.Form.NFD)
.replace("[^\\p{ASCII}]".toRegex(), "")
}
}
Loading