diff --git a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt index 0d9a3b2c0f88..4d611ababeb0 100644 --- a/app/src/main/java/org/mozilla/fenix/FenixApplication.kt +++ b/app/src/main/java/org/mozilla/fenix/FenixApplication.kt @@ -11,6 +11,10 @@ import android.os.StrictMode import androidx.annotation.CallSuper import androidx.appcompat.app.AppCompatDelegate import androidx.core.content.getSystemService +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ProcessLifecycleOwner +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent import kotlinx.coroutines.Deferred import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -39,6 +43,7 @@ import mozilla.components.support.utils.logElapsedTime import mozilla.components.support.webextensions.WebExtensionSupport import org.mozilla.fenix.StrictModeManager.enableStrictMode import org.mozilla.fenix.components.Components +import org.mozilla.fenix.components.metrics.AppAllStartTelemetry import org.mozilla.fenix.components.metrics.MetricServiceType import org.mozilla.fenix.ext.resetPoliciesAfter import org.mozilla.fenix.ext.settings @@ -55,7 +60,7 @@ import org.mozilla.fenix.utils.Settings * Installs [CrashReporter], initializes [Glean] in fenix builds and setup Megazord in the main process. */ @Suppress("Registered", "TooManyFunctions", "LargeClass") -open class FenixApplication : LocaleAwareApplication() { +open class FenixApplication : LocaleAwareApplication(), LifecycleObserver { init { recordOnInit() // DO NOT MOVE ANYTHING ABOVE HERE: the timing of this measurement is critical. } @@ -90,6 +95,7 @@ open class FenixApplication : LocaleAwareApplication() { } setupInMainProcessOnly() + ProcessLifecycleOwner.get().lifecycle.addObserver(this) } protected fun initializeGlean() { @@ -407,4 +413,9 @@ open class FenixApplication : LocaleAwareApplication() { companion object { private const val KINTO_ENDPOINT_PROD = "https://firefox.settings.services.mozilla.com/v1" } + + @OnLifecycleEvent(Lifecycle.Event.ON_STOP) + fun applicationOnBackground() { + AppAllStartTelemetry.isApplicationOnBackground = true + } } diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index db2f37b1d133..843b01db6c94 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -57,6 +57,7 @@ import org.mozilla.fenix.browser.UriOpenedObserver import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.browser.browsingmode.BrowsingModeManager import org.mozilla.fenix.browser.browsingmode.DefaultBrowsingModeManager +import org.mozilla.fenix.components.metrics.AppAllStartTelemetry import org.mozilla.fenix.components.metrics.BreadcrumbsRecorder import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.exceptions.ExceptionsFragmentDirections @@ -134,6 +135,8 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { private lateinit var navigationToolbar: Toolbar + lateinit var appAllStartTelemetry: AppAllStartTelemetry + final override fun onCreate(savedInstanceState: Bundle?) { StrictModeManager.changeStrictModePolicies(supportFragmentManager) // There is disk read violations on some devices such as samsung and pixel for android 9/10 @@ -197,6 +200,12 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { } captureSnapshotTelemetryMetrics() + + setupAppAllStartTelemetry() + } + + internal open fun setupAppAllStartTelemetry() { + appAllStartTelemetry = AppAllStartTelemetry(intent, components.analytics.metrics, false) } @CallSuper @@ -255,6 +264,8 @@ open class HomeActivity : LocaleAwareAppCompatActivity() { ?.also { it.dismissAllowingStateLoss() } } + appAllStartTelemetry.onNewIntentHomeActivity(intent) + // If there is a warm or hot startup, onNewIntent method is always called first. // Note: This does not work in case of an user sending an intent with ACTION_VIEW // for example, launch the application, and than use adb to send an intent with diff --git a/app/src/main/java/org/mozilla/fenix/components/metrics/AppAllStartTelemetry.kt b/app/src/main/java/org/mozilla/fenix/components/metrics/AppAllStartTelemetry.kt new file mode 100644 index 000000000000..45f131718a99 --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/components/metrics/AppAllStartTelemetry.kt @@ -0,0 +1,43 @@ +package org.mozilla.fenix.components.metrics + +import android.content.Intent +import mozilla.components.support.utils.SafeIntent +import mozilla.components.support.utils.toSafeIntent + +class AppAllStartTelemetry( + intent: Intent?, + private val metrics: MetricController, + private val isExternalAppBrowserActivity: Boolean +) { + + init { + intent?.toSafeIntent()?.let(::getIntentAllStartSource) + ?.also { + metrics.track(Event.AppRecievedIntent(it)) + } + } + + fun onNewIntentHomeActivity(intent: Intent?) { + if (isApplicationOnBackground) { + intent?.toSafeIntent()?.let(::getIntentAllStartSource) + ?.also { + metrics.track(Event.AppRecievedIntent(it)) + } + isApplicationOnBackground = false + } + } + + private fun getIntentAllStartSource(intent: SafeIntent): Event.AppRecievedIntent.Source? { + return when { + isExternalAppBrowserActivity + -> Event.AppRecievedIntent.Source.CUSTOM_TAB + intent.isLauncherIntent -> Event.AppRecievedIntent.Source.APP_ICON + intent.action == Intent.ACTION_VIEW -> Event.AppRecievedIntent.Source.LINK + else -> Event.AppRecievedIntent.Source.UNKNOWN + } + } + + companion object { + var isApplicationOnBackground = false + } +} diff --git a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt index 2914f7326206..097bd4247afb 100644 --- a/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/customtabs/ExternalAppBrowserActivity.kt @@ -18,6 +18,7 @@ import org.mozilla.fenix.BrowserDirection import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.IntentReceiverActivity import org.mozilla.fenix.NavGraphDirections +import org.mozilla.fenix.components.metrics.AppAllStartTelemetry import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.components import java.security.InvalidParameterException @@ -45,6 +46,10 @@ open class ExternalAppBrowserActivity : HomeActivity() { final override fun getIntentSessionId(intent: SafeIntent) = intent.getSessionId() + override fun setupAppAllStartTelemetry() { + super.appAllStartTelemetry = AppAllStartTelemetry(intent, components.analytics.metrics, true) + } + override fun getNavDirections( from: BrowserDirection, customTabSessionId: String?