Skip to content

Commit

Permalink
Merge pull request #1068 from Dexterp37/debug_activity_options
Browse files Browse the repository at this point in the history
Bug 1634064 - Propagate `GleanDebugActivity` intent options to the main intent
  • Loading branch information
Dexterp37 authored Jul 20, 2020
2 parents f798534 + 13442b1 commit fdf341f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

[Full changelog](https://github.com/mozilla/glean/compare/v31.4.0...main)

* General
* Enable propagating options to the main product Activity when using the `GleanDebugActivity`.

# v31.4.0 (2020-07-16)

[Full changelog](https://github.com/mozilla/glean/compare/v31.3.0...v31.4.0)

* General
* Enable debugging features through environment variables. ([#1058](https://github.com/mozilla/glean/pull/1058))

[Full changelog](https://github.com/mozilla/glean/compare/v31.3.0...v31.4.0)

# v31.3.0 (2020-07-10)

[Full changelog](https://github.com/mozilla/glean/compare/v31.2.3...v31.3.0)
Expand Down
3 changes: 3 additions & 0 deletions docs/user/debugging/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ In the above:
| `sendPing` | string (`--es`) | Sends the ping with the given name immediately |
| `tagPings` | string (`--es`) | Tags all outgoing pings as debug pings to make them available for real-time validation, on the [Glean Debug View](./debug-ping-view.md). The value must match the pattern `[a-zA-Z0-9-]{1,20}` |

All [the options](https://developer.android.com/studio/command-line/adb#am) provided to start the activity are passed over to the main activity for the application to process.
This is useful if SDK users wants to debug telemetry while providing additional options to the product to enable specific behaviors.

> **Note:** Due to limitations on Android logcat message size, pings larger than 4KB are broken into multiple log messages when using `logPings`.
For example, to direct a release build of the Glean sample application to (1) dump pings to logcat, (2) tag the ping with the `test-metrics-ping` tag, and (3) send the "metrics" ping immediately, the following command can be used:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package mozilla.telemetry.glean.debug

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import mozilla.telemetry.glean.Glean
Expand Down Expand Up @@ -80,14 +81,14 @@ class GleanDebugActivity : Activity() {

// Check for ping debug view tag to apply to the X-Debug-ID header when uploading the
// ping to the endpoint
var debugViewTag: String? = intent.getStringExtra(TAG_DEBUG_VIEW_EXTRA_KEY)
val debugViewTag: String? = intent.getStringExtra(TAG_DEBUG_VIEW_EXTRA_KEY)

// Set the debug view tag, if the tag is invalid it won't be set
debugViewTag?.let {
Glean.setDebugViewTag(debugViewTag)
}

var logPings: Boolean? = intent.getBooleanExtra(LOG_PINGS_EXTRA_KEY, false)
val logPings: Boolean? = intent.getBooleanExtra(LOG_PINGS_EXTRA_KEY, false)
logPings?.let {
Glean.setLogPings(logPings)
}
Expand All @@ -97,8 +98,18 @@ class GleanDebugActivity : Activity() {
}
}

val intent = packageManager.getLaunchIntentForPackage(packageName)
startActivity(intent)
// This Activity can be used to tag tests on CI or start products with specific
// options. We need to make sure to retain and propagate all the options that
// we were passed to the next intent. Our strategy:
// - use the `Intent` copy constructor to copy all the intent options from the
// intent which started this activity;
// - get the main launch intent for the product using the Glean SDK;
// - change the starting "component" and package to the one from the previous step.
val nextIntent = Intent(intent)
val defaultLaunchIntent = packageManager.getLaunchIntentForPackage(packageName)!!
nextIntent.component = defaultLaunchIntent.component
nextIntent.`package` = defaultLaunchIntent.`package`
startActivity(nextIntent)

finish()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,22 @@ class GleanDebugActivityTest {
}

@Test
fun `the main activity is correctly started`() {
fun `the main activity is correctly started and intent args are propagated`() {
// Build the intent that will call our debug activity, with no extra.
val intent = Intent(ApplicationProvider.getApplicationContext<Context>(),
GleanDebugActivity::class.java)
// Add at least an option, otherwise the activity will be removed.
intent.putExtra(GleanDebugActivity.LOG_PINGS_EXTRA_KEY, true)
intent.putExtra("TestOptionFromCLI", "TestValue")
// Start the activity through our intent.
val scenario = launch<GleanDebugActivity>(intent)

// Check that our main activity was launched.
scenario.onActivity { activity ->
assertEquals(testPackageName,
shadowOf(activity).peekNextStartedActivityForResult().intent.`package`!!)
val startedIntent = shadowOf(activity).peekNextStartedActivityForResult().intent
assertEquals(testPackageName, startedIntent.`package`!!)
// Make sure that the extra intent option was propagated to this intent.
assertEquals("TestValue", startedIntent.getStringExtra("TestOptionFromCLI"))
}
}

Expand Down

0 comments on commit fdf341f

Please sign in to comment.