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 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package io.getstream.video.android.core.internal.module

import android.content.Context
import android.net.ConnectivityManager
import android.os.Build
import io.getstream.video.android.core.StreamVideo
import io.getstream.video.android.core.api.SignalServerService
import io.getstream.video.android.core.dispatchers.DispatcherProvider
Expand All @@ -40,6 +41,7 @@ import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import retrofit2.converter.wire.WireConverterFactory
import java.io.IOException
import java.text.Normalizer
import java.util.concurrent.TimeUnit

/**
Expand Down Expand Up @@ -67,7 +69,7 @@ internal class ConnectionModule(
private val authInterceptor: CoordinatorAuthInterceptor by lazy {
CoordinatorAuthInterceptor(apiKey, userToken)
}
private val headersInterceptor: HeadersInterceptor by lazy { HeadersInterceptor() }
private val headersInterceptor: HeadersInterceptor by lazy { HeadersInterceptor(context) }
val okHttpClient: OkHttpClient by lazy { buildOkHttpClient() }
val networkStateProvider: NetworkStateProvider by lazy {
NetworkStateProvider(
Expand Down Expand Up @@ -288,12 +290,65 @@ internal class CoordinatorAuthInterceptor(
}
}

internal class HeadersInterceptor : Interceptor {
internal class HeadersInterceptor(
context: Context,
) : Interceptor {
private val userAgent by lazy { buildUserAgent(context) }

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
.newBuilder()
.addHeader("User-Agent", userAgent)
.addHeader("X-Stream-Client", StreamVideo.buildSdkTrackingHeaders())
JcMinarro marked this conversation as resolved.
Show resolved Hide resolved
.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