Skip to content

Commit

Permalink
for mozilla-mobile#11830 created class containing the logic for sendi…
Browse files Browse the repository at this point in the history
…ng AllStartup telemetry logic

lint check
  • Loading branch information
sraturi committed Jun 29, 2020
1 parent 70f90e7 commit c1506b2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/src/main/java/org/mozilla/fenix/FenixApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
}
Expand Down Expand Up @@ -90,6 +95,7 @@ open class FenixApplication : LocaleAwareApplication() {
}

setupInMainProcessOnly()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

protected fun initializeGlean() {
Expand Down Expand Up @@ -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
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/org/mozilla/fenix/HomeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -197,6 +200,12 @@ open class HomeActivity : LocaleAwareAppCompatActivity() {
}

captureSnapshotTelemetryMetrics()

setupAppAllStartTelemetry()
}

internal open fun setupAppAllStartTelemetry() {
appAllStartTelemetry = AppAllStartTelemetry(intent, components.analytics.metrics, false)
}

@CallSuper
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down

0 comments on commit c1506b2

Please sign in to comment.