diff --git a/CHANGELOG.md b/CHANGELOG.md index 039fd007307..6445308d26b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Android * Updated to Kotlin 1.5, Android Gradle Plugin 4.2.2 and Gradle 6.7.1 ([#1747](https://github.com/mozilla/glean/pull/1747)) + * `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.0.0 (2021-07-28) diff --git a/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt b/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt index 63beb449a5a..c1f897bfcc2 100644 --- a/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt +++ b/glean-core/android/src/main/java/mozilla/telemetry/glean/Glean.kt @@ -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 @@ -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. @@ -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 @@ -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 @@ -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 @@ -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. @@ -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) { @@ -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) @@ -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) } /** diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanFromJavaTest.java b/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanFromJavaTest.java index 7e95458b33f..5a1e46b01ad 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanFromJavaTest.java +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/GleanFromJavaTest.java @@ -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() { @@ -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 diff --git a/glean-core/android/src/test/java/mozilla/telemetry/glean/scheduler/MetricsPingSchedulerTest.kt b/glean-core/android/src/test/java/mozilla/telemetry/glean/scheduler/MetricsPingSchedulerTest.kt index ae4fc99d6a8..92f1d931630 100644 --- a/glean-core/android/src/test/java/mozilla/telemetry/glean/scheduler/MetricsPingSchedulerTest.kt +++ b/glean-core/android/src/test/java/mozilla/telemetry/glean/scheduler/MetricsPingSchedulerTest.kt @@ -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 @@ -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. @@ -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 @@ -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