Skip to content

Commit

Permalink
Merge pull request #25 from thegrizzlylabs/add-app-build-number
Browse files Browse the repository at this point in the history
feat(Environment) Add app build number to signal payload
  • Loading branch information
kkostov authored May 7, 2024
2 parents 2c4962e + fb23322 commit 08fe243
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class EnvironmentMetadataProvider : TelemetryProvider {
if (!appVersion.isNullOrEmpty()) {
metadata["appVersion"] = appVersion
}
ManifestMetadataReader.getBuildNumber(ctx)?.let { buildNumber ->
metadata["buildNumber"] = buildNumber.toString()
}
} else {
manager.logger?.error("EnvironmentMetadataProvider requires a context but received null. Signals will contain incomplete metadata.")
}
Expand Down Expand Up @@ -73,4 +76,4 @@ class EnvironmentMetadataProvider : TelemetryProvider {
}
return signalPayload
}
}
}
18 changes: 13 additions & 5 deletions lib/src/main/java/com/telemetrydeck/sdk/ManifestMetadataReader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package com.telemetrydeck.sdk

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.core.content.pm.PackageInfoCompat
import java.net.URL
import java.util.*

Expand All @@ -19,13 +21,20 @@ internal class ManifestMetadataReader {
}

fun getAppVersion(context: Context): String? {
try {
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
return packageInfo.versionName
return getPackageInfo(context)?.versionName
}

fun getBuildNumber(context: Context): Long? {
return getPackageInfo(context)?.let { PackageInfoCompat.getLongVersionCode(it) }
}

private fun getPackageInfo(context: Context): PackageInfo? {
return try {
context.packageManager.getPackageInfo(context.packageName, 0)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
null
}
return null
}

private fun getMetaData(context: Context): Bundle? {
Expand Down Expand Up @@ -82,4 +91,3 @@ internal class ManifestMetadataReader {
}
}
}

0 comments on commit 08fe243

Please sign in to comment.