Skip to content

Commit

Permalink
Android: Remove support for reading app version info from package man…
Browse files Browse the repository at this point in the history
…ager

This removes the previously deprecated functionatilty.
Passing in the build-time generated `BuildInfo` struct is required.
  • Loading branch information
badboy committed Sep 13, 2021
1 parent 646ec65 commit 7d495c4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 89 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* General
* BUGFIX: Only clear specified storage in delayed ping io mode ([#1782](https://github.com/mozilla/glean/pull/1782))
* Require Rust >= 1.53.0 ([#1782](https://github.com/mozilla/glean/pull/1782))
* Android
* `Glean.initialize` now requires a `buildInfo` parameter to pass in build time version information. A suitable instance is generated by `glean_parser` in `${PACKAGE_ROOT}.GleanMetrics.GleanBuildInfo.buildInfo`. Support for not passing in a `buildInfo` object has been removed. ([#1752](https://github.com/mozilla/glean/pull/1752))

# v40.2.0 (2021-09-08)

Expand Down
95 changes: 11 additions & 84 deletions glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package mozilla.telemetry.glean
import android.app.ActivityManager
import android.util.Log
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Process
import androidx.annotation.MainThread
Expand Down Expand Up @@ -105,42 +103,7 @@ open class GleanInternalAPI internal constructor () {
internal var isSendingToTestEndpoint: Boolean = false

// Store the build information provided by the application.
internal var buildInfo: BuildInfo? = null

/**
* Initialize the Glean SDK.
*
* This should only be initialized once by the application, and not by
* libraries using the Glean SDK. A message is logged to error and no
* changes are made to the state if initialize is called a more than
* once.
*
* A LifecycleObserver will be added to send pings when the application goes
* into foreground and background.
*
* This method must be called from the main thread.
*
* This form of `initialize` is deprecated. Use the form that requires a
* `buildInfo` parameter instead.
*
* @param applicationContext [Context] to access application features, such
* as shared preferences
* @param uploadEnabled A [Boolean] that determines whether telemetry is enabled.
* If disabled, all persisted metrics, events and queued pings (except
* first_run_date and first_run_hour) are cleared.
* @param configuration A Glean [Configuration] object with global settings.
*/
@JvmOverloads
@Synchronized
@MainThread
@Deprecated("The buildInfo parameter will be required in the future. See bug 1691953")
fun initialize(
applicationContext: Context,
uploadEnabled: Boolean,
configuration: Configuration = Configuration()
) {
return initializeInternal(applicationContext, uploadEnabled, configuration, null)
}
internal lateinit var buildInfo: BuildInfo

/**
* Initialize the Glean SDK.
Expand All @@ -165,6 +128,7 @@ open class GleanInternalAPI internal constructor () {
* object is generated at build time by glean_parser at the import path
* ${YOUR_PACKAGE_ROOT}.GleanMetrics.GleanBuildInfo.buildInfo
*/
@Suppress("ReturnCount", "LongMethod", "ComplexMethod")
@JvmOverloads
@Synchronized
@MainThread
Expand All @@ -173,18 +137,6 @@ open class GleanInternalAPI internal constructor () {
uploadEnabled: Boolean,
configuration: Configuration = Configuration(),
buildInfo: BuildInfo
) {
return initializeInternal(applicationContext, uploadEnabled, configuration, buildInfo)
}

@Suppress("ReturnCount", "LongMethod", "ComplexMethod")
@Synchronized
@MainThread
internal fun initializeInternal(
applicationContext: Context,
uploadEnabled: Boolean,
configuration: Configuration = Configuration(),
buildInfo: BuildInfo? = null
) {
this.buildInfo = buildInfo

Expand Down Expand Up @@ -278,7 +230,7 @@ open class GleanInternalAPI internal constructor () {
// The next times we start, we would have them around already.
val isFirstRun = LibGleanFFI.INSTANCE.glean_is_first_run().toBoolean()
if (isFirstRun) {
initializeCoreMetrics(applicationContext)
initializeCoreMetrics()
}

// Deal with any pending events so we can start recording new ones
Expand Down Expand Up @@ -309,7 +261,7 @@ open class GleanInternalAPI internal constructor () {
// Any new value will be sent in newly generated pings after startup.
if (!isFirstRun) {
LibGleanFFI.INSTANCE.glean_clear_application_lifetime_metrics()
initializeCoreMetrics(applicationContext)
initializeCoreMetrics()
}

// Signal the RLB dispatcher to unblock, if any exists.
Expand Down Expand Up @@ -393,7 +345,7 @@ open class GleanInternalAPI internal constructor () {
if (!originalEnabled && enabled) {
// If uploading is being re-enabled, we have to restore the
// application-lifetime metrics.
initializeCoreMetrics((this@GleanInternalAPI).applicationContext)
initializeCoreMetrics()
}

if (originalEnabled && !enabled) {
Expand Down Expand Up @@ -557,42 +509,15 @@ open class GleanInternalAPI internal constructor () {
/**
* Initialize the core metrics internally managed by Glean (e.g. client id).
*/
private fun initializeCoreMetrics(applicationContext: Context) {
private fun initializeCoreMetrics() {
// Set a few more metrics that will be sent as part of every ping.
// Please note that the following metrics must be set synchronously, so
// that they are guaranteed to be available with the first ping that is
// generated. We use an internal only API to do that.

// Set required information first.
buildInfo?.let {
GleanInternalMetrics.appBuild.setSync(it.versionCode)
GleanInternalMetrics.appDisplayVersion.setSync(it.versionName)
} ?: run {
val packageInfo: PackageInfo

try {
packageInfo = applicationContext.packageManager.getPackageInfo(
applicationContext.packageName, 0
)
} catch (e: PackageManager.NameNotFoundException) {
Log.e(
LOG_TAG,
"Could not get own package info, unable to report build id and display version"
)

GleanInternalMetrics.appBuild.setSync("inaccessible")
GleanInternalMetrics.appDisplayVersion.setSync("inaccessible")

return
}

@Suppress("DEPRECATION")
GleanInternalMetrics.appBuild.setSync(packageInfo.versionCode.toString())

GleanInternalMetrics.appDisplayVersion.setSync(
packageInfo.versionName?.let { it } ?: "Unknown"
)
}
GleanInternalMetrics.appBuild.setSync(buildInfo.versionCode)
GleanInternalMetrics.appDisplayVersion.setSync(buildInfo.versionName)

GleanInternalMetrics.architecture.setSync(Build.SUPPORTED_ABIS[0])
GleanInternalMetrics.osVersion.setSync(Build.VERSION.RELEASE)
Expand Down Expand Up @@ -845,7 +770,9 @@ open class GleanInternalAPI internal constructor () {
Glean.testDestroyGleanHandle()
// Always log pings for tests
Glean.setLogPings(true)
Glean.initializeInternal(context, uploadEnabled, config, null)

val buildInfo = BuildInfo(versionCode = "0.0.1", versionName = "0.0.1")
Glean.initialize(context, uploadEnabled, config, buildInfo)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class GleanFromJavaTest {
// methods at build-time.

private Context appContext = TestUtilKt.getContextWithMockedInfo("java-test");
private BuildInfo buildInfo = new BuildInfo("java-test", "java-test");

@Before
public void setup() {
Expand All @@ -37,14 +38,15 @@ public void setup() {

@Test
public void testInitGleanWithDefaults() {
Glean.INSTANCE.initialize(appContext, true);
Configuration config = new Configuration();
Glean.INSTANCE.initialize(appContext, true, config, buildInfo);
}

@Test
public void testInitGleanWithConfiguration() {
Configuration config =
new Configuration(Configuration.DEFAULT_TELEMETRY_ENDPOINT, "test-channel");
Glean.INSTANCE.initialize(appContext, true, config);
Glean.INSTANCE.initialize(appContext, true, config, buildInfo);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.testing.WorkManagerTestInitHelper
import mozilla.telemetry.glean.any
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.BuildInfo
import mozilla.telemetry.glean.GleanBuildInfo
import mozilla.telemetry.glean.GleanMetrics.Pings
import mozilla.telemetry.glean.checkPingSchema
Expand Down Expand Up @@ -497,9 +498,12 @@ class MetricsPingSchedulerTest {

val oldVersion = "version.0"
val oldContext = getContextWithMockedInfo(oldVersion)
val oldBuildInfo = BuildInfo(versionCode = oldVersion, versionName = oldVersion)

// New version
val newContext = getContextWithMockedInfo("version.1")
val newVersion = "version.1"
val newContext = getContextWithMockedInfo(newVersion)
val newBuildInfo = BuildInfo(versionCode = newVersion, versionName = newVersion)

try {
// Initialize Glean for the first time.
Expand All @@ -509,7 +513,8 @@ class MetricsPingSchedulerTest {
Glean.initialize(
oldContext,
true,
configuration
configuration,
oldBuildInfo
)

// Create a metric and set its value. We expect this to be sent after the restart
Expand Down Expand Up @@ -539,7 +544,8 @@ class MetricsPingSchedulerTest {
Glean.initialize(
newContext,
true,
configuration
configuration,
newBuildInfo
)

// Unfortunately, we need to delay a bit here to give init time to run because we are
Expand Down

0 comments on commit 7d495c4

Please sign in to comment.