From ad6f6d17b805c65ff83031144d15ac27eda701a9 Mon Sep 17 00:00:00 2001 From: Colin Lee Date: Fri, 14 Jun 2019 12:08:46 -0500 Subject: [PATCH] [fenix] No issue: Add Sentry breadcrumbs to ease crash investigations --- .../java/org/mozilla/fenix/HomeActivity.kt | 64 +++++++++++++------ .../org/mozilla/fenix/components/Analytics.kt | 4 +- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index bfff887158..a2aca84a05 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -12,9 +12,13 @@ import android.view.View import androidx.annotation.IdRes import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.navigation.NavController import androidx.navigation.fragment.NavHostFragment import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.NavigationUI +import io.sentry.Sentry +import io.sentry.event.Breadcrumb +import io.sentry.event.BreadcrumbBuilder import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -28,6 +32,7 @@ import mozilla.components.support.base.feature.BackHandler import mozilla.components.support.ktx.kotlin.isUrl import mozilla.components.support.ktx.kotlin.toNormalizedUrl import mozilla.components.support.utils.SafeIntent +import org.mozilla.fenix.components.isSentryEnabled import org.mozilla.fenix.components.metrics.Event import org.mozilla.fenix.ext.components import org.mozilla.fenix.ext.nav @@ -55,32 +60,30 @@ open class HomeActivity : AppCompatActivity() { lateinit var browsingModeManager: BrowsingModeManager + private val onDestinationChangedListener = NavController.OnDestinationChangedListener { _, dest, _ -> + val fragmentName = resources.getResourceEntryName(dest.id) + Sentry.getContext().recordBreadcrumb( + BreadcrumbBuilder() + .setCategory("DestinationChanged") + .setMessage("Changing to fragment $fragmentName, isCustomTab: $isCustomTab") + .setLevel(Breadcrumb.Level.INFO) + .build() + ) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) components.publicSuffixList.prefetch() - browsingModeManager = createBrowsingModeManager() - themeManager = createThemeManager( - when (browsingModeManager.isPrivate) { - true -> ThemeManager.Theme.Private - false -> ThemeManager.Theme.Normal - } - ) - - setTheme(themeManager.currentTheme) - ThemeManager.applyStatusBarTheme(window, themeManager, this) + setupThemeAndBrowsingMode() setContentView(R.layout.activity_home) - // Add ids to this that we don't want to have a toolbar back button - val appBarConfiguration = AppBarConfiguration.Builder().build() - val navigationToolbar = findViewById(R.id.navigationToolbar) - setSupportActionBar(navigationToolbar) - NavigationUI.setupWithNavController(navigationToolbar, navHost.navController, appBarConfiguration) - navigationToolbar.setNavigationOnClickListener { - onBackPressed() + setupToolbarAndNavigation() + + if (Settings.getInstance(this).isTelemetryEnabled && isSentryEnabled()) { + navHost.navController.addOnDestinationChangedListener(onDestinationChangedListener) } - supportActionBar?.hide() intent ?.let { SafeIntent(it) } @@ -97,8 +100,33 @@ open class HomeActivity : AppCompatActivity() { handleOpenedFromExternalSourceIfNecessary(intent) } + private fun setupThemeAndBrowsingMode() { + browsingModeManager = createBrowsingModeManager() + themeManager = createThemeManager( + when (browsingModeManager.isPrivate) { + true -> ThemeManager.Theme.Private + false -> ThemeManager.Theme.Normal + } + ) + setTheme(themeManager.currentTheme) + ThemeManager.applyStatusBarTheme(window, themeManager, this) + } + + private fun setupToolbarAndNavigation() { + // Add ids to this that we don't want to have a toolbar back button + val appBarConfiguration = AppBarConfiguration.Builder().build() + val navigationToolbar = findViewById(R.id.navigationToolbar) + setSupportActionBar(navigationToolbar) + NavigationUI.setupWithNavController(navigationToolbar, navHost.navController, appBarConfiguration) + navigationToolbar.setNavigationOnClickListener { + onBackPressed() + } + supportActionBar?.hide() + } + override fun onDestroy() { sessionObserver?.let { components.core.sessionManager.unregister(it) } + navHost.navController.removeOnDestinationChangedListener(onDestinationChangedListener) super.onDestroy() } diff --git a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt index 365d7d3206..2d497bfb90 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Analytics.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Analytics.kt @@ -34,7 +34,7 @@ class Analytics( val crashReporter: CrashReporter by lazy { val services = mutableListOf() - if (!BuildConfig.SENTRY_TOKEN.isNullOrEmpty()) { + if (isSentryEnabled()) { val sentryService = SentryService( context, BuildConfig.SENTRY_TOKEN, @@ -85,3 +85,5 @@ class Analytics( ) } } + +fun isSentryEnabled() = !BuildConfig.SENTRY_TOKEN.isNullOrEmpty()